Updating a Lambda function in Python 3.8 - AWS Lambda

Updating a Lambda function in Python 3.8

This tutorial guides you through the process of installing a Python library, creating a deployment package, and updating a Lambda function in Python 3.8 using the AWS Command Line Interface (AWS CLI).

The following steps assume that you have created a Lambda function and are updating the .zip file used as your deployment package. If you haven't created a function yet, see Tutorial: Creating a Lambda function in Python 3.8.

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:

Updating a function without runtime dependencies

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.

For more information, see What is a runtime dependency?.

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 without runtime 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 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 this step uses a function handler of lambda_function.lambda_handler. For more information about function handler naming conventions, see Naming in AWS Lambda function handler in Python.

Updating a function with runtime dependencies

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.

For more information, see What is a runtime dependency?.

The following steps show how to install the requests library, 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 Including 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 this step uses a function handler of lambda_function.lambda_handler. For more information about function handler naming conventions, see Naming in AWS Lambda function handler in Python.

Using a virtual environment

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.

For more information, see What is a runtime dependency?.

The following steps show how to install the requests library, create a deployment package, and upload it to Lambda using the AWS CLI. 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 Including library dependencies in a layer.

To update a Python function with a virtual environment

  1. Activate the virtual environment. For example:

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

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

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

    ~/my-function$cd 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.

  5. 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/ ...
  6. Use 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 this step uses a function handler of lambda_function.lambda_handler. For more information about function handler naming conventions, see Naming in AWS Lambda function handler in Python.