I’m encountering an issue with my AWS CDK (v2) Python deployment when attaching a Lambda layer. I have a zipped Lambda layer file structured as follows: python/lib/python3.11/site-packages/. When I upload this zip file manually through the AWS Management Console, my Lambda functions work perfectly fine.
However, when I attempt to attach the same zipped Lambda layer using AWS CDK in Python, I encounter an import error within the Lambda functions that utilize this layer.
Here’s part of my CDK setup for the Lambda layer:
layer_zip_path = "path/to/my/layer.zip"
my_layer = _lambda.LayerVersion(
self, "MyLayer",
code=_lambda.Code.from_asset(layer_zip_path),
compatible_runtimes=[_lambda.Runtime.PYTHON_3_11]
)
I’ve checked the zip structure, ensured that necessary dependencies are included within site-packages, and confirmed that the runtime specified in CDK matches my Lambda functions’ runtime. However, the import error persists when using the CDK-deployed Lambda layer.
Could someone provide insights or suggestions on what might be causing this discrepancy between manual upload and CDK deployment that leads to the import error?
Any help or guidance on troubleshooting this issue would be greatly appreciated! Thank you.
2
Answers
The problem is solved... I didn't provide the path to the actual zip file, only to the directory containing the zip file. That's why my layer didn't work.
Move the shared zip file in the path of your lambda cdk code and try something like this.