Skip to main content

Interacting with DynamoDB using the AWS SDK for Node.js

Hands-On Lab


Photo of

Training Architect





In this activity, you will look at how we can take advantage of the AWS SDK for Node.js to interact with DynamoDB. You will be provided with an EC2 instance that will allow you to emulate a local environment without having to worry about cluttering up the development tools on your personal machine. After signing into that EC2 instance you will install the AWS SDK for Node.js and use it to load music data into our DynamoDB PrometheonMusic table.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

DynamoDB AWS SDK Practical

In this lab, we are working for the company PrometheonPartners. We are tasked with updating a table in DynamoDB. The table, called PrometheonMusic, needs to have all of its items deleted and then a new set of items need to be loaded into the table. To do this, we use an emulated local machine, using the provided lab credentials, and AWS SDK for JavaScript. Then we run pre-made files that will perform both the deletion and the upload of the table's items.

Review the Table

Sign in to the AWS Console with the account and password given. Once logged in, select DynamoDB from either under Recent, or select the Services dropdown. Scroll down to Databases, and select DynamoDB from the list.

On the DybamoDB page, select Tables. The table PrometheonMusic appears. Open the table. There is already a large amount of information in the table, and we want to remove all of it.

Prepare for Deletion

Sign in to your local machine and then, using the provided information, sign in to the emulated machine as a cloud user for Amazon Linux AMI. You may be asked to perform a yum update while signing in.

Once we've signed in to Amazon Linux AMI, use the ls command to list out your current directory. Three items appear in the directory:

[cloud_user@IP]$ ls
dataDeleter.js dataWriter.js musicData.json

Use head to review the information for musicData.json to get a general idea of the information in the file. There is much more information in the file than what the head command will show:

[cloud_user@IP`]$ head musicData.json

Information including an Artist, SongTitle, and other data about that song will appear. This is the information that we will be placing into the PrometheonMusic table. Before we can add it, though, we need to review our dataDeleter.js file. Use your preferred editor to review the file:

[cloud_user@IP]$ $EDITOR dataDeleter.js

This file is already set up to delete the data from the table. The following is a breakdown of the information in the file.

Deletion Script

The first portion of the script goes over what will be required to run it, where it will be run, and for what client. In this instance, we will need aws-sdk and sleep to run the process. Our region tells our script where this will be run, and our documentClient designates that it will be working with DynamoDB, making it easier to run with the Java SDK.

var AWS = require("aws-sdk");
AWS.config.update({region: "us-east-1"}); 
var documentClient = new AWS.DynamnoDB.DocumentClient( );

sleep is a third-party application that will keep us from removing information from the table too quickly.

var sleep = require("sleep"); 

This next section designates what we are doing with this script. All of our current items under SongTitle start with test and are numbered 0-14. This is where our counter comes in to play, designating all items named title, with numbers 0-15, will be deleted. This works along with the counter++ at the end of the script.

var counter = 0

while (counter < 15) { 
  console.log("Deleting item with SongTitle of test" + counter.toString( ) ) 

The following portion of code designates everything that will be deleted and where it will be deleted from. We designate our table, PrometheonMusic, and the table's two keys, the Artist and SongTitle, which we set up in the previous lab. The code designates that all artists named "Stephen James" will be deleted, along with the items designated in by our counter.

  var params = {
    TableName : "PrometheonMusic",
    Key: {
      Artist: "Stephen James",
      SongTitle: "test"+conter.toString( )

This portion of the code is to let us know if there are any errors when running the deletion script. If so, an error is logged and we are told.

documentClient.delete(params, fucntion(err, data) {
  if (err) consol.log(err);
  else console.log(data);

The last thing added is the sleep command we made mandatory at the beginning. This command designates a pause in milliseconds, as designated by the 'm' before the sleep, and that it will pause for 300 milliseconds between deletions, which will stop the table from trying to delete information too quickly and cause a possible error. This will only allow four items to be deleted at a time — which is considered a write action — and as we set our write unit amount to five in the last lab, this will ensure that we do not exceed the write unit amount.


Go ahead and leave the file.

Run the Deletion

Now that we've gone over our deletion script, it is time to run it. First, check to make sure that node is installed. Without it, we won't be getting very far. Test it with console.log("hello"), which will have node return 'hello' and 'undefined'. Exit the test with control+X.

[cloud_user@IP`]$ node

Alright, we have a working node. Time to begin the deletion process. Use node to run the dataDeleter.js file.

[cloud_user@IP`]$ node dataDeleter.js

Well shoot, we got an error. Why? Because we forgot to install the aws-skd module, which we designated as a requirement for our deletion script. Using the npm command, install aws-sdk.

[cloud_user@IP`]$ npm instal aws-sdk

There, now that that's installed, let's run the script again.

[cloud_user@IP`]$ node dataDeleter.js

Another error. This time, it tells us that we are missing the sleep program. Again, use npm to install sleep.

[cloud_user@IP`]$ npm instal sleep

There, our two requirements are installed. Let's run the file again.

[cloud_user@IP`]$ node dataDeleter.js

This time it runs, deleting everything out of the table. To check, go back over to your DynamoDB, refresh the page, and look at the now empty table.

Add Items to the Table

Our table is ready to receive new information. Note that if you ls now, node_modules and tmp will be added to our list of files.

To add new items into our table, we will use the dataWriter.js file, which will take all of the content from our musicData.json and add it to the table. Using your preferred editor, open the file.

[cloud_user@IP`]$ $EDITOR dataWriter.js

Add Script

As before, the first section sets up all of our requirements. The only main difference is the requirement of the fs system to interact with the file system.

var AWS = require("aws-sdk");
AWS.confog.update({region: "us-east-1"}); 
var documentCloent = new AWS.DynamnoDB.DocumentClient( );

var fs = require("fs");

var sleep = require("sleep");

For this script, we are setting up the file system to read, and sync, the musicData.json file and then take the content of that file and parse through it.

var musicData = fs.readFileSync("musicData.json");
var jsonContent = JSON.parse(musicData);

The information pulled from the JSON file is then set up to pull all information from an entry and log it as the jsonContent[entry], meaning each entry will act as an item when uploaded to our table in DynamoDB.

for (entry in jsonContent) {
  console.log("THIS IS THE ENTRY")

Our parameters designate our table, being the PrometheonMusc table, and designates that the entries we coded above will populate the table as separate items.

  var params = {
    TableName : "PrometheonMusic",
    Item: jsonContent[entry]

As with the deletion, we have a section to tell us about errors when running the script.

  documentClient.put(params, function(err,data) {
    if (err) console.log(err);
    else console.log(data);

This time we are using sleep to make sure we do not upload items too quickly. Again, this will stop us from exceeding our write unit of 5.


Close the script, and then use node deploy the script.

[cloud_user@IP`]$ node dataWriter.js

Once the script finishes running, go back to the DynamoDB and refresh the page. It may take a few refreshes before the table shows as populated. Once we can see the populated table, we're done!


Congratulations! You are now in possession of knowledge that allows you to do a mass deletion, or addition, of data to a DynamoDB table from your local machine!