-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
cloudwatch-logs-action.ts
49 lines (44 loc) · 1.25 KB
/
cloudwatch-logs-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
import * as iam from '@aws-cdk/aws-iam';
import * as iot from '@aws-cdk/aws-iot';
import * as logs from '@aws-cdk/aws-logs';
import { singletonActionRole } from './private/role';
/**
* Configuration properties of an action for CloudWatch Logs.
*/
export interface CloudWatchLogsActionProps {
/**
* The IAM role that allows access to the CloudWatch log group.
*
* @default a new role will be created
*/
readonly role?: iam.IRole;
}
/**
* The action to send data to Amazon CloudWatch Logs
*/
export class CloudWatchLogsAction implements iot.IAction {
private readonly role?: iam.IRole;
/**
* @param logGroup The CloudWatch log group to which the action sends data
* @param props Optional properties to not use default
*/
constructor(
private readonly logGroup: logs.ILogGroup,
props: CloudWatchLogsActionProps = {},
) {
this.role = props.role;
}
bind(rule: iot.ITopicRule): iot.ActionConfig {
const role = this.role ?? singletonActionRole(rule);
this.logGroup.grantWrite(role);
this.logGroup.grant(role, 'logs:DescribeLogStreams');
return {
configuration: {
cloudwatchLogs: {
logGroupName: this.logGroup.logGroupName,
roleArn: role.roleArn,
},
},
};
}
}