Deploy Python Lambda functions with .zip file archives - AWS Lambda

Deploy Python Lambda functions with .zip file archives

Note

End of support for the Python 2.7 runtime starts on July 15, 2021. For more information, see Runtime support policy.

Your AWS Lambda function's code consists of scripts or compiled programs and their dependencies. You use a deployment package to deploy your function code to Lambda. Lambda supports two types of deployment packages: container images and .zip file archives.

To create the deployment package for a .zip file archive, you can use a built-in .zip file archive utility or any other .zip file utility (such as 7zip) for your command line tool. Note the following requirements for using a .zip file as your deployment package:

  • The .zip file contains your function's code and any dependencies used to run your function's code (if applicable) on Lambda. If your function depends only on standard libraries, or AWS SDK libraries, you don't need to include these libraries in your .zip file. These libraries are included with the supported Lambda runtime environments.

  • If the .zip file is larger than 50 MB, we recommend uploading it to your function from an Amazon Simple Storage Service (Amazon S3) bucket.

  • If your deployment package contains native libraries, you can build the deployment package with AWS Serverless Application Model (AWS SAM). You can use the AWS SAM CLI sam build command with the --use-container to create your deployment package. This option builds a deployment package inside a Docker image that is compatible with the Lambda execution environment.

    For more information, see sam build in the AWS Serverless Application Model Developer Guide.

  • Lambda uses POSIX file permissions, so you may need to set permissions for the deployment package folder before you create the .zip file archive.

This section describes how to create a .zip file as your deployment package, and then use the .zip file to deploy your function code to Lambda using the AWS Command Line Interface (AWS CLI).

Prerequisites

The AWS CLI is an open-source tool that enables you to interact with AWS services using commands in your command line shell. To complete the steps in this section, you must have the following:

What is a runtime dependency?

A deployment package is required to create or update a Lambda function with or without runtime dependencies. The deployment package acts as the source bundle to run your function's code and dependencies (if applicable) on Lambda.

A dependency can be any package, module or other assembly dependency that is not included with the Lambda runtime environment for your function's code.

The following example describes a Lambda function without runtime dependencies:

  • If your function's code is in Python 3.8, and it depends only on standard Python math and logging libraries, you don't need to include the libraries in your .zip file. These libraries are included with the python3.8 runtime.

  • If your function's code depends only on the AWS SDK for Python (Boto3), you don't need to include the boto3 library in your .zip file. These libraries are included with the python3.8 runtime.

For a complete list of AWS SDKs, see Tools to Build on AWS.