Creating a Deployment Package (Python)
To create a Lambda function you first create a Lambda function deployment package, a .zip file consisting of your code and any dependencies.
You can create a deployment package yourself or write your code directly in the Lambda console, in which case the console creates the deployment package for you and uploads it, creating your Lambda function. Note the following to determine if you can use the console to create your Lambda function:
Simple scenario – If your custom code requires only the AWS SDK library, then you can use the inline editor in the AWS Lambda console. Using the console, you can edit and upload your code to AWS Lambda. The console will zip up your code with the relevant configuration information into a deployment package that the Lambda service can run.
You can also test your code in the console by manually invoking it using sample event data.
The Lambda service has preinstalled the AWS SDK for Python.
Advanced scenario – If you are writing code that uses other resources, such as a graphics library for image processing, or you want to use the AWS CLI instead of the console, you need to first create the Lambda function deployment package, and then use the console or the CLI to upload the package.
After you create a deployment package, you may either upload it directly or upload the .zip file first to an Amazon S3 bucket in the same AWS region where you want to create the Lambda function, and then specify the bucket name and object key name when you create the Lambda function using the console or the AWS CLI.
The following is an example procedure to create a deployment package (outside the console).
This should work for most standard installations of Python and pip when using pure Python modules in your Lambda function. If you are including modules that have native dependencies or have Python installed with Homebrew on OS X, you should see the next section which provides instructions to create a deployment package when using Virtualenv. For more information, see Create Deployment Package Using a Python Environment Created with Virtualenv and the Virtualenv website.
You will use
pip to install dependencies/libraries. For information to install
pip, go to Installation.
You create a directory, for example
Save all of your Python source files (the .py files) at the root level of this directory.
Install any libraries using pip. Again, you install these libraries at the root level of the directory.
For example, the following command installs the
requestsHTTP library in the
pip install requests -t
If using Mac OS X and you have Python installed using Homebrew (see Homebrew), the preceding command will not work. A simple workaround is to add a
setup.cfgfile in your
with the following content.
Zip the content of the
project-dirdirectory, which is your deployment package.
Zip the directory content, not the directory. The contents of the Zip file are available as the current working directory of the Lambda function. For example: /project-dir/codefile.py/lib/yourlibraries
AWS Lambda includes the AWS SDK for Python (Boto 3), so you don't need to include it in your deployment package. However, if you want to use a version of Boto3 other than the one included by default, you can include it in your deployment package.
Create Deployment Package Using a Python Environment Created with Virtualenv
This section explains how to create a deployment package if you are using a Python environment that you created with the Virtualenv tool. Consider the following example:
Created the following isolated Python environment using the Virtualenv tool and activated the environment:
You can activate the environment on Windows, OS X, and Linux as follows:
On Windows, you activate using the
On OS X and Linux, you source the
Also, suppose you have installed the requests package in the activated environment (assume that you will you use these in your code). You can install these packages as follows :
pip install requests
Now, to create a deployment package you do the following:
First, create .zip file with your Python code you want to upload to AWS Lambda.
Add the libraries from preceding activated virtual environment to the .zip file. That is, you add the content of the following directory to the .zip file (note again that you add the content of the directory and not the directory itself).
For Windows the directory is:
For OS X, Linux, the directory is:
If you don't find the packages in the
site-packagesdirectory in your virtual environment, you might find it in the
For an example of creating a Python deployment package, see Python.