Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?

Deploy your first Node 20 Lambda function on AWS!



TL;DR

AWS launched a public base picture for Node 20 Lambda runtime final week. It’s now attainable to deploy Node 20 Lambda features on AWS, by utilizing this picture with ECR (Elastic Container Registry). On this article, uncover how you can deploy your first Node 20 Lambda operate on AWS, earlier than the managed runtime is launched! Keep tuned until the tip to see the efficiency outcomes.

I you wish to keep in contact right here is my twitter account. I usually submit or re-post fascinating stuff about AWS and serverless, be happy to observe me!

Follow me on twitter 🚀

Fast announcement: I additionally work on a library known as 🛡 sls-mentor 🛡. It’s a compilation of 30 serverless best-practices, which might be robotically checked in your AWS serverless tasks (regardless of the framework). It’s free and open supply, be happy to test it out!

Find sls-mentor on Github ⭐️



Tips on how to deploy a pre-release Node 20 Lambda operate on AWS?

Node 20 is not formally supported by AWS Lambda but. Nonetheless, AWS launched a public base picture for Node 20 Lambda runtime final week. It’s attainable to make use of this base picture to construct a customized runtime with docker, and use it to deploy Lambda features on AWS. That is achieved in three easy steps:

  • Construct a Node 20 Lambda docker picture primarily based on the general public AWS base picture (achieved domestically)
  • Push this picture to ECR (Elastic Container Registry)
  • Create a Lambda operate utilizing this picture



Construct a Node 20 Lambda docker picture

First step: create a dockerfile to construct a Node 20 Lambda docker picture.

FROM public.ecr.aws/lambda/nodejs:20.2023.11.12.08-arm64
COPY app.js ./
CMD [ "app.handler" ]
Enter fullscreen mode

Exit fullscreen mode

This picture is derived from the official public.ecr.aws/lambda/nodejs:20.2023.11.12.08-arm64 picture. You may also use public.ecr.aws/lambda/nodejs:20.2023.11.12.08-x86_64 if the goal Lambda structure is x86_64. Then, it copies the app.js file to the picture, and units the CMD to app.handler. Which means now we have to create a app.js file in the identical listing because the dockerfile, and outline a handler operate in it.

To check if the picture runs on Node 20, let’s use a Node 20 solely characteristic: the Array.prototype.toSorted technique, which creates a brand new sorted array with out modifying the unique array. This new technique is just obtainable in Node 20, so if the picture runs on Node 20, it ought to work. Right here is the app.js file:

exports.handler = async () => {
  const desk = [2, 3, 1, 9, 4];

  const sortedTable = desk.toSorted();

  return sortedTable; // [1, 2, 3, 4, 9]
};
Enter fullscreen mode

Exit fullscreen mode

We’re achieved! Lets construct the picture! I add a lambda-image:node-20 tag to the picture, in order that I can simply discover it later.

docker construct --platform linux/amd64 -t lambda-image:node-20 . # tag the picture with your individual tag
Enter fullscreen mode

Exit fullscreen mode



Push the picture to ECR (Elastic Container Registry)

Now that the picture is constructed, we are able to push it to ECR. First, we have to create a repository in ECR. I’ll use the AWS CLI to take action. For all the next instructions, substitute <REPOSITORY_NAME> with the title you wish to give to your repository, together with your AWS account ID, and with the AWS area you wish to use.

# create the ECR repository
aws ecr create-repository --repository-name <REPOSITORY_NAME> --image-scanning-configuration scanOnPush=true --region <AWS_REGION>

# login docker to ECR
aws ecr get-login-password | docker login --username AWS --password-stdin <AWS_ACCOUNT_ID>.dkr.ecr.<AWS_REGION>.amazonaws.com

# tag the picture with the ECR repository URI
docker tag lambda-image:node-20 <AWS_ACCOUNT_ID>.dkr.ecr.eu-west-1.amazonaws.com/<REPOSITORY_NAME>:newest

# push the picture to ECR
docker push <AWS_ACCOUNT_ID>.dkr.ecr.eu-west-1.amazonaws.com/<REPOSITORY_NAME>:newest
Enter fullscreen mode

Exit fullscreen mode

We’re achieved with Docker! Now, let’s create a Lambda operate utilizing this picture.



Create a Lambda operate utilizing this picture

For this step, I’ll use the Net UI, for readability. You may also use any programmatic solution to create your Lambda operate. The method is fairly easy:

Step 2

Step 3

You might be achieved! Now you can check your Lambda operate. If all the things went effectively, it’s best to see the next end result:

Step 4

The Array.prototype.toSorted technique labored as anticipated! Which means the Lambda operate ran on Node 20.



Lambda Node 20 efficiency

In ran 50 chilly and heat begins on this Lambda operate, and measured the execution time. Listed here are the outcomes, in comparison with the same Lambda operate working on the aws-managed Node 18 runtime:

Runtime Median execution time Common execution time Chilly ⛄️ or heat 🔥
Node 18 177ms 175ms Chilly ⛄️
Node 20 236ms 232ms Chilly ⛄️
Node 18 2ms 11ms Heat 🔥
Node 20 2.5ms 28ms Heat 🔥

What can we be taught from these outcomes? Node 20 is slower than Node 18 on chilly begins, and has related efficiency on heat begins. This isn’t stunning, as Node 20 remains to be in pre-release and is deployed utilizing a container picture, which has slower chilly begins.

Count on the efficiency to be higher when the managed Node 20 runtime is launched!



Let’s join!

I might actually recognize for those who may react and share this text with your mates and colleagues. It should assist me lots to develop my viewers. Additionally, remember to subscribe to be up to date when the following article comes out!

I you wish to keep in contact right here is my twitter account. I usually submit or re-post fascinating stuff about AWS and serverless, be happy to observe me!

Follow me on twitter 🚀



Add a Comment

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?