Working With DynamoDB, AWS NoSQL Workbench, and Node.JS
Tags: aws, cloudnative, cloudskills, node
Published: 2021-06-14 03:50:18

## Introduction
In this tutorial, I will show you how to set up a locally-hosted DynamoDB server, use the AWS NoSQL Workbench to insert data into DynamoDB, and finally, create a simple Node.JS project that will query sample data provided by AWS NoSQL Workbench.

## Prerequisites
This tutorial assumes you're comfortable executing terminal commands, navigating, and modifying the macOS and Ubuntu Linux file system. 

> My setup is a hybrid environment consisting of a Host, macOS Catalina, and a Guest Ubuntu VM instance. But You can set up everything on one host, macOS, Ubuntu, or Windows.

To follow along, you will need the following already installed:

On Ubuntu (Guest) or Host:
- [Docker](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04)
- [Docker Compose](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-ubuntu-20-04)
- [Node.js](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04)

On MacOS (Host):
- [Ubuntu 20.04.2](http://www.releases.ubuntu.com/20.04/) VM instance

Okay, let's get started! :) 

## Install AWS NoSQL Workbench

Let's get the simple stuff out of the way. Navigate to the [AWS DynamoDB Developer](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/workbench.settingup.html) page and download the AWS NoSQL Workbench for your Operating System.

![download](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zx1sfklk3ouyod7h1oij.png)

After downloading the installation file, follow the onscreen instructions to install NoSQL Workbench.


## Install DynamoDB

Docker and Docker Compose will create and manage the DynamoDB server container and install all the required software packages and dependencies.

Open your preferred terminal app and type the following command to create a folder, change directories and create a YAML file in one swoop, like so:

``` bash
$ mkdir ~/dynamodblocal && cd ~/dynamodblocal && touch docker-compose.yml
```

Next, copy-paste the following YAML code into the  docker-compose.yml file.
```yml

# docker-compose.yml

version: '3.8'
services:
  dynamodb-local:
    command: "-jar DynamoDBLocal.jar -sharedDb -optimizeDbBeforeStartup -dbPath ./data"
    image: "amazon/dynamodb-local:latest"
    container_name: dynamodb-local
    ports:
      - "8000:8000"
    volumes:
      - "./docker/dynamodb:/home/dynamodblocal/data"
    working_dir: /home/dynamodblocal
```

Finally, launch the DynamoBD Server by typing the following command in the terminal:
``` bash
$ docker-compose up
```
The first time you execute this command, it will take a few minutes to get the server up and running. The next time after that, it should be instant. 

Now, if you see an output similar to the one below. That means you have successfully installed and launched a DynamoDB Server!

```bash
dynamodb-local    | Initializing DynamoDB Local with the following configuration:
dynamodb-local    | Port:       8000
dynamodb-local    | InMemory:   false
dynamodb-local    | DbPath:     ./data
dynamodb-local    | SharedDb:   true
dynamodb-local    | shouldDelayTransientStatuses:       false
dynamodb-local    | CorsParams: *
dynamodb-local    | 
```

To quickly shut down or stop the server, press:
```
Ctrl + C 
```

## Insert Data Into Your DynamoDB Server

Before we can perform requests to the database from inside Node.js, we need to insert data. To do that, we're going to leverage AWS NoSQL Workbench GUI and connect to our recently launched DynamobDB server.

### Choose a Model
- First, open/launch the AWS NoSQL Workbench.
- On the left sidebar, locate and click on **Visualizer**.
- Click on the dropdown menu and select **AWS Discussion Forum Data Model**.

![aws workbench](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhlqc156v6u654w2h4sc.png)

This model includes three tables for you to explore: Forum, Reply, and Thread. 

- Next, click on the **Commit to Amazon DynamoDB** button

![connect](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yznz8sxwt8jcj7b0k82e.png)

- When the popup opens, click on the tab furthest to the right that says **Add a new DynamoDB local connection**.

- Then, click on the **Commit to connect to your local database**. 

- You can choose whatever name you want for **Connection name** field.

![local connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lpg22rhkt9fjyqcah25t.png)

This step does three things. First, it creates a connection, inserts all three tables into your database, and opens a new window or view, called **Operation builder**, that displays all your databases. 

- Finally, Click the **Open** button and Voilà!

![Open](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0trynn7xgs1kldivt0y.png)


Now we're ready to make requests to the database in Node.js!

## Query Data With Node.js

To query data from DynamoDB in your Node.js project, you use the AWS JavaScript SDK for Node.js. So, Let's get to it...

Open your preferred terminal app, create a directory, change directories, and initialize it to install npm packages. We can do this in one swoop like so:

```bash
$ cd ~/ && mkdir my-project && cd my-project && npm init -y
```
While we're here, install DynamoDB specific packages by typing the following command:
```bash
$ npm i @aws-sdk/client-dynamodb
```
Next, create a file called index.js :
```bash
$ touch index.js
```
Copy-paste the following code into the index.js file:

```javascript
// index.js
const { QueryCommand } = require('@aws-sdk/client-dynamodb');
const REGION = "eu-west-2"; //e.g. "us-east-1"

// Create an Amazon DynamoDB service client object.
const ddbClient = new DynamoDBClient({
    region: REGION,
    endpoint: 'http://localhost:8000'
});
// Query Parameter to retrieve data from DynamoDB
const params = {
    TableName: "Forum",
    ScanIndexForward: true,
    ConsistentRead: false,
    KeyConditionExpression: "#0b290 = :0b290",
    ExpressionAttributeValues: {
        // Attribute Name
        ":0b290": {
            "S": "AWS Data Pipeline"
        }
    },
    "ExpressionAttributeNames": {
        // Partition key
        "#0b290": "ForumName" 
    }
};

// a "run" function 
const run = async () => {
    try {
        const data = await ddbClient.send(new QueryCommand(params));
        data.Items.forEach(function (element, index, array) {
            // Display data in the console
            console.log(element);            
            return data;
        });
    } catch (err) {
        console.error(err);
    }
};

run();

```

Now, let's see if this works. First, open a terminal and type the following command to execute the run() function, like so:
```
$ node index.js
```
Your output should look like this:
```bash
{
  Threads: { N: '19' },
  Category: { S: 'Amazon Web Services' },
  Messages: { N: '9' },
  Views: { N: '500' },
  ForumName: { S: 'AWS Data Pipeline' }
}
```

Phew! If you've gotten this far, you're a champ! AND you're pretty serious about AWS Cloud Engineering and Development. Hats off to you!

There was a lot to unpack in such a short time. However, you can deep dive into some of these practical solutions by jumping on over to the AWS Developer documentation.

- [Getting Started with DynamoDB](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStartedDynamoDB.html)
- [Getting Started with DynamoDB and AWS SDKs](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.html)

Thank you for sticking around and holding on to the end.

Until next time!