1

I have a django app deployed in AWS EB using autoscaling. This app uses Django Rest with Token Authentication. In order for this to work, I have to add the following lines in etc/httpd/conf.d/wsgi.conf file:

RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

WSGIPassAuthorization On

The problem is: when AWS do an autoscale or ElasticBeanstalk environment upgrade, the wsgi.conf file is updated and the custom settings are deleted.

How can I avoid that?

Thanks in advance

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
Ronaldo Bahia
  • 576
  • 3
  • 24

2 Answers2

4

To avoid ElasticBeanstalk erasing your custom settings while autoscaling or re-initializing any instance in your environment you should use your .ebextensions scripts to do any durable modification to your ec2 instance's config files.

(After having tested these modifications as you did 'manually' using eb ssh)

In that case you could use for example a sed command to edit your wsgi.conf file.

add the following container_command to one of you your yaml Elastic Beanstalk configuration file (ie: .ebextension/yourapp.config):

03_wsgipass: command: 'sed -i -f .ebextensions/wsgi_update.sed ../wsgi.conf'

It should then look like this :

container_commands:
  01_migrate:
    command: "django-admin.py migrate --noinput"
    leader_only: true
  02_collectstatic:
    command: "django-admin.py collectstatic --noinput"
  03_wsgipass:
    command: 'sed -i -f .ebextensions/wsgi_update.sed ../wsgi.conf'

create a new file in wsgi_update.sed in the .ebextensions folder with the following content:

/<Virtual/ a\
RewriteEngine On\
RewriteCond %{HTTP:Authorization} ^(.*)\
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

/<\/Virtual/ a\
WSGIPassAuthorization On

This is a small sed program for example that will add the apache mod_rewrite rules inside your <VirtualHost> block and the WSGIPassAuthorization line after the closing tag </VirtualHost> in your wsgi.conf file

It will be executed on each application deployment to any existing or new instances created by autoscaling in your environment.

see Using the AWS Elastic Beanstalk Python Platform for more info

Amar Pratap
  • 1,000
  • 7
  • 20
0

This is not working i have tried all options nothing seems to work with ebs. I don't really know what else to try.

Amen Abe
  • 1
  • 1
  • This is not an answer. Please move to a comment. – Lorraine R. Feb 10 '22 at 05:04
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/31031694) – fn control option Feb 14 '22 at 23:11