Skip to content

Commit 84b23cb

Browse files
authored
feat(iot-actions-alpha): open search action in IoT topic rule (#28748)
The CDK has no support to writes data to an Amazon OpenSearch Service via AWS IoT Rule. Adding the action that writes data to an Amazon OpenSearch Service to AWS IoT topic rule. OpenSearch action for IoT Topic Rule documentation is here: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-iot-topicrule-opensearchaction.html. Closes #17702. Tested by deploying my own stack and confirmed that publishing a message to IoT topic rule will successfully write data to open search service. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent a238590 commit 84b23cb

14 files changed

+1435
-16
lines changed

packages/@aws-cdk/aws-iot-actions-alpha/README.md

+37-16
Original file line numberDiff line numberDiff line change
@@ -355,20 +355,41 @@ to an HTTPS endpoint when it is triggered:
355355

356356
```ts
357357
const topicRule = new iot.TopicRule(this, 'TopicRule', {
358-
sql: iot.IotSql.fromStringAsVer20160323(
359-
"SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'",
360-
),
361-
});
362-
363-
topicRule.addAction(
364-
new actions.HttpsAction('https://example.com/endpoint', {
365-
confirmationUrl: 'https://example.com',
366-
headers: [
367-
{ key: 'key0', value: 'value0' },
368-
{ key: 'key1', value: 'value1' },
369-
],
370-
auth: { serviceName: 'serviceName', signingRegion: 'us-east-1' },
371-
}),
372-
);
373-
}
358+
sql: iot.IotSql.fromStringAsVer20160323(
359+
"SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'",
360+
),
361+
});
362+
363+
topicRule.addAction(
364+
new actions.HttpsAction('https://example.com/endpoint', {
365+
confirmationUrl: 'https://example.com',
366+
headers: [
367+
{ key: 'key0', value: 'value0' },
368+
{ key: 'key1', value: 'value1' },
369+
],
370+
auth: { serviceName: 'serviceName', signingRegion: 'us-east-1' },
371+
}),
372+
);
373+
```
374+
375+
## Write Data to Open Search Service
376+
377+
The code snippet below creates an AWS IoT Rule that writes data
378+
to an Open Search Service when it is triggered:
379+
380+
```ts
381+
import * as opensearch from 'aws-cdk-lib/aws-opensearchservice';
382+
declare const domain: opensearch.Domain;
383+
384+
const topicRule = new iot.TopicRule(this, 'TopicRule', {
385+
sql: iot.IotSql.fromStringAsVer20160323(
386+
"SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'",
387+
),
388+
});
389+
390+
topicRule.addAction(new actions.OpenSearchAction(domain, {
391+
id: 'my-id',
392+
index: 'my-index',
393+
type: 'my-type',
394+
}));
374395
```

packages/@aws-cdk/aws-iot-actions-alpha/lib/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export * from './iotevents-put-message-action';
88
export * from './iot-republish-action';
99
export * from './kinesis-put-record-action';
1010
export * from './lambda-function-action';
11+
export * from './open-search-action';
1112
export * from './s3-put-object-action';
1213
export * from './sqs-queue-action';
1314
export * from './sns-topic-action';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import * as iam from 'aws-cdk-lib/aws-iam';
2+
import * as iot from '@aws-cdk/aws-iot-alpha';
3+
import * as opensearch from 'aws-cdk-lib/aws-opensearchservice';
4+
import { CommonActionProps } from './common-action-props';
5+
import { singletonActionRole } from './private/role';
6+
7+
/**
8+
* Configuration properties of an action for Open Search.
9+
*/
10+
export interface OpenSearchActionProps extends CommonActionProps {
11+
/**
12+
* The unique identifier for the document you are storing.
13+
*/
14+
readonly id: string;
15+
16+
/**
17+
* The OpenSearch index where you want to store your data.
18+
*/
19+
readonly index: string;
20+
21+
/**
22+
* The type of document you are storing.
23+
*/
24+
readonly type: string;
25+
}
26+
27+
/**
28+
* The action to write data to an Amazon OpenSearch Service domain.
29+
*/
30+
export class OpenSearchAction implements iot.IAction {
31+
constructor(private readonly domain: opensearch.Domain, private readonly props: OpenSearchActionProps) {
32+
}
33+
34+
/**
35+
* @internal
36+
*/
37+
public _bind(rule: iot.ITopicRule): iot.ActionConfig {
38+
const role = this.props.role ?? singletonActionRole(rule);
39+
40+
// According to CloudFormation documentation, we only need 'es:ESHttpPut' permission
41+
// https://docs.aws.amazon.com/iot/latest/developerguide/opensearch-rule-action.html#opensearch-rule-action-requirements
42+
role.addToPrincipalPolicy(new iam.PolicyStatement({
43+
actions: ['es:ESHttpPut'],
44+
resources: [this.domain.domainArn, `${this.domain.domainArn}/*`],
45+
}));
46+
47+
return {
48+
configuration: {
49+
openSearch: {
50+
endpoint: `https://${this.domain.domainEndpoint}`,
51+
id: this.props.id,
52+
index: this.props.index,
53+
type: this.props.type,
54+
roleArn: role.roleArn,
55+
},
56+
},
57+
};
58+
}
59+
}

packages/@aws-cdk/aws-iot-actions-alpha/test/opensearch/integ.open-search-action.js.snapshot/asset.1b474110b3f79050ba8693912584a5d5bac8c7e94d63afa8c73f1d087444e7cc/index.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-iot-actions-alpha/test/opensearch/integ.open-search-action.js.snapshot/aws-iot-opensearch-integ-stack.assets.json

+32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)