Ronaldo Bahia Ronaldo Bahia - 1 year ago 116
Apache Configuration Question

AWS ElasticBeanstalk update without modifing Django wsgi.conf

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

Answer Source

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 :

    command: " migrate --noinput"
    leader_only: true
    command: " collectstatic --noinput"
    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