Python: AWS Lambda Layers
How to use AWS Lambda layers with Python

I am an AWS Certified Solutions Architect with over 12+ years of experience with a primary focus on AWS serverless solutions, AWS CDK, event driven architecture and Python.
Search for a command to run...
How to use AWS Lambda layers with Python

I am an AWS Certified Solutions Architect with over 12+ years of experience with a primary focus on AWS serverless solutions, AWS CDK, event driven architecture and Python.
No comments yet. Be the first to comment.
Fix error 1227 (42000): Access denied; you need (at least one of) the SUPER or SYSTEM_VARIABLES_ADMIN privilege(s) for this operation

How to resolve the error: Invalid stage identifier specified (Service: ApiGateway, Status Code: 400)

A walkthrough for a .NETCore serverless project

A beginner's guide

Easily adopt best practices such as tracing, structured logging, custom metrics, and more.

Modular code is often encouraged and considered a good practice. In Python, modules are just a .py file containing Python statements and are called upon via import.
In the below example, the "my_module" file is in the same directory as the working .py file "hello_lambda_layer".

To refer to the my_module we simply use the "from" statement.

When you prep the files for AWS lambda deployment, you can include the my_module file in the zip and the code will continue to work as expected, but it's not recommended. For example, if the module is being used in multiple lambda functions, the module .py file will need to be included in all those zip files and any change in the module will require code redeploy for all the linked lambda functions.
Lambda Layers overcome this issue and provide a centralised location to promote code sharing and separation of responsibilities so that you can iterate faster on writing business logic.
A Lambda layer is a .zip file archive that can contain additional code or data. A layer can contain libraries, a custom runtime, data, or configuration files. The whole process consists of 3 main stages:
Creating a zip for the Python lambda layer is quite simple and involve below steps:
Now, this zip can be uploaded as a Lambda layer.
Next, let's set up a Lambda layer in the AWS console.
NOTE: Make sure you are working in the correct region. The lambda layers are region-specific that means both lambda functions and layers need to be in the same region.
Via Cloudformation layers can be created using the "Type" of AWS::Serverless::LayerVersion. An example is shared below:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: A Lambda application that calls the Lambda API.
Resources:
function:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: python3.9
CodeUri: function/.
Description: Call the Lambda API
Timeout: 10
# Function's execution role
Policies:
- AWSLambdaBasicExecutionRole
- AWSLambda_ReadOnlyAccess
- AWSXrayWriteOnlyAccess
Tracing: Active
Layers:
- !Ref libs
libs:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: my-project-lib
Description: Dependencies for the sample app.
Content:
S3Bucket: my-bucket-region-123456789012
S3Key: layer.zip
CompatibleRuntimes:
- python3.9
- python3.8
If you are testing or using the AWS console, once the lambda is created, on the details page, "Layers" is listed as the last section.

Click add layer and on the next screen, select "Custom Layer" and select the required layer.

I hope you've found this article useful. Follow me if you are interested in: