1. 22
    Add external dependencies to an AWS Lambda function deployed with CDK
    2m 26s
⚠️ This lesson is retired and might contain outdated information.

Add external dependencies to an AWS Lambda function deployed with CDK

Tomasz Łakomy
InstructorTomasz Łakomy
Share this video with your friends

Social Share Links

Send Tweet

Most JavaScript projects use some (sometimes even too many) external dependencies. AWS Lambda functions written in node.js are no different - it's quite common to use packages from npm in order to ship our code faster and provide value to our users.

Luckily, CDK helps us here as well - in this quick lesson we're going to learn how to add external dependencies to an AWS Lambda function deployed with AWS CDK. We're also going to see where to find our code + dependencies after we deploy out stack to AWS.

Instructor: [0:00] There's one more thing to we have to fix in our API. Right now, every single todo item that we are going to add to the list is going to get a totally random id, which means that we cannot add more than one item to the list, because in DynamoDB, every item has to have a unique partition key.

[0:15] A better solution will be to generate a random id for each item, because this is going to allow us to store all those items in our table. In order to do that, we're going to use the uuid package from npm. Lambda function don't have to be contained in a single file. They can also use external dependencies like most JavaScript projects.

[0:33] First, open up the terminal, navigate to the Lambda directory, and here I'm going to run npm init -y in order to create a package.json. Once our package.json is created, I'm going to clear the terminal and run yarn add uuid package. For the record, you can also use npm. I just happen to use yarn. Since we are using TypeScript with our Lambda function, I'm going to also install yarn add @types/uuid.

[0:58] Next, import the uuid package over here. I will be using the version 4 because as we can see in the documentation, "This version is entirely random created from cryptographically strong random values." This is more than enough for our use case.

[1:12] Next, instead of using this totally random id in our @todo item, I will be using either an id which was provided by our frontend in the data object or I would be generating an entirely random id using the uuid package.

[1:27] Let's strip it. In our terminal run yarn CDK deploy. Before we test our API, let's take a look at our Lambda function in the AWS Console. Here, from CloudFormation let's go ahead and click on the physical id of the Lambda function.

[1:40] In the AWS Lambda Console, we can see that not only our to-the-handler.js is here, but also package.json, yarn log, and all of our Node modules. All of that was packaged by CDK and deployed to the Cloud for us. Yet another thing that we don't have to maintain ourselves.

[1:55] I'm back in Insomnia in order to test the API. Sending a get request is getting me all those todo items. Let's add a new one. I'm switching the get request to post and setting the body to a JSON and over here I'm going to do todo test todo, and then send that.

[2:10] As you can see, the test todo was added to the database. Let me delete that. Let me switch to get again, clear the body and I will be sending request to get all those todo items.

[2:19] Here you can see that our test todo was added to the database and also it has received a completely unique random id.