From abb1d1f44d9a9922a08d8a407ad651a5eb80ba46 Mon Sep 17 00:00:00 2001 From: yamatatsu Date: Wed, 3 Nov 2021 11:35:28 +0900 Subject: [PATCH 1/5] feat(iot): allow setting `errorAction` of TopicRule 1. add properties `errorAction` 2. add tests 3. add lines to README --- packages/@aws-cdk/aws-iot/README.md | 18 +++++++++++++ packages/@aws-cdk/aws-iot/lib/topic-rule.ts | 8 ++++++ .../@aws-cdk/aws-iot/test/topic-rule.test.ts | 25 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/packages/@aws-cdk/aws-iot/README.md b/packages/@aws-cdk/aws-iot/README.md index 42ab651f26f81..925e48acb3b3e 100644 --- a/packages/@aws-cdk/aws-iot/README.md +++ b/packages/@aws-cdk/aws-iot/README.md @@ -75,6 +75,22 @@ const topicRule = new iot.TopicRule(this, 'TopicRule', { topicRule.addAction(new actions.LambdaFunctionAction(func)) ``` +You can also supply `errorAction` as following, and the IoT Rule trigger it +if a rule's action is unable to perform: + +```ts +import * as iot from '@aws-cdk/aws-iot'; +import * as actions from '@aws-cdk/aws-iot-actions'; +import * as logs from '@aws-cdk/aws-logs'; + +const logGroup = new logs.LogGroup(this, 'MyLogGroup') + +new iot.TopicRule(this, 'TopicRule', { + sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp FROM 'device/+/data'"), + errorAction: new actions.CloudWatchLogsAction(logGroup), +}); +``` + If you wanna make the topic rule disable, add property `enabled: false` as following: ```ts @@ -83,3 +99,5 @@ new iot.TopicRule(this, 'TopicRule', { enabled: false, }); ``` + +See also [@aws-cdk/aws-iot-actions](https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-iot-actions/README.md) for other actions. diff --git a/packages/@aws-cdk/aws-iot/lib/topic-rule.ts b/packages/@aws-cdk/aws-iot/lib/topic-rule.ts index 8860a611e4af3..e8b7da61d7c2c 100644 --- a/packages/@aws-cdk/aws-iot/lib/topic-rule.ts +++ b/packages/@aws-cdk/aws-iot/lib/topic-rule.ts @@ -48,6 +48,13 @@ export interface TopicRuleProps { */ readonly description?: string; + /** + * The action AWS IoT performs when it is unable to perform a rule's action. + * + * @default No action will be perform + */ + readonly errorAction?: IAction; + /** * Specifies whether the rule is enabled. * @@ -117,6 +124,7 @@ export class TopicRule extends Resource implements ITopicRule { actions: Lazy.any({ produce: () => this.actions }), awsIotSqlVersion: sqlConfig.awsIotSqlVersion, description: props.description, + errorAction: props.errorAction?.bind(this).configuration, ruleDisabled: props.enabled === undefined ? undefined : !props.enabled, sql: sqlConfig.sql, }, diff --git a/packages/@aws-cdk/aws-iot/test/topic-rule.test.ts b/packages/@aws-cdk/aws-iot/test/topic-rule.test.ts index 6e4a49c234e60..5f84201a37e5d 100644 --- a/packages/@aws-cdk/aws-iot/test/topic-rule.test.ts +++ b/packages/@aws-cdk/aws-iot/test/topic-rule.test.ts @@ -233,6 +233,31 @@ test('cannot add an action that have multiple keys', () => { }).toThrow('An action property cannot have multiple keys, received: http,lambda'); }); +test('can set errorAction', () => { + const stack = new cdk.Stack(); + + const action: iot.IAction = { + bind: () => ({ + configuration: { + http: { url: 'http://example.com' }, + }, + }), + }; + + new iot.TopicRule(stack, 'MyTopicRule', { + errorAction: action, + sql: iot.IotSql.fromStringAsVer20151008("SELECT topic(2) as device_id, temperature FROM 'device/+/data'"), + }); + + Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', { + TopicRulePayload: { + ErrorAction: { + Http: { Url: 'http://example.com' }, + }, + }, + }); +}); + test('can import a TopicRule by ARN', () => { const stack = new cdk.Stack(); From 7f8ea26e822813d2ac28a6549f35c4d81ee6c060 Mon Sep 17 00:00:00 2001 From: Tatsuya Yamamoto Date: Thu, 4 Nov 2021 09:20:37 +0900 Subject: [PATCH 2/5] Update packages/@aws-cdk/aws-iot/lib/topic-rule.ts Co-authored-by: Adam Ruka --- packages/@aws-cdk/aws-iot/lib/topic-rule.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-iot/lib/topic-rule.ts b/packages/@aws-cdk/aws-iot/lib/topic-rule.ts index e8b7da61d7c2c..89f40c3797d97 100644 --- a/packages/@aws-cdk/aws-iot/lib/topic-rule.ts +++ b/packages/@aws-cdk/aws-iot/lib/topic-rule.ts @@ -51,7 +51,7 @@ export interface TopicRuleProps { /** * The action AWS IoT performs when it is unable to perform a rule's action. * - * @default No action will be perform + * @default - no action will be performed */ readonly errorAction?: IAction; From b32287d1944b8ca93b9848d2b70fea0c1f3186bb Mon Sep 17 00:00:00 2001 From: Tatsuya Yamamoto Date: Thu, 4 Nov 2021 09:21:03 +0900 Subject: [PATCH 3/5] Update packages/@aws-cdk/aws-iot/README.md Co-authored-by: Adam Ruka --- packages/@aws-cdk/aws-iot/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-iot/README.md b/packages/@aws-cdk/aws-iot/README.md index 925e48acb3b3e..c6fe3b3bf17fe 100644 --- a/packages/@aws-cdk/aws-iot/README.md +++ b/packages/@aws-cdk/aws-iot/README.md @@ -83,7 +83,7 @@ import * as iot from '@aws-cdk/aws-iot'; import * as actions from '@aws-cdk/aws-iot-actions'; import * as logs from '@aws-cdk/aws-logs'; -const logGroup = new logs.LogGroup(this, 'MyLogGroup') +const logGroup = new logs.LogGroup(this, 'MyLogGroup'); new iot.TopicRule(this, 'TopicRule', { sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp FROM 'device/+/data'"), From a22c9b52aeb29275d3d79b5842a4438dfe3d3e53 Mon Sep 17 00:00:00 2001 From: Tatsuya Yamamoto Date: Thu, 4 Nov 2021 09:21:50 +0900 Subject: [PATCH 4/5] Update packages/@aws-cdk/aws-iot/README.md Co-authored-by: Adam Ruka --- packages/@aws-cdk/aws-iot/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-iot/README.md b/packages/@aws-cdk/aws-iot/README.md index c6fe3b3bf17fe..143a9418096df 100644 --- a/packages/@aws-cdk/aws-iot/README.md +++ b/packages/@aws-cdk/aws-iot/README.md @@ -75,8 +75,8 @@ const topicRule = new iot.TopicRule(this, 'TopicRule', { topicRule.addAction(new actions.LambdaFunctionAction(func)) ``` -You can also supply `errorAction` as following, and the IoT Rule trigger it -if a rule's action is unable to perform: +You can also supply `errorAction` as following, +and the IoT Rule will trigger it if a rule's action is unable to perform: ```ts import * as iot from '@aws-cdk/aws-iot'; From fe3cd51eb41542aaa68eee17c289da5e91ae59b9 Mon Sep 17 00:00:00 2001 From: yamatatsu Date: Thu, 4 Nov 2021 09:26:14 +0900 Subject: [PATCH 5/5] doc(iot): use use a link to API reference instead of GitHub --- packages/@aws-cdk/aws-iot/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-iot/README.md b/packages/@aws-cdk/aws-iot/README.md index 143a9418096df..402036b531dfc 100644 --- a/packages/@aws-cdk/aws-iot/README.md +++ b/packages/@aws-cdk/aws-iot/README.md @@ -100,4 +100,4 @@ new iot.TopicRule(this, 'TopicRule', { }); ``` -See also [@aws-cdk/aws-iot-actions](https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-iot-actions/README.md) for other actions. +See also [@aws-cdk/aws-iot-actions](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-iot-actions-readme.html) for other actions.