Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

amazon aws elastic beanstalk. Custom Configuration Files not working

I've got a problem with Custom Configuration File in aws elastic beanstalk.

My application is python flask app.

I put 01wsgi.config file into .ebextensions.

and zipped it then upload to elastic beanstalk.

The source deployed well, but the configuration didn't executed.

How can I make it works properly?

directory structure:

source_root
  - .ebextensions
     -- 01wsgi.config
  - application
  - application.wsgi

01wsgi.config content:

files:
  "/etc/httpd/conf.d/wsgi.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      LoadModule wsgi_module modules/mod_wsgi.so
      WSGIPythonHome /opt/python/run/baselinenv
      WSGISocketPrefix run/wsgi
      WSGIRestrictEmbedded On

      <VirtualHost *:80>
      #############
      # TYPES FIX #
      #############
      AddType text/css .css
      AddType text/javascript .js

      ####################
      # GZIP COMPRESSION #
      ####################
      SetOutputFilter DEFLATE
      AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml text/javascript application/x-javascript application/x-httpd-php
      BrowserMatch ^Mozilla/4 gzip-only-text/html
      BrowserMatch ^Mozilla/4\.0[678] no-gzip
      BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
      BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
      SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip
      Header append Vary User-Agent env=!dont-vary

      Alias /static/(.*)? /opt/python/current/app/application/frontend/static-build/
      <Directory /opt/python/current/app/application/frontend/static-build/>
      Order allow,deny
      Allow from all
      Header append Cache-Control "max-age=2592000, must-revalidate"
      </Directory>

      WSGIScriptAlias / /opt/python/current/app/application.py

      <Directory /opt/python/current/app/>
      Order allow,deny
      Allow from all
      </Directory>

      WSGIDaemonProcess wsgi processes=1 threads=15 display-name=%{GROUP} \
      python-path=/opt/python/current/app:/opt/python/run/venv/lib/python2.7/site-packages user=wsgi group=wsgi \
      home=/opt/python/current/app
      WSGIProcessGroup wsgi
      WSGIScriptReloading On
      </VirtualHost>

I followed document below:

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers.html

SOLVED

Put your wsgi.conf file into .ebextensions directory.

And make a config file that copy wsgi.conf to ondeck.

01wsgi.config content:

container_commands:
  replace_wsgi_config:
    command: "cp .ebextensions/wsgi.conf /opt/python/ondeck/wsgi.conf"
like image 694
dorajistyle Avatar asked Apr 04 '14 01:04

dorajistyle


People also ask

Where are Elastic Beanstalk configuration files?

ebextensions, see the Elastic Beanstalk Configuration Files Repository . Location – Place all of your configuration files in a single folder, named . ebextensions , in the root of your source bundle.

What format can source files be in for Amazon Elastic Beanstalk?

Your source bundle must meet the following requirements: Consist of a single ZIP file or WAR file (you can include multiple WAR files inside your ZIP file) Not exceed 512 MB. Not include a parent folder or top-level directory (subdirectories are fine)

Can I use custom AMI in Elastic Beanstalk?

When you create an AWS Elastic Beanstalk environment, you can specify an Amazon Machine Image (AMI) to use instead of the standard Elastic Beanstalk AMI included in your platform version.


1 Answers

I wanted to add some info about another gotcha: Beanstalk will overwrite your apache hosts file if you make changes that do not execute a full deployment (e.g. change environment variables). In most cases, your web server will stop serving up your app, unless you are not actually customizing wsgi.conf.

Now, to be clear, you are correct in that you need to put your WSGI config in the .ebextentions directory. Then you use a container command to move the configuration file to the location that EB looks at. You can do this safely with this command:

# Replace the default wsgi with ours
cp .ebextensions/wsgi.conf ../wsgi.conf

To prevent your custom wsgi.conf from being overwritten during an update that does not execute a deployment, you will need to monkey patch the native EB hook that recreates wsgi.conf. I haven't found any docs around this, but custom hooks are documented and the native ones work in the same fashion.

Here is the monkey patch that I have been using:

# Elastic Beanstalk always forces generation of apache hosts,
# stop it by returning true in the function that does it
sed '    /return True # DO NOT REGENERATE APACHE HOSTS/d' /opt/elasticbeanstalk/hooks/config.py \
  | sed -e 's/def generate_apache_config(params, filename):/def generate_apache_config(params, filename):\n    return True # DO NOT REGENERATE APACHE HOSTS/1' \
  -e 's/if not os.path.exists(WSGI_STAGING_CONFIG):/if not os.path.exists(WSGI_STAGING_CONFIG):\n        return True # DO NOT REGENERATE APACHE HOSTS/1' \
  > /tmp/config.py && mv -f /tmp/config.py /opt/elasticbeanstalk/hooks/config.py

SSH into an EB instance and take a look at /opt/elasticbeanstalk/hooks/config.py and you'll see what EB does during deployments or updates to the environment.

Happy AWSing!

like image 132
Ryan Fisher Avatar answered Oct 06 '22 11:10

Ryan Fisher