AWS Elastic Beanstalk
Developer Guide (API Version 2010-12-01)
AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

Setting Up Your Python Development Environment

AWS Elastic Beanstalk provides a consistent interface for deploying Python applications, so there are common procedures to follow regardless of the application framework you're using, or whether you're using one at all.

Common Prerequisites

For all Python applications that you'll deploy with Elastic Beanstalk, these prerequisites are common:

  1. Python 2.7 or 3.4

  2. The pip utility, matching your Python version. This is used to install and list dependencies for your project, so that Elastic Beanstalk knows how to set up your application's environment.

  3. The virtualenv package. This is used to create an environment used to develop and test your application, so that the environment can be replicated by Elastic Beanstalk without installing extra packages that aren't needed by your application.

  4. The awsebcli package. This is used to initialize your application with the files necessary for deploying with Elastic Beanstalk.

  5. A working ssh installation. This is used to connect with your running instances when you need to examine or debug a deployment.

For instructions on installing Python, pip, and the EB CLI, see Install the Elastic Beanstalk Command Line Interface (EB CLI).

Setting up a virtual Python environment

Once you have the prerequisites installed, set up a virtual environment with virtualenv to install your application's dependencies. By using a virtual environment, you can discern exactly which packages are needed by your application so that the required packages are installed on the EC2 instances that are running your application.

To set up a virtual environment

  1. Open a command-line window and type:

    virtualenv -p python2.7 /tmp/eb_python_app

    Replace eb_python_app with a name that makes sense for your application (using your application's name or directory name is a good idea). The virtualenv command creates a virtual environment for you and prints the results of its actions:

    Running virtualenv with interpreter /usr/bin/python2.7
    New python executable in /tmp/eb_python_app/bin/python2.7
    Also creating executable in /tmp/eb_python_app/bin/python
    Installing setuptools, pip...done.
  2. Once your virtual environment is ready, start it by running the activate script located in the environment's bin directory. For example, to start the eb_python_app environment created in the previous step, you would type:

    . /tmp/eb_python_app/bin/activate

    The virtual environment prints its name (for example: (eb_python_app)) at the beginning of each command prompt, reminding you that you're in a virtual Python environment.

Note

Once created, you can restart the virtual environment at any time by running its activate script again.

Configuring a Python project for Elastic Beanstalk

You can use the Elastic Beanstalk CLI to prepare your Python applications for deployment with Elastic Beanstalk.

To configure a Python application for deployment with Elastic Beanstalk

  1. From within your virtual environment, return to the top of your project's directory tree (python_eb_app), and type:

    pip freeze >requirements.txt

    This command copies the names and versions of the packages that are installed in your virtual environment to requirements.txt, For example, if the PyYAML package, version 3.11 is installed in your virtual environment, the file will contain the line:

    PyYAML==3.11

    This allows Elastic Beanstalk to replicate your application's Python environment using the same packages and same versions that you used to develop and test your application.

  2. Configure the EB CLI repository with the eb init command. Follow the prompts to choose a region, platform and other options. For detailed instructions, see Managing Elastic Beanstalk Environments with the EB CLI.

By default, Elastic Beanstalk looks for a file called application.py to start your application. If this doesn't exist in the Python project that you've created, some adjustment of your application's environment is necessary. You will also need to set environment variables so that your application's modules can be loaded. See Using the AWS Elastic Beanstalk Python Platform for more information.