AWS Lambda deployment package in Python

A deployment package is a ZIP archive that contains your function code and dependencies. You need to create a deployment package if you use the Lambda API to manage functions, or if you need to include libraries and dependencies other than the AWS SDK. You can upload the package directly to Lambda, or you can use an Amazon S3 bucket, and then upload it to Lambda. If the deployment package is larger than 50 MB, you must use Amazon S3.

If you use the Lambda console editor to author your function, the console manages the deployment package. You can use this method as long as you don't need to add any libraries. You can also use it to update a function that already has libraries in the deployment package, as long as the total size doesn't exceed 3 MB.

Note You can use the AWS SAM CLI build command to create a deployment package for your Python function code and dependencies. The AWS SAM CLI also provides an option to build your deployment package inside a Docker image that is compatible with the Lambda execution environment. See Building applications with dependencies in the AWS SAM Developer Guide for instructions.

Prerequisites

These instructions assume that you already have a function. If you haven't created a function yet, see Building Lambda functions with Python.

To follow the procedures in this guide, you will need a command line terminal or shell to run commands. Commands are shown in listings preceded by a prompt symbol ($) and the name of the current directory, when appropriate:

~/lambda-project$ this is a command this is output

For long commands, an escape character ( \ ) is used to split a command over multiple lines.

On Linux and macOS, use your preferred shell and package manager. On Windows 10, you can install the Windows Subsystem for Linux to get a Windows-integrated version of Ubuntu and Bash.

Updating a function with no dependencies

To create or update a function with the Lambda API, create an archive that contains your function code and upload it with the AWS CLI.

To update a Python function with no dependencies Create a ZIP archive. ~/my-function$ zip function.zip lambda_function.py adding: lambda_function.py (deflated 17%) Use the fileb:// prefix to upload the binary ZIP deployment package to Lambda and update the function code. ~/my-function$ aws lambda update-function-code --function-name my-function --zip-file fileb://function.zip { "FunctionName": "my-function", "FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:my-function", "Runtime": "python3.8", "Role": "arn:aws:iam::123456789012:role/lambda-role", "Handler": "lambda_function.lambda_handler", "CodeSize": 815, "CodeSha256": "GcZ05oeHoJi61VpQj7vCLPs8DwCXmX5sE/fE2IHsizc=", "Version": "$LATEST", "RevisionId": "d1e983e3-ca8e-434b-8dc1-7add83d72ebd", ... }

Updating a function with additional dependencies

If your function depends on libraries other than the SDK for Python (Boto3), install them to a local directory with pip , and include them in your deployment package.

Note For libraries that use extension modules written in C or C++, build your deployment package in an Amazon Linux environment. You can use the SAM CLI build command, which uses Docker, or build your deployment package on Amazon EC2 or AWS CodeBuild.

The following example shows how to create a deployment package that includes a common graphics library named Pillow.

To update a Python function with dependencies Install libraries in a new, project-local package directory with pip 's --target option. ~/my-function$ pip install --target ./package Pillow Collecting Pillow Using cached https://files.pythonhosted.org/packages/62/8c/230204b8e968f6db00c765624f51cfd1ecb6aea57b25ba00b240ee3fb0bd/Pillow-5.3.0-cp37-cp37m-manylinux1_x86_64.whl Installing collected packages: Pillow Successfully installed Pillow-5.3.0 Note In order for --target to work on Debian-based systems like Ubuntu, you may also need to pass the --system flag to prevent distutils errors. Create a ZIP archive of the dependencies. ~/my-function$ cd package ~/my-function/package$ zip -r9 $ { OLDPWD}/function.zip . adding: PIL/ (stored 0%) adding: PIL/.libs/ (stored 0%) adding: PIL/.libs/libfreetype-7ce95de6.so.6.16.1 (deflated 65%) adding: PIL/.libs/libjpeg-3fe7dfc0.so.9.3.0 (deflated 72%) adding: PIL/.libs/liblcms2-a6801db4.so.2.0.8 (deflated 67%) ... Add your function code to the archive. ~/my-function/package$ cd $OLDPWD ~/my-function$ zip -g function.zip lambda_function.py adding: lambda_function.py (deflated 56%) Use the fileb:// prefix to upload the binary ZIP deployment package to Lambda and update the function code. ~/my-function$ aws lambda update-function-code --function-name my-function --zip-file fileb://function.zip { "FunctionName": "my-function", "FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:my-function", "Runtime": "python3.8", "Role": "arn:aws:iam::123456789012:role/lambda-role", "Handler": "lambda_function.lambda_handler", "CodeSize": 2269409, "CodeSha256": "GcZ05oeHoJi61VpQj7vCLPs8DwCXmX5sE/fE2IHsizc=", "Version": "$LATEST", "RevisionId": "a9c05ffd-8ad6-4d22-b6cd-d34a00c1702c", ... }

With a virtual environment

In some cases, you may need to use a virtual environment to install dependencies for your function. This can occur if your function or its dependencies have dependencies on native libraries, or if you used Homebrew to install Python.