# 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
There's no need to create a DynamoDB database, you do need to create a table. Here’s how you can do it:
- Log into the AWS Management Console and navigate to the DynamoDB section.
- Click on Create table.
- 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.
- We will keep the default settings.
- Finally, click on Create to establish your table.
# Configuring IAM for DynamoDB
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:
- Go to IAM in the AWS Console.
- Create a new role and give a name.
- For permission options select Attach Policies Directly.
- Search for the policy AmazonDynamoDBFullAccess and select Next.
- For finer control, customize the policy to restrict access as needed.
- Lastly click Create User.
# Generate Access Keys:
- Inside the IAM console select Users.
- Select the newly created user.
- Navigate to Security credentials.
- Navigate to Access Keys and select Create access key.
- Select Application running outside AWS and click Next.
- Give description tag if desired and click Create access Key.
- Save the Access ID and Secret Key for use later.
# Working with DynamoDB: Use Case Flows
# Prerequisites
Please install the following nodes:
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.
- Configure the AWS DyanamoDB node by editing the node.
- Click the pencil to create a new config.
- Give it a name.
- Input the region where your DynamoDB is located. (e.g. us-east-1)
- Input Access ID and Secrete key provide in the step above "Configuring IAM for DynamoDB."
- Click Confirm.
- Trigger the inject node and confirm you don't have any errors.
- The Debug node can confirm the data is stored correctly.
- The data structure will look similar to the image below.
# 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:
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.
# 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.
# 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.