AWS Lambda Python 3.12 – Runtime.ImportModuleError: cannot import name DEFAULT_CHECKSUM_ALGORITHM from botocore.httpchecksum when using aws-xray-sdk layer
I am encountering an import error when deploying an AWS Lambda function using the Python 3.12 runtime.
Error: Runtime.ImportModuleError: cannot import name 'DEFAULT_CHECKSUM_ALGORITHM' from 'botocore.httpchecksum' (/opt/python/botocore/httpchecksum.py).
Context: Environment: AWS Lambda Runtime: Python 3.12 The function uses a shared Lambda Layer This layer installs aws-xray-sdk Originally, the layer was using an older version of aws-xray-sdk, which installed versions of boto3 / botocore that did not include the DEFAULT_CHECKSUM_ALGORITHM attribute in botocore.httpchecksum, causing this error.
Layer update performed: The layer has been updated to: aws-xray-sdk==2.15.0
The associated dependencies (boto3 and botocore) were also updated to ensure that DEFAULT_CHECKSUM_ALGORITHM exists in botocore/httpchecksum.py.
Actions taken:
- Updated the Lambda Layer
- Installed aws-xray-sdk==2.15.0
- Updated related dependencies
- Verified the content of httpchecksum.py inside the layer
- Fully redeployed the Lambda function with the new layer
Issue: Despite these updates, the error persists exactly as before.
- It appears that either:
- AWS Lambda is still loading an older version of botocore, or
- There is a conflict between the botocore version bundled with the Lambda runtime and the one provided by the layer
Questions:
- Can AWS Lambda prioritize its internal botocore over the version provided by a Lambda Layer?
- Is there a known incompatibility between aws-xray-sdk==2.15.0 and Python 3.12 on AWS Lambda?
- What is the recommended best practice to ensure Lambda uses the botocore version provided by a layer?
- Topics
- ServerlessCompute
- Tags
- AWS Lambda
- Language
- English
asked 7 months ago277 views
- Newest
- Most votes
- Most comments
This issue appears to be related to a version conflict between the AWS SDK (boto3/botocore) included in the Python 3.12 Lambda runtime and the version being imported from your Lambda Layer.
AWS Lambda Python runtimes come with pre-installed versions of boto3 and botocore. When you add a layer that includes its own version of these libraries, you can encounter import conflicts like the one you're experiencing. The Lambda runtime's built-in libraries are typically loaded first, which can cause conflicts when your layer's code expects a different version.
The error "cannot import name 'DEFAULT_CHECKSUM_ALGORITHM' from 'botocore.httpchecksum'" suggests that the runtime is using a version of botocore that doesn't have this attribute, even though your layer includes a newer version that does.
To resolve this issue, you have a few options:
-
Package the required boto3/botocore versions directly with your function code rather than in the layer. This can help ensure your function uses the versions you specify.
-
Modify your import path to explicitly use the versions from your layer by manipulating the Python path at runtime.
-
Check the exact version of boto3/botocore in your Lambda environment by creating a simple function that prints the versions:
import boto3 import botocore def lambda_handler(event, context): print(f'boto3 version: {boto3.__version__}') print(f'botocore version: {botocore.__version__}')
- Consider using a container image for your Lambda function instead of layers if you need precise control over the environment and dependencies.
Architecture mismatches can also cause similar issues. Make sure that any compiled extensions in your layer are compatible with the Lambda runtime architecture.
For AWS X-Ray SDK specifically, you might need to ensure that all its dependencies are properly included and that there are no conflicts with the runtime's built-in libraries.
Sources
Building Lambda functions with Python - AWS Lambda
Troubleshooting - Powertools for AWS Lambda (Python)
answered 7 months ago
Relevant content
- Accepted Answer
asked a year ago
asked 2 years ago
