VOOZH about

URL: https://repost.aws/knowledge-center/lambda-python-runtime-errors

⇱ Troubleshoot Python (Boto 3) Lambda function runtime errors | AWS re:Post


Skip to content

How do I resolve Python (Boto 3) Lambda function runtime errors?

4 minute read
2

My Python (Boto3) AWS Lambda function returns "unknown service", "parameter validation failed", or "object has no attribute" errors."

Short description

These errors occur when the function calls an AWS service or AWS API that requires the latest version of Boto3.

To resolve this issue, create a Lambda layer that uses the latest version of Boto3. Then, add the new layer to your Lambda function's configuration. You can create a Lambda layer either manually, or with Docker. It's a best practice to create or update your Lambda layer through Docker to make sure that your binaries are correct.

Important: If you don't have the latest version of Botocore, then you must upgrade Botocore before you can upgrade to the latest Boto3 version. For more information, see botocore on the GitHub website.

Resolution

Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.

It's a best practice to create a Lambda layer on the same operating system that your Lambda runtime is based on. For example, Python versions 3.8 and 3.9 are based on an Amazon Linux 2 Amazon Machine Image (AMI). Python 3.7 and Python 3.6 are based on the Amazon Linux AMI.

Prerequisites:

  • Install the pip3 for Python 3 package. Or, if you have a previous version of pip, then upgrade it. For more information, see Installation on the pip website.
  • Install or update the AWS CLI with pip3.

Note: The latest version of the AWS CLI includes the Lambda Layers API model.

Use Docker to create or update a Lambda layer that uses the latest Boto3 version

Prerequisite: Install Docker on your system. To download the latest version of Docker, see Installing Docker.

Complete the following steps:

  1. Navigate to the directory where you want to create the layer file.

  2. Run the following command in your system's CLI:

    docker run --entrypoint "" -v "$PWD":/var/task "public.ecr.aws/lambda/python:[IP_ADDRESS]" /bin/sh -c "mkdir -p /tmp/python && pip3 install boto3 -t /tmp/python && cd /tmp && yum install -y zip && zip -r /var/task/boto3-mylayer.zip ."

    Note: Replace public.ecr.aws/lambda/python:[IP_ADDRESS] with the base image for your version of Python. Replace boto3-mylayer with a customized name for your package. After the command completes successfully, you see a file with your package name in the current directory, such as boto3-mylayer.zip.

  3. To create or update your Lambda layer, run the following publish-layer-version command:

    aws lambda publish-layer-version --layer-name boto3-mylayer --zip-file fileb://boto3-mylayer.zip --compatible-runtimes python3.9 --region REGION_NAME

    Note: Replace boto3-mylayer with your package name. Replace REGION_NAME with your AWS Region. Also, include the compatible runtimes that you previously specified.

Manually create a Lambda layer that uses the latest Boto3 version

The following AWS CLI commands work for Linux, Unix, and macOS operating systems.

Note: In each command, make sure that you replace boto3-mylayer with your preferred name for the lib folder and Lambda layer.

Complete the following steps:

  1. Create a lib folder:

    LIB_DIR=boto3-mylayer/pythonmkdir
  2. Install the library in LIB_DIR:

    pip3 install boto3 -t $LIB_DIR
  3. To zip all the dependencies to /tmp/boto3-mylayer.zip run the following command:

    cd boto3-mylayer
    zip -r /tmp/boto3-mylayer.zip .
  4. To publish the layer, run the following publish-layer-version command:

    aws lambda publish-layer-version --layer-name boto3-mylayer --zip-file fileb:///tmp/boto3-mylayer.zip

    The preceding command returns the new layer's Amazon Resource Name (ARN).

    Example output:

    arn:aws:lambda:region:$ACC_ID:layer:boto3-mylayer:1

Add the new layer to your Lambda function's configuration

To add the new layer to your Lambda function's configuration, run the following update-function-configuration command:

aws lambda update-function-configuration --function-name MY_FUNCTION --layers LAYER_ARN

Note: Replace MY_FUNCTION with your function's name. Replace LAYER_ARN with your layer's ARN.

All AWS services and arguments are now available to your Lambda function.

To confirm the version of Boto3 and Botocore, use print(boto3.__version__) and print(botocore.__version__) in your function code. Import boto3 and botocore libraries before you use these commands.

Related information

Building Lambda functions with Python

Working with .zip file archives for Python Lambda functions

Language
English
👁 Image
AWS OFFICIALUpdated 4 months ago
3 Comments

In the first command, instead of "yum", "dnf" has to be used. I got errors while using "yum" as its not available within the latest version of the container image. I used an image "public.ecr.aws/lambda/python:3.12.2024.06.05.12-arm64"

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

MODERATOR

replied 2 years ago

This article was reviewed and updated on 2026-03-12.

EXPERT

replied 4 months ago