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): allow setting errorAction of TopicRule #17287

Merged
merged 8 commits into from Nov 4, 2021
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-iot/README.md
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 trigger it
if a rule's action is unable to perform:
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved

```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')
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved

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://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-iot-actions/README.md) for other actions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use a link to our API reference (https://docs.aws.amazon.com/cdk/api/latest/docs/aws-iot-actions-readme.html) instead of GitHub.

8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-iot/lib/topic-rule.ts
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 perform
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved
*/
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
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