Create a DynamoDB Table in an AWS CDK App

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

Social Share Links

Send Tweet

Having a hardcoded source of data isn't going to work out in the short or long term. We need something a little more dynamic.

That's why we'll be learning how to create a DynamoDB table to store our data instead! In this lesson, we will create a table in our CDK app, and grant our lambda function read access to the table.

Tomasz Łakomy: [0:01] Hard coding data in a Lambda function is not exactly the best practice. We probably need a database. Let's go back to our stack and create a DynamoDB table. We don't have a construct for DynamoDB table yet, so we have to install it. To do that, we're going to open up the terminal and run npm install @aws-cdk/aws-dynamodb.

[0:20] With that done, let me go ahead and import it again. I'm going to just copy that and change lambda to dynamodb, like this. Next, we're going to create a table to store our books. I'm going to go over here and do const booksTable, which is a new dynamodb.table, and the first argument is this. I'm going to call it a "BooksTable", because it's going to store books.

[0:41] There's a number of props that we need to pass in. First of all, I'm going to set the partitionKey to be an id. Its type is going to be a dynamodb.AttributeType, and it's going to be a STRING. The reason for that is that I would like to have this id match the ID type in our graphql schema. This id is in fact a string. That is why this partitionKey for our BooksTable is also a string.

[1:06] I'm going to try one more thing. I'm going to add a billingMode which I'm going to set to dynamodb.BillingMode.PAY_PER_REQUEST. Now, with the DynamoDB table in place, we need to be able to tell the listBooksLambda where it can find all the books.

[1:21] In order to do that, I'm going to create an environment variable which I'm going to call BOOKS_TABLE. I'm going to set it to booksTable.tableName. This will ensure that in the code for our Lambda function, I will be always able to read the variable of BOOKS_TABLE in order to get the tableName of our booksTable.

[1:40] There's one more thing that we need to do. We need to tell AWS that it is OK for this Lambda to read the data from this booksTable, because by default, AWS is following the principle of least privilege. The idea is that all the resources should have the minimal amount of privileges in order to do their job.

[1:59] By default, this Lambda function cannot read the data from this table, but if I do booksTable.grantReadData(), and I'm going to provide this Lambda function here, this will ensure that the listBooksLambda can read the data from booksTable. Note that I'm only giving read access for this Lambda function because listBooks doesn't need to be able to write any data into our table.

[2:22] With all of that in place, let me run cdk deploy in order to deploy our stack to AWS. Again, it's going to ask me whether I'm OK with those changes. If we take a look over here, this is saying, do we want the listBooksHandler to be able to, for instance, get items from this table?

[2:40] Yes, we are OK with that. I'm going to hit yes and hit Enter. After this is done, let us verify our deployment. I'm going to navigate to AWS Console, and I'm going to navigate to CloudFormation Console.

[2:51] Next, I'm going to click on our stack name and go to Resources. Here, I'm going to search for dynamodb. Apparently, we do have a DynamoDB table created. I'm going to click on its id in order to navigate to it. Hear it is, we do have a DynamoDB table created.

[3:07] Let us view its items by clicking on this button over here. Apparently, this table is empty. For the time being, let us create two items. To simplify things a bit, I am not going to use this web form, but instead, I'm going to navigate to JSON, and just paste a JSON object for the first book, create it, and we can see this book over here.

[3:24] Now, I'm going to create another item, which is going to be another book, and hit Create item again. We have our DynamoDB table pre-populated with two books.