Important Note: You will create AWS resources during the exercise which will incur cost in your AWS account. It is recommended to clean-up the resources as soon as you finish the exercise to minimize the cost.

Quick Hands-on with Lambda Layer

AWS Lambda Layer works as common library which can be used by more than one Lambda functions. A layer is a ZIP archive that contains common libraries, a custom runtime, or other dependencies. Layers also help in keeping the deployment package small by keeping common libraries out of the Lambda function deployment.

In this exercise, you will configure and use a Lambda Layer with the Lambda function.

The AWS Resource consumption for the exercise falls under AWS Free Tier.

Step1: Pre-Requisite

You need to have an AWS account with administrative access to complete the exercise. If you don’t have an AWS account, kindly use the link to create free trial account for AWS.

Step2: Configure Lambda Layer

Let’s start with the configuration of the Lambda Layer. The exercise provides you code for the layer but you are free to build your own. The code provided by AWS Dojo can be downloaded from the link. The code zip file is simple. It has a python folder. The python folder has and files. These python files contain the common functions which can be shared / used by the Lambda functions. The following are the codes in both and files.

def commonfunction1(input):                           
    return "common function1 called with input - " + input

def commonfunction2(input):                           
    return "common function2 called with input - " + input

You can see the code is simple. They have functions doing simple jobs. Please feel free to make your own code and package in the zip file for the exercise.

Let’s create Lambda Layer using this code.

  1. Login to the AWS Console and select a region of your choice. The exercise will use the Ireland region.

  2. Goto the Lambda Console. Click on the Layers menu in the left and then click on the Create layer button.


  3. On the next screen, type in dojolayer as the name. Select Upload a .zip file as the option and upload the file provided in the exercise. Select Python 3.8 as the runtime. Finally click on the Create button.


  4. The layer is deployed in no time with version 1. The next step is to create a Lambda function which uses this layer.

Step3: Create Lambda Function

In this step, you create a Lambda function which uses the layer configured earlier.

  1. In the Lambda Console, click on the Functions menu in the left and then click on the Create function button.


  2. On the next screen, select Author from scratch as the option. Type in dojolambdafunction as the name. Select Python 3.8 as the runtime. Select Create a new role with basic Lambda permissions as the option for the permissions. Finally click on the Create function button.


  3. The function is created in no time. Click on the Layers link to add the layer.


  4. It will open Layers section. Click on the Add a layer button.


  5. On the next screen, select Custom layers as the option. Select dojolayer as the custom layer and select 1 for the version. Click on the Add button.


  6. The layer gets added to the Lambda function. Next, you goto the Function code area and update the code with the code provided below.


    import json
    import commonlib1 as clib1
    import commonlib2 as clib2
    def lambda_handler(event, context):
        resp1 = clib1.commonfunction1(event)
        resp2 = clib2.commonfunction2(event)
        return {'statusCode': 200, 'body': json.dumps(resp1 + " - " + resp2)}


  7. In the code above, you first import libraries from the layer - commonlib1 & commonlib2. You then call functions commonfunction1 and commonfunction2 in the respective libraries. Finally the code returns the response of the each function call.

  8. Click on the Save button to save and upload the lambda function code.


  9. The Lambda function is ready. It has code to call libraries in the layer. Time to test.

Step4: Test Lambda Function

Time to test the lambda function you created.

  1. In the Lambda Console, Keep the function details open and click on the Test button.


  2. On the configure test event popup, select Create new test event option. Type in dojotest as the event name. Type in “test data” as the test data. Click on the Create button.


  3. The test configuration is created. Select dojotest and click on the Test button.


  4. The Lambda function is executed with the test data. You can see the output where libraries functions are called and their responses are returned.


  5. This finished the exercise. You can create complex, reusable libraries with the Lambda Layer. This was a quick and small exercise to understand the working of the layer. The next step is to clean-up the resources so that you don’t incur any cost post this exercise.

Step5: Clean up

Delete dojolambdafunction Lambda Function.

Delete dojolayer Lambda Layer.

Thanks and hope you enjoyed the exercise.

Back to the Exercises