Using DynamoDB with Node-RED

Amazon's DynamoDB is a fully managed NoSQL database service known for its fast and predictable performance and scalable design. This makes it suitable for applications needing low-latency responses. In this documentation, we’ll look at how to set up and use DynamoDB, configure the necessary IAM roles, and apply Node-RED flows to store and retrieve data effectively.

Setting Up DynamoDB

Creating a DynamoDB Table

AWS DynamoDB with Node-RED

There's no need to create a DynamoDB database, you do need to create a table. Here’s how you can do it:

  1. Log into the AWS Management Console and navigate to the DynamoDB section.
  2. Click on Create table.
  3. For the table name, enter a table name. We will use FlowFuse-Email for this demonstration. For the partition key, use email with type String.
  4. We will keep the default settings.
  5. Finally, click on Create to establish your table.

Configuring IAM for DynamoDB

AWS IAM DynamoDB with Node-RED

When working with Amazon DynamoDB, it’s crucial to ensure that the right entities have the appropriate permissions to perform actions on your database. This is where AWS Identity and Access Management (IAM) plays an important role. By properly configuring IAM, you ensure that your data is not only secure from unauthorized access but also that the correct roles and users have the precise level of access needed, avoiding any unnecessary permissions that could lead to security risks.

Create a New IAM Role with Necessary Policies:

  1. Go to IAM in the AWS Console.
  2. Create a new role and give a name.
  3. For permission options select Attach Policies Directly.
  4. Search for the policy AmazonDynamoDBFullAccess and select Next.
  5. For finer control, customize the policy to restrict access as needed.
  6. Lastly click Create User.

Generate Access Keys:

  1. Inside the IAM console select Users.
  2. Select the newly created user.
  3. Navigate to Security credentials.
  4. Navigate to Access Keys and select Create access key.
  5. Select Application running outside AWS and click Next.
  6. Give description tag if desired and click Create access Key.
  7. Save the Access ID and Secret Key for use later.

Working with DynamoDB: Use Case Flows

DynamoDB Flow Node-RED

Prerequisites

Please install the following nodes:

  1. node-red-contrib-aws
  2. node-red-node-data-generator

Now, let's dive into some practical examples using Node-RED to manage a customer list:

Inserting Data into DynamoDB

This first flow will generate data and send it to DynamoDB via the PutItem operation.

  1. Configure the AWS DyanamoDB node by editing the node.
  2. Click the pencil to create a new config.
  3. Give it a name.
  4. Input the region where your DynamoDB is located. (e.g. us-east-1)
  5. Input Access ID and Secrete key provide in the step above "Configuring IAM for DynamoDB."
  6. Click Confirm.
  7. Trigger the inject node and confirm you don't have any errors.
  8. The Debug node can confirm the data is stored correctly.
  9. The data structure will look similar to the image below.

DynamoDB Flow Node-RED

Retrieving All Data for a specific Partition Key

The GetItem operation fetches data, which you can then display using debug nodes. To retrieve specific customer information based on their email import this flow:

Dynamodb Get Item flowfuse

The function node specifies the TableName and Partition key to search against.

Querying Data

When you need to find items under specific criteria, set up your query with partition keys and conditions. The Query operation allows you to efficiently retrieve data without scanning the entire contents of a particular partition key. If a key has significant about of data, but only need one particular value. This would then be the ideal path.

In this example we are retrieving only name and temp from the matching partition key match.

DynamoDB Query Node-RED

Scanning Data

While scanning is available, it should be used sparingly due to its high demand on resources, especially in large databases. Use it when necessary prioritize using query for regular operations.

DynamoDB Scan Node-RED

Conclusion

Leveraging DynamoDB in your production environment can streamline how you manage recipes, control quality, and monitor production processes. Its fast data handling capabilities ensure that you can react quickly to changes in production conditions and maintain high standards of operation.

Full Flow