Create and Deploy a Node.js AWS Lambda Function with AWS CDK

Tomasz Łakomy
InstructorTomasz Łakomy

Share this video with your friends

Send Tweet
Published 4 months ago
Updated 3 months ago

It's time to start building our serverless backend! In this quick lesson we're going to use the NodejsFunction construct provided by CDK in order to build, deploy and test a 'Hello World!' AWS Lambda function. You can find out more about what kind of constructs are available in AWS CDK construct library here

Instructor: [0:00] Since we have an empty stack right now, it is a perfect time to start building our serverless backend. Since we are using TypeScript in this course to provision our infrastructure, we are also going to use TypeScript in order to write the code of our serverless AWS Lambda functions.

[0:15] To learn how to do that, let me go to AWS CDK API Reference, which lists all the possible constructs that we can use in our CDK applications. I'm going to scroll all the way over here to AWS Lambda Node.js, and I'm going to click on Overview. Here, we can see the description of Lambda Node.js module.

[0:34] This module exposes a Node.js function construct, which is going to create a Lambda function with automatic transpiling and binding of TypeScript code, and it's also using esbuild under the hood. Let's go back to our codebase.

[0:46] First of all, I'm going to open up the terminal and install types for AWS Lambda as well as esbuild. With this done, I'm going to go ahead and create a new directory, which I'm going to call Lambda, and inside of it, I'm going to create a new file called helloworld.ts.

[1:05] Let me paste the body of a very simple Lambda function. This function is going to always return a status code of 200, and it's also going to return a body with a message of "Hello World".

[1:17] With that in place, let's go back to our stack, and over here, I'm going to import the Lambda Node.js module from AWS CDK lib, and I'm going to create a new Helloworld function, which is going to be an instance of new Lambda Node.js, that Node.js function.

[1:34] As mentioned before, Node.js function is a CDK construct, and each CDK construct takes three arguments. The first one is a scope, which in this case is going to be this because I am creating this Node.js function in the scope of the studio app stack. The second one is a logical ID. I'm just going to call it Helloworld function, and the third one are the props.

[1:56] There are two props that are actually required in order to create this function. The first one is the entry, which is in our case, lambda/helloworld.ts, which if you recall, is where our function lives, and the second required prop is the handler.

[2:12] I'm going to call it handler, and that's because, in our Helloworld function over here, we are exporting a function called handler, which is a convention when it comes to AWS Lambda functions. Let's go back to our stack, and we are ready to deploy. Let's open up the terminal and run CDK div to see what we are about to deploy.

[2:32] We can see that by adding our Node.js function to our CDK stack, we are about to deploy two things. We are going to deploy the actual Lambda function, which we can see over here, and we are going to deploy an IAM role, which is necessary so this function can actually be invoked.

[2:47] That, again, is the power of CDK, because we get to focus on the things that we want to accomplish, in that case, running this code as a serverless Lambda function, and CDK takes care of everything else. With that, I'm going to hit cdk deploy to deploy this stack into AWS.

[3:05] Now that our stack has been deployed, let's go back to AWS console. I'm going to go to CloudFormation, click on our stack, expand this a little bit, go to Resources, and here we can see our Helloworld function. If I expand that, we can see that this role that I mentioned earlier is here in our AWS account, and we also have this function.

[3:26] I'm going to click over here in order to visit AWS Lambda console for this particular function. Here we can see that we have our brand new Helloworld function, which was last modified two minutes ago. If I scroll down a little, I can click on Test, scroll down even more, and click on Test button in order to actually invoke this function. As we can see, our function has been invoked.

[3:48] If I expand the Details section over here, we can see that the status code was 200, and the body did contain the message "Hello World," which is exactly the same message that we have defined over here inside of our Lambda function.