AWS announced two new AWS Lambda features, Lambda Runtime API and Lambda Layers, on Nov 29, 2018. These features enables developers to build common code, manage it and share it between multiple functions.
A Layer is a zip archive which contains libraries and dependencies which can be imported to Lambda functions at runtime. They also support versioning which makes it possible to upgrade the Lambda layer with new code without impacting the Lambda functions using it. Lambda Layers provide two major benefits as below…
- If there are more than one Lambda function have same dependencies, those dependencies can be uploaded as a Layer and used across Lambda functions.
- The deployment package size of the Lambda package is reduced as the dependencies are no more required to be part of the deployment package.
As part of this article, we will learn how to create a Lambda layer and how to use it in a Lambda fuction. We will be using Python runtime for creating Layer and Lambda function. We will be performing following tasks.
- Create a Lambda Layer with Python runtime
- Create an IAM policy and role which will grant needed permissions to Lambda Function for accessing Lambda Layers
- Create a Lambda function with Python runtime which will use the Lambda Layer
- Test the Lambda function
All the examples, code snippets, commands etc are demonstrated with respect to Mac OS. So let’s start.
Create a new directory with name python and create a new file “helloworld.py” under it. Open the file and write following code to it and save the file.
To check if the above code works fine or not, navigate to the above directory in terminal and type following commad.
This should print following output
Create zip file “python_lib.zip” by zipping python folder.
zip -r python_lib.zip ./python
Important point to note here : All the library files which needs to be part of the layer should be put under a folder name “python” and that folder only needs to be zipped.
After this all the activities will be done on AWS Console. So let’s LogOn to AWS Console and Navigate to Lambda Console. Click Layers from the left pane and click Create layer.
Provide appropriate values for Name and Description of the Layer, select Python3.6 as runtime for the Layer.
Click Upload and select python_lib.zip file created earlier. Click Create.
This would create a Lambda layer supporting selected runtime and it is given Version 1. We will talk about versioning of Lambda Layers later.
For the Lambda function to be able to use the Layers, it requires a permission to do so. A new IAM role needs to be creates with appropriate policies and that role should be associated with the Lambda function when it is created.
Let’s create a new policy which would allow Lambda Function to access the Lambda Layers and to write events to the CloudWatch logs. Then we will create a new IAM role and associcate this policy with the role.
Naviagate to IAM on AWS Console, select Policies from the left side and the click Create policy button from the right pane. Click JSON tab on Create policy screen and enter following JSON data in the input box.
Click Review policy, provide appropriate name and description for the policy and click Create Policy. This will create the policy and navigate to the list of all the policies.
Click Roles from the left side of the page and click Create role from the right side of the page. Select Lambda from the services listed under Choose the service that will use this role and click Next: Permissions.
Filter the policy with the name of the new policy created above and select the policy from search result. (Click on the refresh button on the right if the new policy does not appear in search result).
Click Next: Tags and then click Next: Review. Provide approprite name and description for the role and click Create role. This would create new IAM role. Remember the role name as this will be used to select the role for the Lambda function when we will create it in next step.
Now let’s create a Lambda function which will use this layer.
Select Functions from the left menu and click on Create Function. Provide appropriate name for the function, select runtime and select previously created role from Existing role dropdown.
Click Create function. Click Layers below the name of the lambda function. This would list all the layers being used by the Lambda function below. Click Add layer button.
Select Lambda Layer from the dropdown and select version and click Add.
Select the Function name, and scroll down to see the python code editor. Enter following code in the code editor and click Save.
Let’s verify if the Lambda function works properly and prints “hello world” using “helloworld” module from the Layer.
Configure test event if already not configured, select the test event and click Test. If everything goes fine, following output should be generated below the python code window. Notice “hello world” being displayed in the output. “hello world” is prited in the output by the “helloworld” module which is part of the Lambda layer.
That’s the simple example of using Lambda Layer in a Lambda function with Python runtime.