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

Last updated: 2021-11-29

My Python (Boto 3) AWS Lambda function returns "unknown service", "parameter validation failed", or "object has no attribute" errors. Why is my function returning these errors, and how do I resolve the issue?

Short description

A Python (Boto 3) Lambda function that isn't using the latest version of Boto 3 can 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 Boto 3.

To resolve the issue, create a Lambda layer that uses the latest version of Boto 3. Then, add the layer to your function's configuration.

Important: The following procedures assumes that you're using the latest version of Botocore. If you're not using the latest version of Botocore, you must upgrade Botocore before you can upgrade to the latest Boto 3 version. Adjust the steps that you take as needed for your specific operating system 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 3.8 is 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. -or- If you have a previous version of pip, then upgrade it.

2.    Install or upgrade the AWS CLI using pip3. Note: The latest version of the AWS CLI includes the Lambda Layers API model.

Create a Lambda layer that uses the latest Boto 3 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 by running the following command:

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

2.    Install the library to LIB_DIR by running the following command:

pip3 install boto3 -t $LIB_DIR

3.    Zip all the dependencies to /tmp/boto3-mylayer.zip by running the following command:

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

4.    Publish the layer by running 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 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 myfunction with your function's name. Replace <layer ARN> with your layer's ARN.

aws lambda update-function-configuration --function-name myfunction --layers <layer ARN>

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

Tip: Use print(boto3.__version__) and print(botocore.__version__) in your function code to confirm the version of Boto 3 and Botocore.