/
cloudwatch-put-metric-action.ts
77 lines (69 loc) · 2.31 KB
/
cloudwatch-put-metric-action.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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 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;
/**
* The CloudWatch metric namespace name.
*
* Supports substitution templates.
* @see https://docs.aws.amazon.com/iot/latest/developerguide/iot-substitution-templates.html
*/
readonly metricNamespace: string;
/**
* A string that contains the timestamp, expressed in seconds in Unix epoch time.
*
* Supports substitution templates.
* @see https://docs.aws.amazon.com/iot/latest/developerguide/iot-substitution-templates.html
*
* @default - none -- Defaults to the current Unix epoch time.
*/
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;
/**
* 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;
}
/**
* The action to capture an Amazon CloudWatch metric.
*/
export class CloudWatchPutMetricAction implements iot.IAction {
constructor(private readonly props: CloudWatchPutMetricActionProps) {
}
bind(rule: iot.ITopicRule): iot.ActionConfig {
const role = this.props.role ?? singletonActionRole(rule);
cloudwatch.Metric.grantPutMetricData(role);
return {
configuration: {
cloudwatchMetric: {
metricName: this.props.metricName,
metricNamespace: this.props.metricNamespace,
metricTimestamp: this.props.metricTimestamp,
metricUnit: this.props.metricUnit,
metricValue: this.props.metricValue,
roleArn: role.roleArn,
},
},
};
}
}