Skip to content

Commit

Permalink
feat(iot): allow setting errorAction of TopicRule (aws#17287)
Browse files Browse the repository at this point in the history
I'm trying to implement aws-iot L2 Constructs.

This PR is one of steps after following PR: 
- aws#16681 (comment)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
yamatatsu authored and TikiTDO committed Feb 21, 2022
1 parent 2648e70 commit ce74fe7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-iot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-iot/lib/topic-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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,
},
Expand Down
25 changes: 25 additions & 0 deletions packages/@aws-cdk/aws-iot/test/topic-rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit ce74fe7

Please sign in to comment.