-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(iot): support the TopicRule L2 construct
1. add The TopicRule L2 construct 2. add unit tests 3. add integration test 4. update package.json
- Loading branch information
Showing
51 changed files
with
4,853 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,32 @@ | ||
AWS Cloud Development Kit (AWS CDK) | ||
Copyright 2018-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
------------------------------------------------------------------------------- | ||
|
||
The AWS CDK includes the following third-party software/licensing: | ||
|
||
** case - https://www.npmjs.com/package/case | ||
Copyright (c) 2013 Nathan Bubna | ||
|
||
Permission is hereby granted, free of charge, to any person | ||
obtaining a copy of this software and associated documentation | ||
files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, | ||
copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following | ||
conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
---------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { CfnTopicRule } from './iot.generated'; | ||
import { ITopicRule } from './topic-rule-ref'; | ||
|
||
/** | ||
* An abstract action for TopicRule. | ||
*/ | ||
export interface IAction { | ||
/** | ||
* Returns the topic rule action specification. | ||
* | ||
* @param rule The TopicRule that would trigger this action. | ||
*/ | ||
bind(rule: ITopicRule): ActionConfig; | ||
} | ||
|
||
/** | ||
* Properties for an topic rule action | ||
*/ | ||
export interface ActionConfig { | ||
/** | ||
* An action to set state of an Amazon CloudWatch alarm. | ||
* @default None | ||
*/ | ||
readonly cloudwatchAlarm?: CfnTopicRule.CloudwatchAlarmActionProperty; | ||
/** | ||
* An action to send data to Amazon CloudWatch Logs. | ||
* @default None | ||
*/ | ||
readonly cloudwatchLogs?: CfnTopicRule.CloudwatchLogsActionProperty; | ||
/** | ||
* An action to capture an Amazon CloudWatch metric. | ||
* @default None | ||
*/ | ||
readonly cloudwatchMetric?: CfnTopicRule.CloudwatchMetricActionProperty; | ||
/** | ||
* An action to write all or part of an MQTT message to an Amazon DynamoDB table. | ||
* @default None | ||
*/ | ||
readonly dynamoDb?: CfnTopicRule.DynamoDBActionProperty; | ||
/** | ||
* An action to write all or part of an MQTT message to an Amazon DynamoDB table. | ||
* @default None | ||
*/ | ||
readonly dynamoDBv2?: CfnTopicRule.DynamoDBv2ActionProperty; | ||
/** | ||
* An action to write data from MQTT messages to an Amazon OpenSearch Service domain. | ||
* @default None | ||
*/ | ||
readonly elasticsearch?: CfnTopicRule.ElasticsearchActionProperty; | ||
/** | ||
* An action to send data from an MQTT message to an Amazon Kinesis Data Firehose stream. | ||
* @default None | ||
*/ | ||
readonly firehose?: CfnTopicRule.FirehoseActionProperty; | ||
/** | ||
* An action to send data from an MQTT message to a web application or service. | ||
* @default None | ||
*/ | ||
readonly http?: CfnTopicRule.HttpActionProperty; | ||
/** | ||
* An action to send data from an MQTT message to an AWS IoT Analytics channel. | ||
* @default None | ||
*/ | ||
readonly iotAnalytics?: CfnTopicRule.IotAnalyticsActionProperty; | ||
/** | ||
* An action to send data from an MQTT message to an AWS IoT Events input. | ||
* @default None | ||
*/ | ||
readonly iotEvents?: CfnTopicRule.IotEventsActionProperty; | ||
/** | ||
* An action to send data from an MQTT message to asset properties in AWS IoT SiteWise. | ||
* @default None | ||
*/ | ||
readonly iotSiteWise?: CfnTopicRule.IotSiteWiseActionProperty; | ||
/** | ||
* An action to sends messages directly to your Amazon MSK or self-managed Apache Kafka clusters for data analysis and visualization. | ||
* @default None | ||
*/ | ||
readonly kafka?: CfnTopicRule.KafkaActionProperty; | ||
/** | ||
* An action to write data from an MQTT message to Amazon Kinesis Data Streams. | ||
* @default None | ||
*/ | ||
readonly kinesis?: CfnTopicRule.KinesisActionProperty; | ||
/** | ||
* An action to invoke an AWS Lambda function, passing in an MQTT message. | ||
* @default None | ||
*/ | ||
readonly lambda?: CfnTopicRule.LambdaActionProperty; | ||
/** | ||
* An action to republish an MQTT message to another MQTT topic. | ||
* @default None | ||
*/ | ||
readonly republish?: CfnTopicRule.RepublishActionProperty; | ||
/** | ||
* An action to write the data from an MQTT message to an Amazon S3 bucket. | ||
* @default None | ||
*/ | ||
readonly s3?: CfnTopicRule.S3ActionProperty; | ||
/** | ||
* An action to send the data from an MQTT message as an Amazon SNS push notification. | ||
* @default None | ||
*/ | ||
readonly sns?: CfnTopicRule.SnsActionProperty; | ||
/** | ||
* An action to send data from an MQTT message to an Amazon SQS queue. | ||
* @default None | ||
*/ | ||
readonly sqs?: CfnTopicRule.SqsActionProperty; | ||
/** | ||
* An action to start an AWS Step Functions state machine. | ||
* @default None | ||
*/ | ||
readonly stepFunctions?: CfnTopicRule.StepFunctionsActionProperty; | ||
/** | ||
* An action to write attributes (measures) from an MQTT message into an Amazon Timestream table. | ||
* @default None | ||
*/ | ||
readonly timestream?: CfnTopicRule.TimestreamActionProperty; | ||
} |
72 changes: 72 additions & 0 deletions
72
packages/@aws-cdk/aws-iot/lib/actions/cloudwatch-alarm-action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import * as cloudwatch from '@aws-cdk/aws-cloudwatch'; | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import { IAction, ActionConfig, ITopicRule } from '..'; | ||
import { singletonActionRole } from './util'; | ||
|
||
/** | ||
* Configuration properties of an action for CloudWatch alarm. | ||
*/ | ||
export interface CloudwatchAlarmActionProps { | ||
/** | ||
* Reason for the alarm change. | ||
* | ||
* Supports substitution templates. | ||
* @see https://docs.aws.amazon.com/iot/latest/developerguide/iot-substitution-templates.html | ||
* | ||
* @default 'This state was set by the rule of AWS IoT Core.' will be set | ||
*/ | ||
readonly stateReason?: string; | ||
/** | ||
* The IAM role that allows access to the CloudWatch alarm. | ||
* | ||
* @default a new role will be created | ||
*/ | ||
readonly role?: iam.IRole; | ||
} | ||
|
||
/** | ||
* The action to change the state of an Amazon CloudWatch alarm. | ||
*/ | ||
export class CloudwatchAlarmAction implements IAction { | ||
private readonly stateReason?: string; | ||
private readonly role?: iam.IRole; | ||
|
||
/** | ||
* `stateValue` supports substitution templates. | ||
* @see https://docs.aws.amazon.com/iot/latest/developerguide/iot-substitution-templates.html | ||
* | ||
* @param alarm The CloudWatch alarm that set state by the rule | ||
* @param stateValue The value of the alarm state. | ||
* Valid values: OK, ALARM, INSUFFICIENT_DATA or substitution templates. | ||
* @param props Optional properties to not use default | ||
*/ | ||
constructor( | ||
private readonly alarm: cloudwatch.IAlarm, | ||
private readonly stateValue: cloudwatch.AlarmState | string, | ||
props: CloudwatchAlarmActionProps = {}, | ||
) { | ||
this.stateReason = props.stateReason; | ||
this.role = props.role; | ||
} | ||
|
||
bind(rule: ITopicRule): ActionConfig { | ||
const role = this.role ?? singletonActionRole(rule); | ||
role.addToPrincipalPolicy(this.putEventStatement(this.alarm)); | ||
|
||
return { | ||
cloudwatchAlarm: { | ||
alarmName: this.alarm.alarmName, | ||
stateReason: this.stateReason ?? 'This state was set by the rule of AWS IoT Core.', | ||
stateValue: this.stateValue, | ||
roleArn: role.roleArn, | ||
}, | ||
}; | ||
} | ||
|
||
private putEventStatement(alarm: cloudwatch.IAlarm) { | ||
return new iam.PolicyStatement({ | ||
actions: ['cloudwatch:SetAlarmState'], | ||
resources: [alarm.alarmArn], | ||
}); | ||
} | ||
} |
Oops, something went wrong.