TABLE OF CONTENT
|2. What is Lambda Function?|
|3. AWS Serverless Service UseCases|
|4. Step-by-Step Guide to building layers in Lambda function|
|5. AWS Layers|
|6. Final Takeaway|
|7. About CloudThat|
In this blog, we will understand the significance of AWS Lambda as a layer by its usage and build a step-by-step process for using any external packages (E.g., NumPy) in the AWS Lambda function. Code sharing capability is the top feature of layers as it can be used across many lambda functions. Before we kick start into lambda layers, we need to know the fundamentals of serverless function Lambda.
What is Lambda Function?
Lambda function is a serverless event-driven cloud computing service provided by Amazon as part of Amazon Web Services. There is no need to provision or manage the servers for the lambda function; hence, it is a serverless application. Hence, we don’t have to concentrate on Lambda’s servers and backend working but just on the code’s functionality.
E.g., We can write a script to run the lambda function that gets the metadata and store the data in DynamoDB once objects are uploaded to S3 Bucket. So, in this example, we need to concentrate on the coding functionality part, whereas Lambda will automatically take care of servers behind the scenes.
Learn more about AWS Lambda for beginners here.
AWS Serverless Service UseCases
- For Computing purposes: AWS Lambda, AWS Fargate.
- For Application Integration: Amazon EventBridge, AWS Step Functions, Amazon SQS, Amazon SNS, Amazon API Gateway, AWS AppSync
- For Data store: Amazon S3, Amazon DynamoDB, Amazon RDS Proxy, Amazon Aurora Serverless, Amazon Aurora Server
Step-by-Step Guide to building layers in Lambda function
A layer in Lambda has a runtime, architecture, config files, code libraries, and some metadata wrapped up into a single package as a .zip file. According to AWS, “Lambda layers provide a convenient way to package libraries and other dependencies that you can use with your Lambda functions.”
Below I am taking three simple steps for creating the external package, creating a layer, and adding a layer to the Lambda function.
Step 1: Creating external package:
In this part, we will create a python external package NumPy that will be used by lambda code. To do this, we need to know all the dependencies for the NumPy package. As of now, it doesn’t have any dependencies.
1.1 Go to the Pipy site https://pypi.org/
1.2 You can search for the NumPy package and go to the download section. In the download section, you will have multiple packages of NumPy with respective python versions and operating systems
E.g., Linux, win, PY 3.9, PY 3.8, etc.
NOTE: Here, cp39 specifies it is to be used for python version 3.9, and the above whl file is for Linux operating system
1.3 Choose the respective version of the NumPy package and download the whl file.
1.4 Once downloaded, create a python folder and extract the files to this folder.
NOTE: Make sure all the dependencies are under this python folder and don’t rename the python folder to anything except python.
1.5 Then zip the python folder. It should be a link python.zip file
We have created the package file required format for the AWS Lambda layer.
Now go to AWS console https://aws.amazon.com/ and search for Lambda and Create a lambda function, try to import the NumPy package and test it.
NOTE: Make sure that the runtime is the same as the runtime of the Lambda layer
Without adding any layers respective to the NumPy package, you will be getting the below error
The above error is because Lambda couldn’t find the NumPy package. As for now, Lambda Function is created, and NumPy Package as a zip file is ready. Now we will create a layer in Lambda that acts as NumPy Package. We can then use that NumPy lambda layer to any different lambda function whenever required.
Step 2: Creating a Layer
2.1 Open the lambda service from the amazon web service and the in-layer section, click on create layer
2.2 Fill in the basic details regarding creating layers according to the requirements. You have two options to upload the package ( python.zip ) designed above directly via the upload button or another object from the s3 bucket. And click create.
Step 3: Attaching a layer to the AWS Lambda function:
3.1 Go to the lambda function to add the lambda layer and click on add a layer.
There are three different ways to choose a layer
It’s a built-in layer that, by default, is given by AWS, and depends on the run time; E.g., Python 3.8 may have layers that 3.9 runtimes won’t have
- Custom Layers:
AWS will not provide all packages, so we need some external packages that is used by lambda code; hence to install externally, we need to push it as a custom layer. We will discuss how to add custom layers in detail
- Specify an ARN (Amazon Resource Names):
It is a unique identifier of AWS resources. EG: We have the option to share the ARN of the package created by custom layers to other lambdas as well. Since you have created a lambda layer, from the drop-down list box of all available layers you have built, you can choose your NumPy package.
The package NumPy has been successfully installed. You can now import the NumPy library and use it accordingly in your lambda code.
In this blog, I have described the fundamentals of serverless architecture and services under different themes like compute, application integration, and data storage provided by Amazon Web Services. The importance of lambda layers is a simple use case that delivers for adding external libraries like (NumPy) to the lambda code as a package.
CloudThat is the official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft gold partner, helping people develop knowledge on the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.
Feel free to drop a comment or any queries you have regarding AWS Lambda Layers, AWS Lambda Function, or its use cases, we will get back to you quickly. You can go through our Expert Advisory page and Managed Services Package for CloudThat‘s offerings.
Q1. What are the languages supported in Lambda?
A: .NET, GO, Java, Node.js, Python, Ruby, and PowerShell
Q2. What compatible architectures are supported by Lambda?
A. Two main architectures supported are x86_64 and arm64.
Q3. What is the time-out for the lambda function?
A. The Lambda maximum time out is 15 minutes.