Simplifying Lambda Layer ARNs and Creating Custom Layers in AWS

Published: September 9, 2022

AWS Lambda Layers play a crucial role in simplifying the deployment process for Lambda functions. They provide an easy way to collect and manage Python packages as AWS Lambda (λ\lambda) Layers, streamlining the development workflow. In this article, we'll explore how to use pre-existing Lambda Layers and create custom layers from scratch using Flask as an example.

Keith's Layers for Easy Deployment

If you're looking for readily available Lambda Layers, keith's Layers is a fantastic resource. These layers make it incredibly convenient to deploy Python packages for AWS Lambda. You can quickly access a collection of Python Packages as AWS Lambda Layers, making the deployment process much more straightforward.

Creating your own Custom Layers

Creating a custom Lambda Layer tailored to your specific needs is a powerful approach. Let's walk through the four main steps to create a Lambda Layer with Flask:

Setting up the Local Directory for the Layer

To begin, create a local directory for the Lambda Layer. In this example, we'll use Flask. Execute the following commands:

mkdir flask-layer
cd flask-layer
mkdir -pv python/lib/python3.8/site-packages

This creates a directory named flask-layer with a subdirectory python/lib/python3.8/site-packages, where we'll install our Python packages.

Adding the Requirements File

Next, create a requirements.txt file in the root of the flask-layer folder, specifying the required packages. For instance, for Flask, the requirements.txt may contain:


The file structure should now look like this:

├── python
|   └── lib
|       └── python3.8
|           └── site-packages
└── requirements.txt

Installing Requirements with Docker

To install the requirements in the local directory using Docker, run the following command in powershell when you are in root directory

docker run -v ${pwd}:/var/task "amazon/aws-sam-cli-build-image-python3.8" /bin/sh -c "pip install -r requirements.txt -t python/lib/python3.8/site-packages; exit"

This command populates the site-packages directory you created earlier with all the necessary dependencies.

Uploading Your Custom Layer to AWS

AWS requires all the layer code to be in a zip archive, so we need to zip everything in the python directory:

zip -r python

Now the layer can be uploaded to AWS using the AWS CLI. You need to provide a few parameters in this step:

  • layer-name is the name your want to give your layer
  • description to briefly summarize the layer
  • zip-file is the path to the zip archive you created in the previous step
  • compatible-runtimes details the Python versions your layer is compatible with

Use the following terminal command to publish your custom layer to AWS:

aws lambda publish-layer-version \
    --layer-name "flask-layer" \
    --description "Lambda Layer for Flask 1.1.1" \
    --zip-file "fileb://" \
    --compatible-runtimes "python3.8"

With these steps, you can create a custom Lambda Layer tailored to your application's needs, enhancing the efficiency and maintainability of your AWS Lambda functions.