AWS Lambda Function with Layers

Lambda function with layer

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…

  1. If there are more than one Lambda function have same dependencies, those dependencies can be uploaded as a Layer and used across Lambda functions.
  2. 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.

  1. Create a Lambda Layer with Python runtime
  2. Create an IAM policy and role which will grant needed permissions to Lambda Function for accessing Lambda Layers
  3. Create a Lambda function with Python runtime which will use the Lambda Layer
  4. Test the Lambda function

All the examples, code snippets, commands etc are demonstrated with respect to Mac OS. So let’s start.

Create a Lambda Layer with Python runtime

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.

1
2
def printMessage():
print ("Hello world")

To check if the above code works fine or not, navigate to the above directory in terminal and type following commad.

1
python helloworld.py

This should print following output

1
Hello world

Create zip file “python_lib.zip” by zipping python folder.

1
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.

Lambda Layers homepage

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.

Create new Lambda Layer

This would create a Lambda layer supporting selected runtime and it is given Version 1. We will talk about versioning of Lambda Layers later.

Lambda Layer created

Create an IAM policy and role

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"lambda:GetLayerVersion",
"logs:PutLogEvents"
],
"Resource": [
"arn:aws:logs:*:*:log-group:*",
"arn:aws:lambda:*:*:layer:*:*"
]
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": "logs:PutLogEvents",
"Resource": "arn:aws:logs:*:*:log-group:*:*:*"
}
]
}

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.

Create new role for Lambda function

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).

Select policy

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.

Create a Lambda function

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.

Create Lambda function

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.

Lambda function layers

Select Lambda Layer from the dropdown and select version and click Add.

Lambda function add layer

Click Save.

Lambda function layer selected

Select the Function name, and scroll down to see the python code editor. Enter following code in the code editor and click Save.

1
2
3
4
5
6
7
8
9
10
import json
import helloworld as hw

def lambda_handler(event, context):
# TODO implement
hw.printMessage()
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}

Test the Lambda function

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.

Lambda function success run

That’s the simple example of using Lambda Layer in a Lambda function with Python runtime.