What is Lambda Layer, importance of it and how to create one?

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:

  1. Code reuse: Layers allow developers to share code across multiple functions, reducing the amount of duplication and making it easier to manage dependencies.
  2. Smaller function packages: By separating dependencies into layers, the size of function packages can be reduced, which can lead to faster deployment times and lower costs.
  3. Faster development cycle: With Layers, developers can focus on writing the function code without worrying about managing dependencies, which can speed up the development process.
  4. Version control: Layers can be versioned independently of functions, allowing for more granular control over dependencies.
  5. Security: Layers can be created and maintained by trusted parties, such as vendors or internal teams, which can help ensure that dependencies are kept up to date and free of security vulnerabilities.

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:

  1. Sharing code between multiple Lambda functions: If you have multiple Lambda functions that require the same dependencies or common code, it might make sense to package those dependencies or code as a Lambda Layer and share it across the functions.
  2. Importing missing packages: If you are running a code lets say on Python3.7 and in your code you require some specific libraries which are missing, you would then want to install those libraries and use it in a lambda layer so your Lambda function can consume those libraries along with your code.
  3. Separating out large dependencies: If your function has large dependencies that don't need to be updated frequently, such as machine learning libraries or data sets, you can separate them out into a Layer to keep the function package size small and reduce deployment times.
  4. Isolating security-sensitive code: If you have code that contains sensitive information or access credentials, you can isolate it in a separate Layer so that it can be managed and updated independently of your function code.
  5. Simplifying deployment: By separating your function code from its dependencies, you can simplify the deployment process by deploying the function code and the Layer independently of each other.
  6. Managing versioning: By creating a separate Layer for dependencies, you can version it independently of your function code, making it easier to manage and update dependencies across multiple functions.

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:

  1. Running EC2 machine.
  2. Pem and PPK keys for your EC2 logins.
  3. Windows CMD Admin rights.


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:

No alt text provided for this image
No alt text provided for this image

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:

No alt text provided for this image

Step 9:

Now head to Lambda Console>>Layers>>Create Layer as below.

Upload the zipped file you saved on your PC.

No alt text provided for this image
No alt text provided for this image

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!

要查看或添加评论,请登录

Anchal Marwah的更多文章

社区洞察

其他会员也浏览了