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

Deploy Python Lambda functions with .zip file archives

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 files.

You can use a built-in ZIP archive utility, or any other ZIP utility (such as 7zip) for your command line tool to create a deployment package.

  • The .zip file must contain 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 do not need to include the libraries in your .zip file. These libraries are included with our supported Lambda runtime environments.

  • The .zip file must be less than 50 MB. If your .zip file is larger than 50 MB, we recommend uploading it to an Amazon S3 bucket. For more information, see Using other AWS services to build a deployment package.

  • The .zip file can not contain libraries written in C or C++. If your .zip file contains C-extension libraries, such as the Pillow (PIL) or numpy libraries, we recommend using the AWS SAM CLI to build a deployment package. For more information, see Using other AWS services to build a deployment package.

This page 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). To upload your .zip file on the Lambda console, see Deployment packages.

Prerequisites

The following steps assume that you have created a Lambda function, and are updating the deployment package for your function. If you haven't created a function yet, see Building Lambda functions with Python.

The AWS Command Line Interface (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 need the following:

Updating a function with no dependencies

The following steps show how to create a deployment package that contains only your function code, and upload it to Lambda using the AWS CLI.

To update a Python function with no dependencies

  1. Add function code files to the root of your deployment package.

    ~/my-function$ zip my-deployment-package.zip lambda_function.py
  2. Use the update-function-code command with the fileb:// prefix to upload the binary .zip file to Lambda and update the function code.

    ~/my-function$ aws lambda update-function-code --function-name MyLambdaFunction --zip-file fileb://my-deployment-package.zip { "FunctionName": "mylambdafunction", "FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:mylambdafunction", "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", ... }

The Lambda function in the last step uses a function handler of lambda_function.lambda_handler. For more information about function handler naming conventions, see AWS Lambda function handler in Python.

Updating a function with additional dependencies

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

The following steps show how to install the requests libary, create a deployment package, and upload it to Lambda using the AWS CLI. The steps assume that you are not using a virtual environment. It also assumes that your function code uses Python 3.8 and the python3.8 Lambda runtime.

Note

If you are creating a deployment package used in a layer, see Include library dependencies in a layer.

To update a Python function with dependencies

  1. Install libraries in a package directory with pip's --target option.

    ~/my-function$ pip install --target ./package requests
    Note

    To prevent distutils errors on Debian-based systems such as Ubuntu, you may need to pass the --system option.

  2. Navigate to the package directory.

    cd package
  3. Create a deployment package with the installed libraries at the root.

    ~/my-function$ zip -r ../my-deployment-package.zip .

    The last command saves the deployment package to the root of the my-function directory.

  4. Navigate back to the my-function directory.

    cd ..
  5. Add function code files to the root of your deployment package.

    ~/my-function$ zip -g my-deployment-package.zip lambda_function.py

    After you complete this step, you should have the following directory structure:

    my-deployment-package.zip$ │ lambda_function.py │ __pycache__ │ certifi/ │ certifi-2020.6.20.dist-info/ │ chardet/ │ chardet-3.0.4.dist-info/ ...
  6. Use the update-function-code command with the fileb:// prefix to upload the binary .zip file to Lambda and update the function code.

    ~/my-function$ aws lambda update-function-code --function-name MyLambdaFunction --zip-file fileb://my-deployment-package.zip { "FunctionName": "mylambdafunction", "FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:mylambdafunction", "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", ... }

The Lambda function in the last step uses a function handler of lambda_function.lambda_handler. For more information about function handler naming conventions, see AWS Lambda function handler in Python.

Using a virtual environment

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

The following steps show how to install the requests libary, create a deployment package, and upload it to Lambda using the AWS CLI. The steps assume that you are using the virtualenv module for a virtual environment. It also assumes that your function code uses Python 3.8 and the python3.8 Lambda runtime.

Note

If you are creating a deployment package used in a layer, see Include library dependencies in a layer.

To update a Python function with a virtual environment

  1. Create a virtual environment.

    ~/my-function$ virtualenv myvenv
    Note

    The virtualenv module uses Python 2.7 by default. You may need to add a local export path to your command line profile, such as export VIRTUALENV_PYTHON=/usr/bin/python3.8 when using the virtualenv module with Python 3 and pip 3.

  2. Activate the environment.

    ~/my-function$ source myvenv/bin/activate
  3. Install libraries with pip.

    (myvenv) ~/my-function$ pip install requests
  4. Deactivate the virtual environment.

    (myvenv) ~/my-function$ deactivate
  5. Create a deployment package with the installed libraries at the root.

    ~/my-function$ cd myvenv/lib/python3.8/site-packages ~/my-function/myvenv/lib/python3.8/site-packages$ zip -r ../../../../my-deployment-package.zip .

    The last command saves the deployment package to the root of the my-function directory.

    Tip

    A library may appear in site-packages or dist-packages and the first folder lib or lib64. You can use the pip show command to locate a specific package.

  6. Add function code files to the root of your deployment package.

    ~/my-function/myvenv/lib/python3.8/site-packages$ cd ../../../../ ~/my-function$ zip -g my-deployment-package.zip lambda_function.py

    After you complete this step, you should have the following directory structure:

    my-deployment-package.zip$ │ lambda_function.py │ __pycache__ │ certifi/ │ certifi-2020.6.20.dist-info/ │ chardet/ │ chardet-3.0.4.dist-info/ ...
  7. Use the update-function-code command with the fileb:// prefix to upload the binary .zip file to Lambda and update the function code.

    ~/my-function$ aws lambda update-function-code --function-name MyLambdaFunction --zip-file fileb://my-deployment-package.zip { "FunctionName": "mylambdafunction", "FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:mylambdafunction", "Runtime": "python3.8", "Role": "arn:aws:iam::123456789012:role/lambda-role", "Handler": "lambda_function.lambda_handler", "CodeSize": 5912988, "CodeSha256": "A2P0NUWq1J+LtSbkuP8tm9uNYqs1TAa3M76ptmZCw5g=", "Version": "$LATEST", "RevisionId": "5afdc7dc-2fcb-4ca8-8f24-947939ca707f", ... }

The Lambda function in the last step uses a function handler of lambda_function.lambda_handler. For more information about function handler naming conventions, see AWS Lambda function handler in Python.