skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. Move the shared zip file in the path of your lambda cdk code and try something like this.

        code=aws_lambda.AssetCode(
            os.path.join(os.path.dirname(__file__), "shared.zip")
        )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search