AWS Elastic Beanstalk
Developer Guide (API Version 2010-12-01)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Customizing and Configuring a Python Container

When deploying your Python application, you may want to customize and configure the behavior of your EC2 instances. You can easily customize your instances at the same time that you deploy your application version by including a configuration file with your source bundle. This section walks you through the process of creating a configuration file and bundling it with your source.

To customize and configure your Python container

  1. Create a configuration file with the extension .config (e.g., myapp.config) and place it in an .ebextensions top-level directory of your source bundle. You can have multiple configuration files in your .ebextensions directory. For information about file format and contents of the configuration file, see Using Configuration Files.

    The following is an example snippet of a configuration file.

    packages:
      yum:
        libmemcached-devel: '0.31'
    
    container_commands:
      collectstatic:
        command: "django-admin.py collectstatic --noinput"
      01syncdb:
        command: "django-admin.py syncdb --noinput"
        leader_only: true
      02migrate:
        command: "django-admin.py migrate"
        leader_only: true
      99customize:
        command: "scripts/customize.sh"
    
    # You can specify any key-value pairs in the aws:elasticbeanstalk:application:environment namespace and it will be 
    # passed in as environment variables on your EC2 instances
    option_settings:
      "aws:elasticbeanstalk:application:environment":
        DJANGO_SETTINGS_MODULE: "djproject.settings"
        "application_stage": "staging"
      "aws:elasticbeanstalk:container:python":
        WSGIPath: djproject/wsgi.py
        NumProcesses: 3
        NumThreads: 20
      "aws:elasticbeanstalk:container:python:staticfiles":
        "/static/": "static/"

    The following is an example of specifying the option_settings in a list format:

    # If you do not specify a namespace, the default used is aws:elasticbeanstalk:application:environment
    option_settings:
      - option_name: PARAM1
        value: somevalue

    Note

    If you want to set environment variables that will be available to your application, you do not need to provide a "namespace" key in the option_settings section.

  2. Create a requirements.txt file and place it in the top-level directory of your source bundle. A typical python application will have dependencies on other third-party Python packages. In Python, pip is the standard way of installing packages. Pip has a feature that allows you to specify all the packages you need (as well as their versions) in a single requirements file. For more information about the requirements file, go to Requirements files. The following is an example requirements.txt file for Django.

    Django==1.4.1
    MySQL-python==1.2.3

    From your working environment, you can also type the following command to generate the requirements file.

    pip install django
    pip install MySQL-python==1.2.3
    pip freeze > requirements.txt
  3. Deploy your application version.

For an example walkthrough of deploying a Django application using an instance configuration file, see Deploying a Django Application to AWS Elastic Beanstalk. For an example walkthrough of deploying a Flask application, see Deploying a Flask Application to AWS Elastic Beanstalk.

Accessing Environment Variables

Inside the Python environment running in AWS Elastic Beanstalk, these values are accessible using Python's os.environ dictionary. For more information, go to http://docs.python.org/library/os.html. For a list of option settings, see Python Container Options.

You might have a code snippet that looks similar to the following to access the keys and parameters:

import os

param1 = os.environ['PARAM1']
django_settings_module = os.environ['DJANGO_SETTINGS_MODULE']