0
0
AWScloud~7 mins

Lambda layers for shared code in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you have code or libraries that many AWS Lambda functions need, copying them into each function is slow and wasteful. Lambda layers let you put shared code in one place and use it in many functions easily.
When multiple Lambda functions use the same helper code or libraries.
When you want to update shared code without changing each Lambda function separately.
When you want to keep your Lambda function packages small and focused.
When you want to share common runtime dependencies across functions.
When you want to organize your code better by separating shared parts.
Config File - layer.zip
layer.zip
This is a zip file containing your shared code or libraries structured properly for Lambda layers. For example, for Python, it should have a folder named 'python' with your modules inside. This zip file is uploaded to AWS Lambda as a layer.

The zip file contains the shared code organized in a folder structure that Lambda expects. For Python, the 'python' folder is where your .py files or packages go. This lets Lambda add the layer's code to the function's runtime environment automatically.

Commands
This command uploads your zip file as a new Lambda layer version named 'my-shared-layer'. It tells Lambda this layer works with Python 3.9 runtime.
Terminal
aws lambda publish-layer-version --layer-name my-shared-layer --description "Shared utilities" --zip-file fileb://layer.zip --compatible-runtimes python3.9
Expected OutputExpected
{ "LayerVersionArn": "arn:aws:lambda:us-east-1:123456789012:layer:my-shared-layer:1", "Version": 1, "Description": "Shared utilities", "CreatedDate": "2024-06-01T12:00:00.000+0000", "CompatibleRuntimes": [ "python3.9" ] }
--layer-name - Names your Lambda layer.
--zip-file - Specifies the zip file with your shared code.
--compatible-runtimes - Defines which Lambda runtimes can use this layer.
This command attaches the published layer version to your existing Lambda function named 'my-function'. This makes the shared code available to that function.
Terminal
aws lambda update-function-configuration --function-name my-function --layers arn:aws:lambda:us-east-1:123456789012:layer:my-shared-layer:1
Expected OutputExpected
{ "FunctionName": "my-function", "Layers": [ { "Arn": "arn:aws:lambda:us-east-1:123456789012:layer:my-shared-layer:1", "CodeSize": 12345 } ] }
--function-name - Specifies which Lambda function to update.
--layers - Lists the layer ARNs to attach to the function.
This command checks the current configuration of your Lambda function to confirm the layer is attached.
Terminal
aws lambda get-function-configuration --function-name my-function
Expected OutputExpected
{ "FunctionName": "my-function", "Layers": [ { "Arn": "arn:aws:lambda:us-east-1:123456789012:layer:my-shared-layer:1", "CodeSize": 12345 } ], "Runtime": "python3.9" }
--function-name - Specifies which Lambda function to inspect.
Key Concept

If you remember nothing else from this pattern, remember: Lambda layers let you share code once and use it in many functions without copying it each time.

Common Mistakes
Uploading the zip file without the correct folder structure inside (like missing the 'python' folder for Python code).
Lambda won't find your shared code because it expects a specific folder layout inside the layer zip.
Make sure your zip file has the correct folder structure, for example, a 'python' folder containing your Python modules.
Not specifying compatible runtimes when publishing the layer.
Your layer might not be usable by your Lambda function if the runtime is not listed as compatible.
Always include the --compatible-runtimes flag with the runtimes your layer supports.
Forgetting to update the Lambda function configuration to attach the new layer version.
Your function won't use the shared code until the layer is attached.
Run the update-function-configuration command with the layer ARN to attach the layer.
Summary
Create a zip file with your shared code organized in the correct folder structure.
Publish the zip as a Lambda layer with compatible runtimes specified.
Attach the published layer version to your Lambda function using its ARN.
Verify the function configuration to confirm the layer is attached.