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-actions-alpha): open search action in IoT topic rule #28748

Merged
merged 8 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions packages/@aws-cdk/aws-iot-actions-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,20 +355,41 @@ to an HTTPS endpoint when it is triggered:

```ts
const topicRule = new iot.TopicRule(this, 'TopicRule', {
sql: iot.IotSql.fromStringAsVer20160323(
"SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'",
),
});

topicRule.addAction(
new actions.HttpsAction('https://example.com/endpoint', {
confirmationUrl: 'https://example.com',
headers: [
{ key: 'key0', value: 'value0' },
{ key: 'key1', value: 'value1' },
],
auth: { serviceName: 'serviceName', signingRegion: 'us-east-1' },
}),
);
}
sql: iot.IotSql.fromStringAsVer20160323(
"SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'",
),
});

topicRule.addAction(
new actions.HttpsAction('https://example.com/endpoint', {
confirmationUrl: 'https://example.com',
headers: [
{ key: 'key0', value: 'value0' },
{ key: 'key1', value: 'value1' },
],
auth: { serviceName: 'serviceName', signingRegion: 'us-east-1' },
}),
);
```

## Write Data to Open Search Service

The code snippet below creates an AWS IoT Rule that writes data
to an Open Search Service when it is triggered:

```ts
import * as opensearch from 'aws-cdk-lib/aws-opensearchservice';
declare const domain: opensearch.Domain;

const topicRule = new iot.TopicRule(this, 'TopicRule', {
sql: iot.IotSql.fromStringAsVer20160323(
"SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'",
),
});

topicRule.addAction(new actions.OpenSearchAction(domain, {
id: 'my-id',
index: 'my-index',
type: 'my-type',
}));
```
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-iot-actions-alpha/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './iotevents-put-message-action';
export * from './iot-republish-action';
export * from './kinesis-put-record-action';
export * from './lambda-function-action';
export * from './open-search-action';
export * from './s3-put-object-action';
export * from './sqs-queue-action';
export * from './sns-topic-action';
Expand Down
59 changes: 59 additions & 0 deletions packages/@aws-cdk/aws-iot-actions-alpha/lib/open-search-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import * as iam from 'aws-cdk-lib/aws-iam';
import * as iot from '@aws-cdk/aws-iot-alpha';
import * as opensearch from 'aws-cdk-lib/aws-opensearchservice';
import { CommonActionProps } from './common-action-props';
import { singletonActionRole } from './private/role';

/**
* Configuration properties of an action for Open Search.
*/
export interface OpenSearchActionProps extends CommonActionProps {
/**
* The unique identifier for the document you are storing.
*/
readonly id: string;

/**
* The OpenSearch index where you want to store your data.
*/
readonly index: string;

/**
* The type of document you are storing.
*/
readonly type: string;
}
aaythapa marked this conversation as resolved.
Show resolved Hide resolved

/**
* The action to write data to an Amazon OpenSearch Service domain.
*/
export class OpenSearchAction implements iot.IAction {
constructor(private readonly domain: opensearch.Domain, private readonly props: OpenSearchActionProps) {
aaythapa marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @internal
*/
public _bind(rule: iot.ITopicRule): iot.ActionConfig {
const role = this.props.role ?? singletonActionRole(rule);

// According to CloudFormation documentation, we only need 'es:ESHttpPut' permission
// https://docs.aws.amazon.com/iot/latest/developerguide/opensearch-rule-action.html#opensearch-rule-action-requirements
role.addToPrincipalPolicy(new iam.PolicyStatement({
actions: ['es:ESHttpPut'],
resources: [this.domain.domainArn, `${this.domain.domainArn}/*`],
}));

return {
configuration: {
openSearch: {
endpoint: `https://${this.domain.domainEndpoint}`,
id: this.props.id,
index: this.props.index,
type: this.props.type,
roleArn: role.roleArn,
},
},
};
}
}

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading