Skip to content

Instantly share code, notes, and snippets.

@djshen-ponddy
Created October 13, 2017 06:20
Show Gist options
  • Save djshen-ponddy/0c44a5d835aad7746518d90c360a1297 to your computer and use it in GitHub Desktop.
Save djshen-ponddy/0c44a5d835aad7746518d90c360a1297 to your computer and use it in GitHub Desktop.
Customize AWS Elastic Beanstalk wsgi.conf without being wiped out after update of environment variables
files:
"/opt/elasticbeanstalk/hooks/configdeploy/pre/99patchwsgi.py": &file
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env python
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
import config
def main():
if 'EB_SYSTEM_STARTUP' in os.environ:
return
try:
WSGI_STAGING_CONFIG = config.get_container_config('wsgi_staging_config')
config.execute(['mkdir', '-p', '/etc/httpd/wsgi.conf.d'])
config.execute(['sed', '-i', '/\<VirtualHost \*:80\>/a IncludeOptional /etc/httpd/wsgi.conf.d/*.conf', WSGI_STAGING_CONFIG])
except Exception, e:
config.emit_error_event(config.USER_ERROR_MESSAGES['badappconfig'])
config.diagnostic('Error patching wsgi.conf during configdeploy/pre: %s' % str(e))
sys.exit(1)
if __name__ == '__main__':
config.configure_stdout_logger()
main()
"/opt/elasticbeanstalk/hooks/appdeploy/pre/99patchwsgi.py": *file
"/etc/httpd/wsgi.conf.d/enable_auth_header.conf":
mode: "000755"
owner: root
group: root
content: |
WSGIPassAuthorization On
@djshen-ponddy
Copy link
Author

djshen-ponddy commented Oct 13, 2017

This script will create two hook files:
/opt/elasticbeanstalk/hooks/configdeploy/pre/99patchwsgi.py
/opt/elasticbeanstalk/hooks/appdeploy/pre/99patchwsgi.py
and will be executed during app deploy or config update.

We can put customized apache configs that must be inside VirtualHost, for example, enable_auth_header.conf,
in /etc/httpd/wsgi.conf.d directory, and the config files will be included by IncludeOptional /etc/httpd/wsgi.conf.d*.conf.

@mmichealjroberts
Copy link

Is there a way to use this to force http => https redirects?

@ankitbeohar90
Copy link

@djshen-ponddy can you elaborate steps how to deploy this script and will it be applicable in autoscale case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment