Add Data to a DynamoDB Table with PUT Operation use AWS CDK

Tomasz Łakomy
InstructorTomasz Łakomy

Share this video with your friends

Send Tweet
Published 4 months ago
Updated 3 months ago

In this quick lesson we're going to create a createTodo AWS Lambda function which will allow us to add new items to our DynamoDB table.

Instructor: [0:00] Let's continue building our backend. Currently, we have a listTodos function, which can list all of the todos, but we would like to also be able to create a todo. I'm going to start by creating a new lambda function, which I'm going to call createTodo.ts, and I'm going to paste in some boilerplate.

[0:17] Going from the top, first of all, as previously, we are going to create a new DynamoDB client. We are going to get the table name from the environment variable. Over here is the addTodoItem function that we have to implement. Let's take a look at the handler. First of all, this function is going to take an API Gateway event because it is going to be invoked via API Gateway.

[0:38] First of all, we are going to destructure the request body from Event, and if there is no request body, we are going to return an error because we do require a request body. Actually, this should be a 403, not 500. Let me fix that.

[0:51] Then we are going to parse the data, and we are going to evoke the addTodoItem function that we are going to implement over here. Afterwards, we are going to either tell the client that the TodoItem was added to the database or we are going to return an error. Let's go to the addTodoItem. We need to send a command to DynamoDB in order to add a new item to a DynamoDB table.

[1:15] First of all, I am going to import the PutCommand over here. Now I'm going to create it. I'm going to do const command = new PutCommand and this command actually requires two things. First of all, we need to parse in the Item that we are about to create in the table. We also need to parse in the TableName because DynamoDB needs to know in which table we would like to create this item.

[1:40] We are going to do the following. The TableName, I'm going to parse in whatever was provided in the environment variable and the Item is going to be a collection containing the ID and todo. Both of those are things that we actually get from the API gateway.

[1:55] We can see that over here. The event contains a request body. We are going to parse that, and the data from the request body is going to be parsed into our addTodoItem and then is going to be put into DynamoDB table. Now that our command is ready, we need to send it. I'm going to await docClient.send(command).

[2:13] You might notice an issue here. What happens if there's no ID provided by the client? We should have a fallback that backend will be able to create its own IDs and store that in DynamoDB. Let me just open up the terminal and I'm going to npm install uuid package. With that, I'm going to go over here and import that as UUID and whenever there is no ID, I'm actually going to generate an ID.

[2:39] Looks like TypeScript is complaining because there are no types for UUID, so let me just copy that and I'm going to install that as well. Right now, we have the package and the types to generate unique IDs. With that, we are ready to go back to our Todo Backend and we need to create a new function.

[2:56] I'm going to just copy that, paste that over here and I'm going to change it to createTodos function. The entry needs to become a createTodo. The handler stays the same because we are exporting a handler function as a convention when it comes to AWS Lambda functions. The rest can stay the same, but we need to do one more thing.

[3:17] Here, we are granting readData to the listTodos functions that we have created in the previous lesson. Now, we need to actually do something a little bit different. We need to grant read/write data access because our createTodos function needs to be able to write data into our DynamoDB table. We need to replace the listTodos function with createTodos function.

[3:43] With all of that in place, we are ready to deploy. Let me open up the terminal and I'm going to run cdk deploy. Before we deploy, we are going to get asked whether we want to allow the createTodos function to perform all those actions on DynamoDB table.

[3:58] The action that we actually care about is the PutItem, which puts a new item in a DynamoDB table. This is exactly what we want, so I'm going to hit yes and hit enter. After a successful deployment, we have one more function in our serverless backend.