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.
For all Python applications that you'll deploy with Elastic Beanstalk, these prerequisites are common:
Python 2.7 or 3.4
piputility, 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.
virtualenvpackage. 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.
awsebclipackage. This is used to initialize your application with the files necessary for deploying with Elastic Beanstalk.
sshinstallation. 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
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
virtualenvcommand 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.
Once your virtual environment is ready, start it by running the
activatescript located in the environment's
bindirectory. For example, to start the
eb_python_appenvironment created in the previous step, you would type:
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.
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
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:
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.
Configure the EB CLI repository with the
eb initcommand. 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.