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): add Action to capture CloudWatch metrics #17503

Merged
merged 5 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
address comments
  • Loading branch information
yamatatsu committed Nov 16, 2021
commit 6ace2c8c2069eb7c0ee25584b1ade41015c47da1
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-iot-actions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ const topicRule = new iot.TopicRule(this, 'TopicRule', {
"SELECT topic(2) as device_id, namespace, unit, value, timestamp FROM 'device/+/data'",
),
actions: [
new actions.CloudWatchMetricAction({
new actions.CloudWatchPutMetricAction({
metricName: '${topic(2)}',
metricNamespace: '${namespace}',
metricUnit: '${unit}',
metricValue: '${value}',
metricTimestamp: '${timestamp}',
})
}),
],
});
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import * as iam from '@aws-cdk/aws-iam';
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as iot from '@aws-cdk/aws-iot';
import { CommonActionProps } from './common-action-props';
import { singletonActionRole } from './private/role';

/**
* Configuration properties of an action for CloudWatch metric.
*/
export interface CloudWatchMetricActionProps extends CommonActionProps {
export interface CloudWatchPutMetricActionProps extends CommonActionProps {
/**
* The CloudWatch metric name.
*
* Supports substitution templates.
* @see https://docs.aws.amazon.com/iot/latest/developerguide/iot-substitution-templates.html
*/
readonly metricName: string,
readonly metricName: string;

/**
* The CloudWatch metric namespace name.
*
* Supports substitution templates.
* @see https://docs.aws.amazon.com/iot/latest/developerguide/iot-substitution-templates.html
*/
readonly metricNamespace: string,
readonly metricNamespace: string;

/**
* A string that contains the timestamp, expressed in seconds in Unix epoch time.
Expand All @@ -31,38 +31,35 @@ export interface CloudWatchMetricActionProps extends CommonActionProps {
*
* @default - none -- Defaults to the current Unix epoch time.
*/
readonly metricTimestamp?: string,
readonly metricTimestamp?: string;

/**
* The metric unit supported by CloudWatch.
*
* Supports substitution templates.
* @see https://docs.aws.amazon.com/iot/latest/developerguide/iot-substitution-templates.html
*/
readonly metricUnit: string,
readonly metricUnit: string;

/**
* A string that contains the CloudWatch metric value.
*
* Supports substitution templates.
* @see https://docs.aws.amazon.com/iot/latest/developerguide/iot-substitution-templates.html
*/
readonly metricValue: string,
readonly metricValue: string;
}

/**
* The action to capture an Amazon CloudWatch metric.
*/
export class CloudWatchMetricAction implements iot.IAction {
constructor(private readonly props: CloudWatchMetricActionProps) {
export class CloudWatchPutMetricAction implements iot.IAction {
constructor(private readonly props: CloudWatchPutMetricActionProps) {
}

bind(rule: iot.ITopicRule): iot.ActionConfig {
const role = this.props.role ?? singletonActionRole(rule);
role.addToPrincipalPolicy(new iam.PolicyStatement({
actions: ['cloudwatch:PutMetricData'],
resources: ['*'],
}));
cloudwatch.Metric.grantPutMetricData(role);

return {
configuration: {
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-iot-actions/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './cloudwatch-logs-action';
export * from './cloudwatch-metric-action';
export * from './cloudwatch-put-metric-action';
export * from './common-action-props';
export * from './firehose-stream-action';
export * from './lambda-function-action';
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-iot-actions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"jest": "^27.3.1"
},
"dependencies": {
"@aws-cdk/aws-cloudwatch": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-iot": "0.0.0",
"@aws-cdk/aws-kinesisfirehose": "0.0.0",
Expand All @@ -92,6 +93,7 @@
},
"homepage": "https://github.com/aws/aws-cdk",
"peerDependencies": {
"@aws-cdk/aws-cloudwatch": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-iot": "0.0.0",
"@aws-cdk/aws-kinesisfirehose": "0.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('Default cloudwatch metric action', () => {

// WHEN
topicRule.addAction(
new actions.CloudWatchMetricAction({
new actions.CloudWatchPutMetricAction({
metricName: '${topic(2)}',
metricNamespace: '${namespace}',
metricUnit: '${unit}',
Expand Down Expand Up @@ -80,7 +80,7 @@ test('can set timestamp', () => {

// WHEN
topicRule.addAction(
new actions.CloudWatchMetricAction({
new actions.CloudWatchPutMetricAction({
metricName: '${topic(2)}',
metricNamespace: '${namespace}',
metricUnit: '${unit}',
Expand Down Expand Up @@ -109,7 +109,7 @@ test('can set role', () => {

// WHEN
topicRule.addAction(
new actions.CloudWatchMetricAction({
new actions.CloudWatchPutMetricAction({
metricName: '${topic(2)}',
metricNamespace: '${namespace}',
metricUnit: '${unit}',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// !cdk-integ pragma:ignore-assets
import * as iot from '@aws-cdk/aws-iot';
import * as logs from '@aws-cdk/aws-logs';
import * as cdk from '@aws-cdk/core';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// !cdk-integ pragma:ignore-assets
import * as iot from '@aws-cdk/aws-iot';
import * as cdk from '@aws-cdk/core';
import * as actions from '../../lib';
Expand All @@ -13,7 +12,7 @@ class TestStack extends cdk.Stack {
sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, namespace, unit, value, timestamp FROM 'device/+/data'"),
});

topicRule.addAction(new actions.CloudWatchMetricAction({
topicRule.addAction(new actions.CloudWatchPutMetricAction({
metricName: '${topic(2)}',
metricNamespace: '${namespace}',
metricUnit: '${unit}',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// !cdk-integ pragma:ignore-assets
import * as iot from '@aws-cdk/aws-iot';
import * as firehose from '@aws-cdk/aws-kinesisfirehose';
import * as destinations from '@aws-cdk/aws-kinesisfirehose-destinations';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// !cdk-integ pragma:ignore-assets
import * as iot from '@aws-cdk/aws-iot';
import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// !cdk-integ pragma:ignore-assets
import * as iot from '@aws-cdk/aws-iot';
import * as s3 from '@aws-cdk/aws-s3';
import * as cdk from '@aws-cdk/core';
Expand Down