Python: AWS Lambda Layers

Python: AWS Lambda Layers

How to use AWS Lambda layers with Python

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". Screenshot 2021-10-09 at 09.05.01.png

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

Screenshot 2021-10-09 at 09.00.39.png

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:

  1. ZIP creation
  2. Lambda Layer creation
  3. Link layer with a lambda function

ZIP Creation

Creating a zip for the Python lambda layer is quite simple and involve below steps:

  1. Create a folder named "python".
  2. Copy the modules py file in the folder. (can contain multiple files)
  3. 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

  1. Login to AWS console and open Lambda service page.
  2. Select Layer from the left menu.
  3. Click on "Create Layer".
  4. 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.

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.

Screenshot 2021-12-13 at 19.34.26.png

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

Screenshot 2021-12-13 at 19.31.51.png

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

  1. Python
  2. AWS Architecture & Security.
  3. AWS Serverless solutions.