Updating WSGI automatically in Amazon AWS

Continuing from my previous post about Setting up HTTPs in Amazon AWS, I still haven’t found an elegant solution to automatically insert the necessary redirect rules into Amazon’s WSGI.conf file, but I have found a work around.   Here’s how to replicate what I’m doing at the moment…

  1. ssh into your EC2 instance
  2. copy the contents of /etc/httpd/conf.d/wsgi.conf into a local file called wsgi.conf which will be placed in the base folder of your application
  3. Edit the local version of wsgi.conf and add the following redirect rules within the <VirtualHost>…</VirtualHost> tags
    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule !/about https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
  4. Change the “/about” to whatever page you are using as a health check page.
  5. Save the file
  6. Edit your <app>.conf file inside your .ebextensions directory to add a container command to copy this version of wsgi.conf over Amazon’s version
    container_commands:
    01_syncdb:
      command: "django-admin.py syncdb --noinput" leader_only: true
    02_collectstatic:
      command: "django-admin.py collectstatic --noinput"
    03_wsgireplace:
     command: 'cp wsgi.conf ../wsgi.conf'
    ...
  7. Deploy the code.
  8. The deployed version of wsg.conf at /etc/httd/conf.d/wsgi.conf will now include the necessary redirect rules.

Voila!   It should work and the file will be properly updated for each deployment.  The only thing to watch for is if Amazon changes their base wsgi.conf file contents in the future, then your copy may no longer work.

5 comments

Leave a comment