How do I resolve "unknown service", "parameter validation failed", and "object has no attribute" errors from a Python (Boto3) Lambda function?

4 minute read
1

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 any of the following errors:

  • unknown service
  • parameter validation failed
  • object has no attribute

These errors occur when the function tries to call 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 do this manually, or you can use Docker. It's more efficient to upgrade your Lambda layer through Docker, and this makes sure that your binaries are correct for the Lambda operating system.

Important: The following procedures assume that you have the latest version of Botocore (on GitHub). If you don't have the latest version of Botocore, then you must upgrade Botocore before you can upgrade to the latest Boto3 version. Implement the resolution as needed for your specific and Python configuration.

Resolution

Note: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, 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 pip3 and the latest AWS CLI version

1.    If you haven't already, then install pip3 for Python 3 packaging from the pip website. 

-or-

If you have a previous version of pip, then upgrade it from the pip website.

2.    Install or upgrade the AWS CLI using 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

(Prerequisites) Install Docker

Make sure that you installed Docker on your system and that it's running. To download the latest version of Docker, see Installing Docker.

Create and apply your layer

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: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. You can replace boto3-mylayer with a customized name for your package.

3.    Wait for the command to complete. When it completes successfully, you see a file with your package name in the current directory, such as boto3-mylayer.zip.

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

Important: The following AWS CLI commands work for Linux, Unix, and macOS operating systems. In each command, make sure that you replace boto3-mylayer with your preferred name for the lib folder and Lambda layer.

1.    Create a lib folder:

LIB_DIR=boto3-mylayer/python
mkdir -p $LIB_DIR

2.    Install the library to LIB_DIR:

pip3 install boto3 -t $LIB_DIR

3.    Zip all the dependencies to /tmp/boto3-mylayer.zip:

cd boto3-mylayer
zip -r /tmp/boto3-mylayer.zip .

4.    Publish the layer:

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 Lambda layer ARN

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:

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

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

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

Tip: 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

Lambda runtimes

Deploy Python Lambda functions with .zip file archives

AWS OFFICIAL
AWS OFFICIALUpdated a year ago