Get All Items from a DynamoDB Table using ScanCommand

Tomasz Łakomy
InstructorTomasz Łakomy

Share this video with your friends

Send Tweet
Published 4 months ago
Updated 3 months ago

Now we're getting somewhere 🎉

In this lesson we're going to create a new lambda function that will read data from our new TodoTable.

Instructor: [0:00] Now that we have a DynamoDB table with some items inside of it, it is time for us to start building our serverless backend. Let me go back to our stack. Here, we are going to create a new lambda function that is going to be responsible for getting all the todo items from our table.

[0:15] I'm going to start by creating a new file in our lambda directory, and I'm going to call it listTodos.ts. I'm going to paste in some boilerplate code that you can find in the source code for this lesson. As we can see here at the top, we are going to use two modules from AWS software development kit. We are going to need the client-dynamodb and the lib-dynamodb.

[0:37] Let me go ahead, open up the terminal, and I'm going to run npm install. Let me grab that and also this part. Now that our dependencies have been installed, let me walk you through the code. Over here at the top, we are creating an instance of DynamoDBClient and also a DynamoDBDocumentClient.

[0:54] I know that this is confusing, but what is important to remember here is that, DynamoDBDocumentClient is a simpler version of DynamoDBClient, which allows us to interact with DynamoDB through our code using a much simpler interface. Next up, we are going to grab the TABLE_NAME from the environment variable. Let me scroll down to the handler.

[1:15] This is the function that's going to be invoked whenever we call this lambda function. First of all, this is going to get all the todos which we have to implement. Secondly, it is going to create a response which we can see over here. By default, it's going to return a statusCode of 200, and it's also going to return, as a body, whatever we pass in over here.

[1:35] Let's go ahead and implement this function. The way that we work with AWS SDK is that we create various commands which we then send to our client. Which command do we need to send in order to get all the items from our todo table? If you take a look at the console, we can either scan or query items over here.

[1:54] Scan is a DynamoDB operation that returns all the items that are in the table, which can get expensive if you have lots of items in your table, but it's going to be good enough for our use case. First of all, I'm going to grab the ScanCommand. Secondly, I'm going to create a new command. I'm going to do const command = new ScanCommand.

[2:15] We need to pass in the ScanCommand input, which in our case requires the TableName. The TableName is going to be equal to tableName. Again, this is the TABLE_NAME that we are grabbing from the environment variable over here. Next up, I am going to do const scanResult, which is going to be, = await docClient.send. We are going to send this command that we have created over here.

[2:38] At the end, I am going to go ahead and return this scanResult. In short, whenever our handler function gets invoked, we are going to issue a new ScanCommand, passing in the tableName, and we are going to create a response which is going to include all the items that were returned by the ScanCommand.