From ce74fe7a80c3c9ec0bfba98e7dc2aa9e6bfacf75 Mon Sep 17 00:00:00 2001 From: Tatsuya Yamamoto Date: Fri, 5 Nov 2021 07:51:11 +0900 Subject: [PATCH] feat(iot): allow setting `errorAction` of TopicRule (#17287) I'm trying to implement aws-iot L2 Constructs. This PR is one of steps after following PR: - https://github.com/aws/aws-cdk/pull/16681#issuecomment-942233029 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- 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..402036b531dfc 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 will 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://docs.aws.amazon.com/cdk/api/latest/docs/aws-iot-actions-readme.html) 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..89f40c3797d97 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 performed + */ + 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();