- Newest
- Most votes
- Most comments
The behavior you're experiencing is the default for AWS IoT rules when updating DynamoDB. However, there are ways to update only specific attributes without overwriting the entire item. Here's how you can modify your setup to achieve this:
-
Use UpdateItem instead of PutItem: By default, the IoT rule action uses PutItem, which overwrites the entire item. To update only specific attributes, you need to use UpdateItem.
-
Modify your IoT rule action: In the AWS IoT console or using the AWS CLI, modify your rule action to use UpdateItem. Here's an example of how to set this up:
{ "sql": "SELECT param1, param2, param3 FROM 'topic/test'", "ruleDisabled": false, "awsIotSqlVersion": "2016-03-23", "actions": [{ "dynamoDB": { "tableName": "YourTableName", "roleArn": "arn:aws:iam::your-account-id:role/your-role", "operation": "UPDATE", "hashKeyField": "your-hash-key", "hashKeyValue": "${topic()}", "hashKeyType": "STRING", "rangeKeyField": "your-range-key", "rangeKeyValue": "${timestamp()}", "rangeKeyType": "NUMBER", "payloadField": "payload" } }] } -
Use an UpdateExpression: In your DynamoDB action, include an UpdateExpression to specify which attributes to update:
"dynamoDBv2": { "roleName": "your-role", "putItem": { "tableName": "YourTableName", "conditionExpression": "attribute_exists(#pk)", "expressionAttributeNames": {"#pk": "your-primary-key"}, "updateExpression": "SET param1 = :p1, param2 = :p2, param3 = :p3" } } -
Use substitution templates: Use ${param1}, ${param2}, and ${param3} in your UpdateExpression to dynamically set the values from your IoT message.
-
Handle new items: If you want to insert a new item when it doesn't exist, you can use a conditional write:
"conditionExpression": "attribute_not_exists(#pk)", "expressionAttributeNames": {"#pk": "your-primary-key"} -
Consider using a Lambda function: For more complex updates or conditional logic, you can use a Lambda function as the rule action. This gives you full control over how the DynamoDB item is updated.
By implementing these changes, your IoT rule will update only the specified attributes (param1, param2, param3) in your DynamoDB table, preserving the values in other columns.
Remember to test your setup thoroughly to ensure it behaves as expected with different types of messages and scenarios.
Your JSON structure is close, but it needs some adjustments to work with AWS IoT SiteWise's batch data ingestion format. Here's a simple example that should work for buffered ingestion into SiteWise:
{ "entries": [ { "entryId": "1", "assetId": "12345678-90ab-cdef-1234-567890abcdef", "propertyValues": [ { "propertyId": "34567890-1bcd-ef12-3456-7890abcdef12", "propertyAlias": "/wind_turbine_1/temperature", "propertyValues": [ { "value": { "doubleValue": 74.2 }, "timestamp": { "timeInSeconds": 1695981000, "offsetInNanos": 0 }, "quality": "GOOD" } ] }, { "propertyId": "45678901-2cde-f123-4567-890abcdef123", "propertyAlias": "/wind_turbine_1/rotation_speed", "propertyValues": [ { "value": { "integerValue": 1540 }, "timestamp": { "timeInSeconds": 1695981000, "offsetInNanos": 0 }, "quality": "GOOD" } ] } ] } ] }
Key points about this structure:
-
The top-level key is
entries, notassets. -
Each entry has an
entryId, which is a unique identifier for the entry in the batch. -
assetIdis used instead ofassetModelIdandassetName. -
Property values are nested under
propertyValues. -
Each property can have multiple values, hence the array under
propertyValues. -
The
valuefield specifies the data type (doubleValuefor decimals,integerValuefor whole numbers). -
Timestamps are in Unix epoch format (seconds since 1970-01-01) with an optional nanosecond offset.
-
A
qualityfield is included for each value. -
You can use either
propertyIdorpropertyAliasto identify properties, but not both in the same entry.
This JSON structure should work for buffered ingestion into AWS IoT SiteWise when stored in S3. Remember to configure your SiteWise ingestion job to point to the S3 bucket and file containing this data.
Also, ensure that the asset and property IDs in your JSON match those in your SiteWise asset model. The timestamp in the example (1695981000) corresponds to 2023-09-29T10:10:00Z, matching your original timestamp.
Relevant content
- asked 3 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 3 months ago
