How to create a lambda layer in AWS

Posted on Sun 10 May 2020

python aws lambda layers

In the last article I wrote that one of the options you have, when creating a lambda function in AWS that requires external modules, is to use a lambda layer that contains all these modules. That way, the lambda is independent of those modules, can be updated by itself, and also you can share that layer between lambdas that use the same modules, thus making it easier to maintain.

In this article, I will use the same example program from the last one, but instead of bundling everything together, I will create a lambda layer that can be used by the lambda.

Preparing the layer

First create a new folder for this project:

mkdir aws-lambda-layer
cd aws-lambda-layer

Next, create a folder structure for the modules that need to be installed.

mkdir -p lambda-layer/python/lib/python3.8/site-packages

Once the folder is created we can install requests in that folder.

pip3 install requests --target lambda-layer/python/lib/python3.8/site-packages

That folder structure is important because that is where Python expects to find the modules. Also as you can see, in this example I am using Python 3.8.

Now we can go into the lambda-layer folder and create a zip file for the layer that will be uploaded using the console.

cd lambda-layer
zip -r9 lambda-layer.zip .

Creating the layer

Log into the AWS console and go to Services -> Lambda -> Layers - Create layer - Name (ex: myRequestsLayer) - Upload - Select your zip file from before - Runtime (Python 3.8) - Create

Creating the lambda

We will be creating the lambda manually for this exercise, so in the AWS console go to Services -> Lambda - Create function - Author from scratch - Function name (ex: randomDadJokes) - Runtime (Python 3.8) - Create function

Replace the code in the editor with the following code:

import json
import requests

url = 'https://icanhazdadjoke.com'

def lambda_handler(event, context):
    r = requests.get(url, headers={"Accept": "application/json"})
    result = json.loads(r.text)
    return result['joke']

Hit Save.

Connecting the layer

Still on the lambda screen, in the Designer section, click on the Layers box. - Add a layer - Select from list of runtime compatible layers - Name (chose your layer) - Version 1 - Add

Creating the Test event

We also need to create a test event so we can trigger the lambda manually. You can do that by clicking on the dropdown (Select a test event) -> Configure test events. - Give it an Event Name (ex: Run) - Inputs don't matter so you can just leave it as is or delete those keys - Create

Run it

Now you can click on the Test button and you should see a random dad joke.

Thoughts

I really like the layers a lot better than having to bundle everything together, because it's not just easier to maintain, but also now I can play with the lambda and edit the code or debug it right there in the browser and I don't have to package it and re upload every time I make a change. This makes prototyping a lot easier and fun.

Note that other type of data can also be included in a layer so if you want to learn more, make sure you visit the AWS Lambda layers page.