How to add Lambda layers using AWS CLI

Lambda layers on AWS Lambda is a helpful feature because it allows us to manage and distribute code dependencies across multiple Lambda functions. Lambda layers are essentially .zip file archives that contain libraries, custom runtimes, or other dependencies. Lambda layers help reduce the size of deployment packages, separate core function logic from dependencies, and share common components across different Lambda functions. Lambda functions can reference the layer once we create a layer with all the dependencies, libraries, and shared code.

CLI commands to create Lambda layers

Consider an example where we want to use pandas in multiple Lambda functions. You can use the following commands in the terminal widget below.

Step 1: Create a Lambda function

To create a lambda function, we need to create an IAM role with permission to execute the Lambda functions and create a ZIP file of the code. To create an IAM role, use the command given below:

aws iam create-role \
--role-name LambdaExecutionRole \
--assume-role-policy-document file://usercode/trust-policy.json

Successfully executing this command will give us an ARN of the IAM role. We will need this ARN while we are creating the Lambda function. Next, create a ZIP file of the lambda function; use the following command:

cd usercode
zip -r my-python-function.zip lambda_function.py

Create the Lambda functions by using the following commands:

aws lambda create-function \
--function-name MyPythonFunction \
--runtime python3.11 \
--handler lambda_function.lambda_handler \
--role <Role_ARN> \
--zip-file fileb://<Path_to_zip_file>

Run the commands given above using this widget. Enter your AWS Access_Key_ID and Secret_Access_Key in the widget below before running any commands. If you don’t have these keys, follow the steps in the AWS documentatiohttps://docs.aws.amazon.com/powershell/latest/userguide/creds-idc.htmln to generate the keys.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

Step 2: Prepare your dependencies

You can create the dependencies by using the commands in the widget below:

mkdir python
cd python
wget https://files.pythonhosted.org/packages/f1/c5/1e9c317a5e6af9280ad86a523ab6efe2ca70a0eb4bfb2220d8d08e255ae1/pandas-2.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
wget https://files.pythonhosted.org/packages/8a/08/a7e5dadc21fe193baea5f257e11b7b70cc27a89692fc9e3ed690e55cc4b6/numpy-1.26.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
wget https://files.pythonhosted.org/packages/32/4d/aaf7eff5deb402fd9a24a1449a8119f00d74ae9c2efa79f8ef9994261fc2/pytz-2023.3.post1-py2.py3-none-any.whl
unzip numpy-1.26.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
unzip pytz-2023.3.post1-py2.py3-none-any.whl
unzip pandas-2.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
rm numpy-1.26.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
rm pytz-2023.3.post1-py2.py3-none-any.whl
rm pandas-2.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
cd ..

Here is a brief explanation of the code:

  • Line 1: We create a new python directory, which will store the libraries and the dependencies.

  • Lines 3–6: We install the libraries required to the directory we created earlier. We download specific packages from the Python package index here. You can also use pip3 install --target ./python <Library_name> to download specific libraries. We explicitly use files from the Python package index because, this way, we can download specific packages for specific compatible runtimes without any consistency issues.

  • Lines 7–9: We extract the packages.

  • Lines 11–13: We remove the files that are not required.

Step 3: Create the layer ZIP file

The next step is to create a ZIP file containing all the dependencies. You can use the following command to create a ZIP file:

zip -r ./python.zip python/.

We create a ZIP file named python.zip. We must name the file python.zip because the packages we use are Python packages.

Step 4: Publish the Lambda layer

The following commands create a Lambda layer on the AWS console.

aws lambda publish-layer-version \
--layer-name pandasLayer \
--description "Description for the layer" \
--zip-file fileb://python.zip \
--compatible-runtimes python3.11

Here is a line-by-line explanation of the code:

  • Line 1: We publish a Lambda layer on the AWS.

  • Line 2: We specify the name of the Lambda layer.

  • Line 3: We specify the description of our Lambda layer.

  • Line 4: We specify the ZIP file we upload to our AWS Lambda layer.

  • Line 5: We specify the runtime our Lambda functions use. The Lambda functions we run in AWS use python3.11. Therefore, we specify python3.11 here as well. You can also specify the version you are using for your Lambdas.

After a layer is successfully published, we receive a response with the layer’s configuration. Save the LayerVersionArn value from the response. We will use it to attach the layer to a function.

Step 5: Add the layer to your Lambda function

aws lambda update-function-configuration --function-name <Your_Function_Name> --layers <Your_Lambda_LayerARN>

In the command above, replace the <Your_Function_Name> placeholder with the name of your function and <Your_Lambda_LayerARN> placeholder with the LayerVersionArn

Step 6: Verification

You can use the following command to verify that the layer is attached successfully to the Lambda function:

aws lambda get-function-configuration --function-name <Your_Function_Name>

In the command above, replace the <Your_Function_Name> placeholder with the name of your function.

Note: While creating the Lambda layer on your local machine, ensure that you have AWS CLI installed. To establish a connection with your AWS console, you must run the aws configure command and then provide the required credentials.

Practice

Run the commands given above using this widget. Enter your AWS Access_Key_ID and Secret_Access_Key in the widget below before running any commands. If you don’t have these keys, follow the steps in the AWS documentation to generate the keys.

Terminal 1
Terminal
Loading...

You can access the created Lambda layer on the AWS console in the Lambda dashboard.

Adding the Lambda layer to the Lambda function via AWS Console

We can add the Lambda layer to a Lambda function by scrolling down to the function’s page, clicking the “Add Layer” button, and selecting the “Custom Layer” option. There, we can select the layer and the version we want. If we run the function, we’ll observe that the libraries are also included.

Benefits of Lambda layers

There are several benefits of using Lambda layers:

  • Efficiency: Layers can improve the efficiency of deployments and updates by keeping deployment packages small.

  • Modularity: Layers promote modularity by allowing us to update dependencies independently of the function code.

  • Reusability: We can apply a single layer to multiple functions, reducing redundancy and simplifying dependency management.

Here is an illustration to demonstrate how Lambda layers are attached to multiple lambda functions:

Lambda functions with Lamdba Layer
Lambda functions with Lamdba Layer

In the illustration above, three Lambda functions are using a Lambda layer. The Lambda layer contains the dependencies, libraries, and shared code required by all three Lambda functions.

We can include up to five layers per function, which only applies to Lambda functions deployed as a ZIP file archive.

Conclusion

Lambda layers offer significant advantages for developers utilizing AWS Lambda. Lambda layers empower developers to build scalable and maintainable serverless applications with their ability to enhance efficiency, promote modularity, and encourage reusability. Leveraging up to five layers per function, Lambda layers provide a flexible and versatile solution for managing code dependencies in Lambda functions deployed as ZIP file archives.


Free Resources

Copyright ©2024 Educative, Inc. All rights reserved