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.
Sections
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 commandthis 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 function.py -
Use the
update-function-codecommand to upload the package.~/my-function$aws lambda update-function-code --function-name python37 --zip-file fileb://function.zip{ "FunctionName": "python37", "FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:python37", "Runtime": "python3.7", "Role": "arn:aws:iam::123456789012:role/lambda-role", "Handler": "function.handler", "CodeSize": 815, "Description": "", "Timeout": 3, "MemorySize": 128, "LastModified": "2018-11-20T20:41:16.647+0000", "CodeSha256": "GcZ05oeHoJi61VpQj7vCLPs8DwCXmX5sE/fE2IHsizc=", "Version": "$LATEST", "VpcConfig": { "SubnetIds": [], "SecurityGroupIds": [], "VpcId": "" }, "TracingConfig": { "Mode": "Active" }, "RevisionId": "d1e983e3-ca8e-434b-8dc1-7add83d72ebd" }
Updating a Function with Additional Dependencies
If your function depends on libraries other than the SDK for Python (Boto 3), install them to a local directory with pip, and include them in your deployment package.
To update a Python function with dependencies
-
Install libraries in a new, project-local
packagedirectory withpip's--targetoption.~/my-function$pip install --target ./package PillowCollecting 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.0Note
In order for
--targetto work on Debian-based systems like Ubuntu, you may also need to pass the--systemflag to preventdistutilserrors. -
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 function.pyadding: function.py (deflated 56%) -
Update the function code.
~/my-function$aws lambda update-function-code --function-name python37 --zip-file fileb://function.zip{ "FunctionName": "python37", "FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:python37", "Runtime": "python3.7", "Role": "arn:aws:iam::123456789012:role/lambda-role", "Handler": "function.handler", "CodeSize": 2269409, "Description": "", "Timeout": 3, "MemorySize": 128, "LastModified": "2018-11-20T20:51:35.871+0000", "CodeSha256": "GcZ05oeHoJi61VpQj7vCLPs8DwCXmX5sE/fE2IHsizc=", "Version": "$LATEST", "VpcConfig": { "SubnetIds": [], "SecurityGroupIds": [], "VpcId": "" }, "TracingConfig": { "Mode": "Active" }, "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.
To update a Python function with a virtual environment
-
Create a virtual environment.
~/my-function$virtualenv v-envUsing base prefix '~/.local/python-3.7.0' New python executable in v-env/bin/python3.7 Also creating executable in v-env/bin/python Installing setuptools, pip, wheel... done.Note
For Python 3.3 and newer, you can use the built-in venv module to create a virtual environment, instead of installing
virtualenv.~/my-function$python3 -m venv v-env -
Activate the environment.
~/my-function$source v-env/bin/activate(v-env) ~/my-function$ -
Install libraries with pip.
(v-env) ~/my-function$pip install PillowCollecting 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 -
Deactivate the virtual environment.
(v-env) ~/my-function$deactivate -
Create a ZIP archive with the contents of the library.
~/my-function$cd v-env/lib/python3.7/site-packages~/my-function/v-env/lib/python3.7/site-packages$zip -r9 ${OLDPWD}/function.zip .adding: easy_install.py (deflated 17%) 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%) ...Depending on the library, dependencies may appear in either
site-packagesordist-packages, and the first folder in the virtual environment may beliborlib64. You can use thepip showcommand to locate a specific package. -
Add your function code to the archive.
~/my-function/v-env/lib/python3.7/site-packages$cd $OLDPWD~/my-function$zip -g function.zip function.pyadding: function.py (deflated 56%) -
Update the function code.
~/my-function$aws lambda update-function-code --function-name python37 --zip-file fileb://function.zip{ "FunctionName": "python37", "FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:python37", "Runtime": "python3.7", "Role": "arn:aws:iam::123456789012:role/lambda-role", "Handler": "function.handler", "CodeSize": 5912988, "Description": "", "Timeout": 3, "MemorySize": 128, "LastModified": "2018-11-20T21:08:26.326+0000", "CodeSha256": "A2P0NUWq1J+LtSbkuP8tm9uNYqs1TAa3M76ptmZCw5g=", "Version": "$LATEST", "VpcConfig": { "SubnetIds": [], "SecurityGroupIds": [], "VpcId": "" }, "TracingConfig": { "Mode": "Active" }, "RevisionId": "5afdc7dc-2fcb-4ca8-8f24-947939ca707f" }
