Publish property value updates to Amazon DynamoDB
This tutorial introduces a convenient way to store your data by using Amazon DynamoDB
In this tutorial, you build on the AWS IoT SiteWise demo that provides a sample set of data for a wind farm. You configure property value updates from the wind farm demo to send data, through AWS IoT Core rules, to a DynamoDB table that you create. When you enable property value updates, AWS IoT SiteWise sends your data to AWS IoT Core in MQTT messages. Then, define AWS IoT Core rules that perform actions, such as the DynamoDB action, depending on the contents of those messages. For more information, see Interact with other AWS services.
Topics
Prerequisites
To complete this tutorial, you need the following:
-
An AWS account. If you don't have one, see Set up an AWS account.
-
A development computer running Windows, macOS, Linux, or Unix to access the AWS Management Console. For more information, see Getting Started with the AWS Management Console.
-
An IAM user with administrator permissions.
-
A running AWS IoT SiteWise wind farm demo. When you set up the demo, it defines models and assets in AWS IoT SiteWise and streams data to them to represent a wind farm. For more information, see Use the AWS IoT SiteWise demo.
Step 1: Configure AWS IoT SiteWise to publish property value updates
In this procedure, you enable property value notifications on your demo turbine assets' Wind Speed properties. After you enable property value notifications, AWS IoT SiteWise publishes each value update in an MQTT message to AWS IoT Core.
To enable property value update notifications on asset properties
-
Sign in to the AWS IoT SiteWise console
. -
Review the AWS IoT SiteWise endpoints and quotas where AWS IoT SiteWise is supported and switch AWS Regions, if necessary. Switch to a Region where you're running the AWS IoT SiteWise demo.
-
In the left navigation pane, choose Assets.
-
Choose the arrow next to Demo Wind Farm Asset to expand the wind farm asset's hierarchy.
-
Choose a demo turbine and choose Edit.
-
Update the Wind Speed property's Notification status to ENABLED.
-
Choose Save asset at the bottom of the page.
-
Repeat steps 5 through 7 for each demo turbine asset.
-
Choose a demo turbine (for example, Demo Turbine Asset 1).
-
Choose Measurements.
-
Choose the copy icon next to the Wind Speed property to copy the notification topic to your clipboard. Save the notification topic to use later in this tutorial. You only need to record the notification topic from one turbine.
The notification topic should look like the following example.
$aws/sitewise/asset-models/
a1b2c3d4-5678-90ab-cdef-11111EXAMPLE
/assets/a1b2c3d4-5678-90ab-cdef-22222EXAMPLE
/properties/a1b2c3d4-5678-90ab-cdef-33333EXAMPLE
Step 2: Create a rule in AWS IoT Core
In this procedure, you create a rule in AWS IoT Core that parses the property value notification messages and inserts data into an Amazon DynamoDB table. AWS IoT Core rules parse MQTT messages and perform actions based on the contents and topic of each message. Then, you create a rule with a DynamoDB action to insert data to a DynamoDB table that you create as part of this tutorial.
To create a rule with a DynamoDB action
-
Navigate to the AWS IoT console
. If a Get started button appears, choose it. -
In the left navigation pane, choose Act and then choose Rules.
-
If a You don't have any rules yet dialog box appears, choose Create a rule. Otherwise, choose Create.
-
Enter a name and description for the rule.
-
Find the notification topic that you saved earlier in this tutorial.
$aws/sitewise/asset-models/
a1b2c3d4-5678-90ab-cdef-11111EXAMPLE
/assets/a1b2c3d4-5678-90ab-cdef-22222EXAMPLE
/properties/a1b2c3d4-5678-90ab-cdef-33333EXAMPLE
Replace the asset ID (the ID after
assets/
) in the topic with a+
. This selects the wind speed property for all demo wind turbine assets. The+
topic filter accepts all nodes from a single level in a topic. Your topic should look like the following example.$aws/sitewise/asset-models/
a1b2c3d4-5678-90ab-cdef-11111EXAMPLE
/assets/+/properties/a1b2c3d4-5678-90ab-cdef-33333EXAMPLE
-
Enter the following rule query statement. Replace the topic in the
FROM
section with your notification topic.SELECT payload.assetId AS asset, (SELECT VALUE (value.doubleValue) FROM payload.values) AS windspeed, timestamp() AS timestamp FROM '$aws/sitewise/asset-models/
a1b2c3d4-5678-90ab-cdef-11111EXAMPLE
/assets/+/properties/a1b2c3d4-5678-90ab-cdef-33333EXAMPLE
' WHERE type = 'PropertyValueUpdate' -
Under Set one or more actions, choose Add action.
-
On the Select an action page, choose Split message into multiple columns of a DynamoDB table (DynamoDBv2).
-
Choose Configure action at the bottom of the page.
-
On the Configure action page, choose Create a new resource.
The DynamoDB console opens in a new tab. Keep the rule action tab open while you complete the following procedures.
Step 3: Create a DynamoDB table
In this procedure, you create an Amazon DynamoDB table to receive wind speed data from the rule action.
To create a DynamoDB table
-
In the DynamoDB console dashboard, choose Create table.
-
Enter a name for your table.
-
For Primary key, do the following:
-
Enter
timestamp
as the partition key. -
Choose the Number type.
-
Select the Add sort key check box.
-
Enter
asset
as the sort key, and leave the default sort key type of String.
-
-
Choose Create.
When the Table is being created notice disappears, your table is ready.
-
Return to the tab with the Configure action page. Keep the DynamoDB tab open while you complete the following procedures.
Step 4: Configure the DynamoDB rule action
In this procedure, you configure the Amazon DynamoDB rule action to insert data from property value updates to your new DynamoDB table.
To configure the DynamoDB rule action
-
On the Configure action page, refresh the Table name list, and choose your new DynamoDB table.
-
Choose Create role to create an IAM role that grants AWS IoT Core access to perform the rule action.
-
Enter a role name and choose Create role.
-
Choose Add action.
-
Choose Create rule at the bottom of the page to finish creating the rule.
Your demo asset data should start appearing in your DynamoDB table.
Step 5: Explore data in DynamoDB
In this procedure, you explore the demo assets' wind speed data in your new Amazon DynamoDB table.
To explore asset data in DynamoDB
-
Return to the tab with the DynamoDB table open.
-
In the table you created earlier, choose the Items tab to view the data in the table. Refresh the page if you don't see rows in the table. If rows don't appear after a few minutes, see Troubleshoot a rule (DynamoDB).
-
In a row in the table, choose the edit icon to expand the data.
-
Choose the arrow next to the windspeed structure to expand the list of wind speed data points. Each list reflects a batch of wind speed data points sent to AWS IoT SiteWise by the wind farm demo. You might want a different data format if you set up a rule action for your own use. For more information, see Query asset property notification messages.
Now that you have completed the tutorial, disable or delete the rule and delete your DynamoDB table to avoid incurring additional charges. To clean up your resources, see Step 6: Clean up resources after the tutorial.
Step 6: Clean up resources after the tutorial
After you complete the tutorial, clean up your resources to avoid incurring additional charges. Your demo wind farm assets are deleted at the end of the duration that you chose when you created the demo. You can also delete the demo manually. For more information, see Delete the AWS IoT SiteWise demo.
Use the following procedures to disable property value update notifications (if you didn't delete the demo), disable or delete your AWS IoT rule, and delete your DynamoDB table.
To disable property value update notifications on asset properties
-
Navigate to the AWS IoT SiteWise console
. -
In the left navigation pane, choose Assets.
-
Choose the arrow next to Demo Wind Farm Asset to expand the wind farm asset's hierarchy.
-
Choose a demo turbine and choose Edit.
-
Update the Wind Speed property's Notification status to DISABLED.
-
Choose Save asset at the bottom of the page.
-
Repeat steps 4 through 6 for each demo turbine asset.
To disable or delete a rule in AWS IoT Core
-
Navigate to the AWS IoT console
. -
In the left navigation pane, choose Act and then choose Rules.
-
Choose the menu on your rule and choose Disable or Delete.
To delete a DynamoDB table
-
Navigate to the DynamoDB console
. -
In the left navigation pane, choose Tables.
-
Choose the table you created earlier, WindSpeedData.
-
Choose Delete table.
-
In the Delete table dialog, choose Delete.