Tutorial Using Lambda function with Layer

Tutorial Using Lambda function with Layer

Aung Kyaw Nyunt
For the Lambda Functions, AWS support various type of programming language, they are Node.Js, Java, C#, Python, Go, PowerShell.
In this tutorial, we will use NodeJs programming language.
Pre-requirement:

  • AWS Account

  • Basic Understanding of NodeJs or Python


In this tutorial, you will learn :

  • What is Lambda?

  • What is Lambda Layer?

  • How to create lambda with layer.

What is Lambda?
Lambda is ServerLess Compute Service that lets you run code without provisioning or managing servers.
Lambda runs your function only when needed and scales automatically.
You can invoke your Lambda functions using the Lambda API, or Lambda can run your functions in response to events from other AWS services.
What is Lambda Layer?
A layer can contain libraries, a custom runtime, data, or configuration files.
Layers promote code sharing and separation of responsibilities so that you can iterate faster on writing business logic.
How to create lambda with layer.
Story : I will create CustomDateFormat funcion in lambda layer.
then I will use these CustomDateFormat function from lambda function.

  • Step 1 : Create Layer and Deploy Layer.

  • Step 2 : Create Lambda Function and Deploy.

Step 1 : Create Layer and Deploy Layer.
Go to Lambda dashboard and then search and click on "layers" from the right navigation bar.
Then click "Create Layer" button as the following screen shoot picture.
Step 1 : Create Layer and Deploy Layer.
let write some codes in file "customDateFormatFunction" . so create "customDateFormat.js" file and write the following code.
const options = { hour:"numeric", minute:"numeric", weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; exports.customDateFormatFunction = (dateObj) => { // TODO implement return new Date(dateObj).toLocaleDateString("en-US", options); };
then zip your file and upload your zip file to lambda layer as the following screen shoot.
now our layer is ready to use. so it is time to create lambda function.
Step 2 : Create Lambda Function and Deploy.
on the left navigation bar of your aws dashboard, there is "Functions" under AWS Lambda.
click on it to create new our function.
Function name : DemoFunction Runtime : Node.js 14.x
then click "Create function".
after we created the function, we will get the DemoFunction on list.
click on it then scroll down to bottom to choose the layer and layer's version.
Code for our DemoFunction.
It is time to modify the index.js code with tthe following code.
const {customDateFormatFunction} = require('/opt/customDateFormat.js'); exports.handler = async (event) => { // TODO implement const response = { statusCode: 200, body: JSON.stringify(customDateFormatFunction(new Date())), }; return response; };
Conclusion
we can install node module to both of function and layers.
But we do not need to install every node module to all functions.
we have to install node moudle to layer.
install node_module to functions : no (you can , if you wish)
Install node_mudule to layers : yes
Thank you for reading my articles
https://www.applix.info
© All right Reserved. Inspired Codes...
Get In Touch
Rule and Policy