What is Lambda Layer, importance of it and how to create one?
This blog post revolves around a very important topic of AWS Lambda, called as "Lambda Layers". In this blog post, Ill also provide some commands to create a lambda layer using EC2 and then transfer that layer to your local windows machine. So stay tuned!
Firstly, what is Lambda-Layer and importance of it?
In AWS Lambda, a Layer is a distribution mechanism for libraries, custom runtimes, or other function dependencies that can be shared across multiple Lambda functions. It allows developers to package their code and dependencies separately, making it easier to manage and reuse across multiple functions.
Lambda Layers are essentially ZIP archives that can be uploaded and associated with one or more Lambda functions. When a function is executed, AWS automatically makes the contents of the associated layers available to the function code, along with the code of the function itself.
There are several benefits to using Lambda Layers:
Overall, Lambda Layers provide a powerful mechanism for sharing and managing dependencies in AWS Lambda functions, allowing developers to focus on writing code and reducing the amount of time spent managing dependencies.
Secondly, when do I need a Lambda Layer?
You might need to create a Lambda Layer in the following scenarios:
In general, you might want to create a Lambda Layer whenever you have code or dependencies that need to be shared or managed independently of your function code. Creating a Layer can help you simplify the deployment process, improve code reuse, and make it easier to manage dependencies across multiple functions.
Steps to create a Lambda Layer Library package using EC2:
Pre Requisite:
Step 1:
Check for the error on your Lambda execution as to which module you want to install. In my scenario, I am running a test lambda function which will require a numpy package in my python3.7 function.
What is numpy package?
In simpler words, its a Python library that is used for mathematical computations.
Error:
{
?"errorMessage": "Unable to import module 'lambda_function': No module named 'numpy'",
?"errorType": "Runtime.ImportModuleError",
?"stackTrace": []
}
Step 2:
Login to your EC2 instance using Putty, and your keys to install the "numpy" package.
Step 3:
Check pip version:
pip –version
Make sure you are on the current version of pip/python.
Step 4:
Now make a directory in which you want to install your package. I am making a directory here called mathsLayer.
领英推荐
mkdir mathsLayer
Step 5:
Now cd to mathsLayer directory and then install the numpy package.
pip install numpy -t .
Please note: “.” Represents that the package will be installed in the current directory i.e mathsLayer.
Step 6:
Now list contents of "mathsLayer" directory and see if the packages have been installed.
Step 7:
Zip the package with your name, the name I am giving here to my lambda package is "numpy.zip" in the mathsLayer directory.
zip numpy.zip -r .
Steps 4-7 shown below in a snippet:
Step 8:
Head back to Command Line on your PC and type the below command in this format to copy the zipped package to your local PC using SCP command.
scp -i "yourPemKey.pem" ec2-user@ip-of-your ec2:/remote/path/of/your/zipped file .
This will install your zipped package to your local machine as shown below:
Step 9:
Now head to Lambda Console>>Layers>>Create Layer as below.
Upload the zipped file you saved on your PC.
Step 10:
Once created add this layer to your lambda function and execute, this should resolve your error.
{
?"statusCode": 200,
?"body": "\"numpy package installed successfully!\""
}
Conclusion
In conclusion, we saw why lambda layer is important and when to use it. Additionally we have gone through over a step by step guide on creating a Lambda Layer using an EC2 instance, which can be a straightforward process. By following a few simple steps, you can create a ZIP archive that contains your code or dependencies and upload it to AWS Lambda.
Using an EC2 instance to create a Lambda Layer allows you to install any dependencies you need and package them in a way that can be shared across multiple functions. This can help reduce the size of your function packages, simplify deployment, and improve code reuse.
Follow for more!