10
Create a Python Layer for your AWS Lambda Function
I just finished creating a few small lambda functions using Python.
Layers are a great way to manage application dependencies, and speeding up your deployment times.
My project needed the following libraries:
If you need a different library, the approach should be the same, assuming it's installable using pip
or pip3
.
Unfortunately we didn’t have access to Container Registry (ECR) so we couldn't create a docker container, which would have allowed us to install the dependencies as part of the docker container creation.
We instead had to create & upload a zip file containing our application code and dependencies.
While we could include the application dependencies in the zip we upload, this has a few disadvantages:
- It slows down each upload, which is not ideal as you might end up uploading a number of times.
- Your application code will not be viewable in the lambda console if your zip file ends up too large.
So now we know why layers might be needed in your lambda function, let's look at how to create one.
The sample project is also available on GitHub.
Folder structure of a lambda layer:
/requirements.txt
/python/
Where requirements.txt
is a file with the names of the packages we’re using, and python
is an empty folder which we will be installing dependencies into. See the Lambda layer documentation for more information on why we need to install into the python folder.
My application folder on my computer looks something like this then:
/lambda_function.py
/lambda_layer/requirements.txt
/lambda_layer/python/
AWS Lambda runs in a Linux machine, so you'll need to install the layer dependencies using an Linux OS. You can't directly follow these steps in MacOS or Windows.
I won't cover how to create a Linux VM, as there are many good tutorials available with a quick search online.
Remember, you need to run these commands in a Linux machine.
Navigate into the layer folder:
cd lambda_layer
Install the dependencies into the python folder:
pip install -r requirements.txt -t .python
Create the lambda layer zip:
zip -r ../app-name-depts-layer.zip *
We now have a ZIP file (app-name-depts-layer.zip
) which can be uploaded to AWS as a Layer. This zip file should have a python folder inside it, with the application dependencies installed from pip
inside.
Fill in the details of your layer:
I recommend including the names of the packages that are included in the layer, as part of the description. This will be useful in the future, especially if you end up creating and using multiple layers.
Click Create layer
In your lambda function, scroll down to the bottom of the page, to the Layers section:
Click on the Add a layer button
That’s it!
Now in your application code, you can simply import the packages installed in the layer.
lambda_function.py
import requests
def lambda_handler(event, context):
test_url = 'https://jsonplaceholder.typicode.com/todos/1'
response = requests.get(test_url)
return {
'statusCode': response.status_code,
'body': response.json()
}
You now have the ability to make HTTP requests and can easily reuse the layer in different applications.
Hopefully this helps get you started, you can easily extend this out as needed...
10