Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(iot-actions): add support for DynamoDBv2 rule #20171

Merged
merged 21 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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