My Python (Boto3) AWS Lambda function returns "unknown service", "parameter validation failed", or "object has no attribute" errors.
Short description
A Python (Boto3) Lambda function that doesn't use the latest version of Boto3 might return the following errors:
- unknown service
- parameter validation failed
- object has no attribute
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. You can create a Lambda layer either manually, or with Docker. It's a best practice to upgrade your Lambda layer through Docker to make sure that your binaries are correct for the Lambda operating system.
Important: The following procedures assume that you have the latest version of Botocore. 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). However, 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.
Create and apply your layer
Complete the following steps:
-
Navigate to the directory where you want to create the layer file.
-
Run the following command in your system's CLI:
docker run --entrypoint "" -v "$PWD":/var/task "public.ecr.aws/lambda/python:3.9.2023.03.21.20" /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:3.9.2023.03.21.20 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.
-
To create or update your Lambda layer, run the following 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:
-
Create a lib folder:
LIB_DIR=boto3-mylayer/pythonmkdir
-
Install the library in LIB_DIR.
pip3 install boto3 -t $LIB_DIR
-
To zip all the dependencies to /tmp/boto3-mylayer.zip run the following command.
cd boto3-mylayer
zip -r /tmp/boto3-mylayer.zip
-
To publish the layer, run the following command.
aws lambda publish-layer-version --layer-name boto3-mylayer --zip-file fileb:///tmp/boto3-mylayer.zip
The 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 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.
Related information
Building Lambda functions with Python
Working with .zip file archives for Python Lambda functions