AWS Lambda: Packaging a Layer

AWS Lambda: Packaging a Layer

We know that AWS Lambda is a serverless, event-driven computing service. It enables us to run our code without provisioning the servers. A Lambda function is the resource or place where we upload/write our code and run.

We can write the code in the Lambda function console editor or develop it locally, zip it, and upload the .zip file to the function.

What

A Lambda layer is a separate .zip file archive that contains supplementary code or data. Lambda layers provide an easy way to package libraries that Lambda functions use. A layer can have data, libraries, custom runtime, or configuration files.

When a layer is added to a function, Lambda extracts the layer contents into the /opt directory in the function’s execution environment.

Why

We use Lambda layers for various reasons.

  • To reduce the size of the deployment package.

  • To separate core function logic from dependencies.

  • To share dependencies across multiple Lambda functions.

For example, you may need to send HTTP requests from a Python code. The widely used Python library for this requirement is the 'requests' library. However, the 'requests' library is unavailable by default in Python installation.

If you are developing your code on a local machine then you will install the 'requests' library using pip. This is not the case with AWS Lambda as we cannot access the underlying server and install the library. Packaging the library and adding it as a layer to our Lambda function make it work.

How to use layers

  • Package the layer content.

  • Create the layer in Lambda

  • Add the layer to the function.

Package the layer

Lambda functions run on Amazon Linux, so our layer content must be able to compile and build in a Linux environment.

  1. Create a directory requests-layer in the following directory structure on your local machine.

     mkdir -p requests-layer
     cd requests-layer
     mkdir -p python/lib/python3.10/site-packages
    

    Why: As mentioned, Lambda extracts the layer contents into the /opt directory in the function’s execution environment. For each Lambda runtime, the PATH variable already includes specific folder paths within the /opt directory. To ensure that the PATH variable picks up our layer content, our layer .zip file should have its dependencies in the folder paths as created above.

    Note: As my Lambda function's runtime is Python3.10, I'm building this package with Python3.10, hence the directory structure is created like that. You can have the structurepython/lib/python3.x/site-packages.

  2. Install the library inside the directory created.

     cd requests-layer
     python3 -m pip install requests -t python/lib/python3.10/site-packages/
    
  3. Zip the contents of the requests-layer directory.

     cd ..
     zip -r requests-layer.zip requests-layer
    
  4. The package is built.

Create the layer

  1. Navigate to the layers page on the AWS Lambda console and click "Create layer".

  2. On the layer creation page, give

    • A name for the layer.

    • Description.

    • Upload the above-built package via S3 or direct upload.

    • Choose a compatible architecture (optional). This depends on the architecture of the Lambda function.

    • Choose compatible runtimes (optional) that this layer can work with.

    • Layer's software license (optional).

  1. Create the layer.

Add the layer to the function

  1. Go to your function.

  2. Scroll down to see the "Layers" section at the bottom and click "Add a layer".

  3. On the add a layer page,

    • Choose a layer source. We can choose either custom layers or specify an ARN in our case, then select the layer from the dropdown or enter the ARN manually.

  4. Click "Add" to add the layer.

  5. Test the function and you shouldn't see any error.

This completes packaging a layer, creating it, and adding it to a function.

If you observed, a layer version was created. That's because a layer is immutable. You cannot modify the layer once it's made. However, you can re-upload a package to an existing layer, creating a new version.

Happy Learning!