Skip to content

Commit

Permalink
feat(iot-actions): add support for DynamoDBv2 rule (#20171)
Browse files Browse the repository at this point in the history
This merge request adds the dynamodbv2 IoT rule action as a new feature. This rule is useful for quickly logging device information into DynamoDB. Furthermore, it adds some other important functionality as IoT action.

Closes #20162

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
bobveringa authored Aug 2, 2022
1 parent c33f1ea commit a57dec3
Show file tree
Hide file tree
Showing 13 changed files with 806 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/@aws-cdk/aws-iot-actions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Currently supported are:
- Put records to Kinesis Data Firehose stream
- Send messages to SQS queues
- Publish messages on SNS topics
- Write messages into columns of DynamoDB

## Republish a message to another MQTT topic

Expand Down Expand Up @@ -278,3 +279,23 @@ const topicRule = new iot.TopicRule(this, 'TopicRule', {
],
});
```

## Write attributes of a message to DynamoDB

The code snippet below creates an AWS IoT rule that writes all or part of an
MQTT message to DynamoDB using the DynamoDBv2 action.

```ts
import * as dynamodb from '@aws-cdk/aws-dynamodb';

declare const table: dynamodb.Table;

const topicRule = new iot.TopicRule(this, 'TopicRule', {
sql: iot.IotSql.fromStringAsVer20160323(
"SELECT * FROM 'device/+/data'",
),
actions: [
new actions.DynamoDBv2PutItemAction(table)
],
});
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import * as iam from '@aws-cdk/aws-iam';
import * as iot from '@aws-cdk/aws-iot';
import { CommonActionProps } from './common-action-props';
import { singletonActionRole } from './private/role';

/**
* Configuration properties of an action for the dynamodb table.
*/
export interface DynamoDBv2PutItemActionProps extends CommonActionProps {
}

/**
* The action to put the record from an MQTT message to the DynamoDB table.
*/
export class DynamoDBv2PutItemAction implements iot.IAction {
private readonly role?: iam.IRole;

/**
* @param table the DynamoDB table in which to put the items.
* @param props Optional properties to not use default
*/
constructor(private readonly table: dynamodb.ITable, props: DynamoDBv2PutItemActionProps = {}) {
this.role = props.role;
}

bind(rule: iot.ITopicRule): iot.ActionConfig {
const role = this.role ?? singletonActionRole(rule);
role.addToPrincipalPolicy(new iam.PolicyStatement({
actions: ['dynamodb:PutItem'],
resources: [this.table.tableArn],
}));
return {
configuration: {
dynamoDBv2: {
putItem: {
tableName: this.table.tableName,
},
roleArn: role.roleArn,
},
},
};
}
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-iot-actions/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './cloudwatch-logs-action';
export * from './cloudwatch-put-metric-action';
export * from './cloudwatch-set-alarm-state-action';
export * from './common-action-props';
export * from './dynamodbv2-put-item-action';
export * from './firehose-put-record-action';
export * from './iot-republish-action';
export * from './kinesis-put-record-action';
Expand Down
173 changes: 173 additions & 0 deletions packages/@aws-cdk/aws-iot-actions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/@aws-cdk/aws-iot-actions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@
"@aws-cdk/aws-kinesisfirehose-destinations": "0.0.0",
"@aws-cdk/cdk-build-tools": "0.0.0",
"@aws-cdk/integ-runner": "0.0.0",
"@aws-cdk/integ-tests": "0.0.0",
"@aws-cdk/pkglint": "0.0.0",
"@types/jest": "^27.5.2",
"jest": "^27.5.1",
"constructs": "^10.0.0"
},
"dependencies": {
"@aws-cdk/aws-cloudwatch": "0.0.0",
"@aws-cdk/aws-dynamodb": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-iot": "0.0.0",
"@aws-cdk/aws-kinesis": "0.0.0",
Expand All @@ -104,6 +106,7 @@
"homepage": "https://github.com/aws/aws-cdk",
"peerDependencies": {
"@aws-cdk/aws-cloudwatch": "0.0.0",
"@aws-cdk/aws-dynamodb": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-iot": "0.0.0",
"@aws-cdk/aws-kinesis": "0.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"20.0.0"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "20.0.0",
"testCases": {
"dynamodbv2-integtest/DefaultTest": {
"stacks": [
"test-dynamodbv2-put-item-action-stack"
],
"assertionStack": "dynamodbv2integtestDefaultTestDeployAssertEF9A9A37"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"version": "20.0.0",
"artifacts": {
"Tree": {
"type": "cdk:tree",
"properties": {
"file": "tree.json"
}
},
"test-dynamodbv2-put-item-action-stack": {
"type": "aws:cloudformation:stack",
"environment": "aws://unknown-account/unknown-region",
"properties": {
"templateFile": "test-dynamodbv2-put-item-action-stack.template.json",
"validateOnSynth": false
},
"metadata": {
"/test-dynamodbv2-put-item-action-stack/TopicRule/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "TopicRule40A4EA44"
}
],
"/test-dynamodbv2-put-item-action-stack/TopicRule/TopicRuleActionRole/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "TopicRuleTopicRuleActionRole246C4F77"
}
],
"/test-dynamodbv2-put-item-action-stack/TopicRule/TopicRuleActionRole/DefaultPolicy/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "TopicRuleTopicRuleActionRoleDefaultPolicy99ADD687"
}
],
"/test-dynamodbv2-put-item-action-stack/MyTable": [
{
"type": "aws:cdk:hasPhysicalName",
"data": {
"Ref": "MyTable794EDED1"
}
}
],
"/test-dynamodbv2-put-item-action-stack/MyTable/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "MyTable794EDED1"
}
]
},
"displayName": "test-dynamodbv2-put-item-action-stack"
},
"dynamodbv2integtestDefaultTestDeployAssertEF9A9A37": {
"type": "aws:cloudformation:stack",
"environment": "aws://unknown-account/unknown-region",
"properties": {
"templateFile": "dynamodbv2integtestDefaultTestDeployAssertEF9A9A37.template.json",
"validateOnSynth": false
},
"displayName": "dynamodbv2-integtest/DefaultTest/DeployAssert"
}
}
}
Loading

0 comments on commit a57dec3

Please sign in to comment.