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:
- ZIP creation
- Lambda Layer creation
- Link layer with a lambda function
Creating a zip for the Python lambda layer is quite simple and involve below steps:
- Create a folder named "python".
- Copy the modules py file in the folder. (can contain multiple files)
- ZIP the "python" folder (created in step 1) as python.zip
Now, this zip can be uploaded as a Lambda layer.
Next, let's set up a Lambda layer in the AWS console.
Lambda Layer Creation
- Login to AWS console and open Lambda service page.
- Select Layer from the left menu.
- Click on "Create Layer".
- Provide relevant name, runtime and save.
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.
Link layer with a Lambda
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:
- AWS Architecture & Security.
- AWS Serverless solutions.