AWS ElasticBeanstalk update without modifing Django wsgi.conf
<p>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:</p>

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

WSGIPassAuthorization On
</code></pre>

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

<p>How can I avoid that?</p>

<p>Thanks in advance</p>

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

<p>(After having tested these modifications as you did 'manually' using <code>eb ssh</code>)</p>

<p>In that case you could use for example a <code>sed command</code> to edit your <code>wsgi.conf</code> file.</p>

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

<p><code>03_wsgipass:
    command: 'sed -i -f .ebextensions/wsgi_update.sed ../wsgi.conf'
</code></p>

<p>It should then look like this :</p>

<pre><code>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'
</code></pre>

<p>create a new file in <code>wsgi_update.sed</code> in the <code>.ebextensions</code> folder with the following content:</p>

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

/&lt;\/Virtual/ a\
WSGIPassAuthorization On
</code></pre>

<p>This is a small <a href="https://www.gnu.org/software/sed/manual/sed.html#sed-Programs" rel="nofollow">sed program</a> for example that will add the apache <code>mod_rewrite</code> rules inside your <code>&lt;VirtualHost&gt;</code> block and the <code>WSGIPassAuthorization</code> line after the closing tag <code>&lt;/VirtualHost&gt;</code> in your <code>wsgi.conf</code> file</p>

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

<p>see <a href="http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-container.html#create-deploy-python-custom-container" rel="nofollow">Using the AWS Elastic Beanstalk Python Platform</a> for more info</p>

python django apache amazon-web-services mod-wsgi