[aws-events-targets] Support CloudWatch Logs as a target
See original GitHub issueAlthough it’s not well-documented yet, CloudWatch Logs is a supported target for event rules. The ARN is the log group ARN.
Use Case
This is useful for, among other things, recording ECS task state changes that are otherwise difficult to observe.
Proposed Solution
Here’s an example. Note, the log group name must start with /aws/events/
because EventBridge relies on an unpublished CloudWatch Logs resource policy in order to work properly.
Caller:
const logGroup = new logs.LogGroup(this, 'EcsEventLog', {
// must start with /aws/events/
logGroupName: `/aws/events/ecs-${cluster.clusterName}`
});
new cdk.CfnOutput(this, 'LogGroupName', {
value: logGroup.logGroupName
});
const clusterEvents = new events.Rule(this, 'EcsEvents', {
eventPattern: {
source: ['aws.ecs'],
detail: {
clusterArn: [cluster.clusterArn]
}
},
targets: [new CloudWatchLogsTarget(this, 'LogsTarget', logGroup)]
});
Implementation:
export class CloudWatchLogsTarget extends cdk.Construct {
private logGroup: logs.ILogGroup;
constructor(scope: cdk.Construct, id: string, logGroup: logs.ILogGroup) {
super(scope, id);
this.logGroup = logGroup;
}
public bind(rule: events.IRule, id?: string): events.RuleTargetConfig {
return {
id: '',
// we can't use logGroup.logArn because it has `:*` on the end, and it's a token
// so we can't just remove the suffix with string replacement operations
arn: `arn:aws:logs:${this.logGroup.stack.region}:${this.logGroup.stack.account}:log-group:${this.logGroup.logGroupName}`,
}
}
}
Ideally, we’d go one step further and implement event handler declarations on ECS Cluster objects, but that’s a story for another day.
Other
- 👋 I may be able to implement this feature request
- ⚠️ This feature might incur a breaking change
This is a 🚀 Feature Request
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:12 (5 by maintainers)
Top Results From Across the Web
aws-cdk/aws-events-targets module - AWS Documentation
Use the LogGroup target to log your events in a CloudWatch LogGroup. For example, the following code snippet creates an event rule with...
Read more >EventBridge to CloudWatch Logs - Serverless Land
Create an EventBridge rule that sends events to CloudWatch Logs ... Events; using Amazon.CDK.AWS.Events.Targets; using Amazon.CDK.AWS.Logs; using Constructs ...
Read more >@aws-cdk/aws-events-targets - npm
This library contains integration classes to send Amazon EventBridge to any number of supported AWS Services. Instances of these classes should ...
Read more >@aws-cdk/aws-events-targets | Yarn - Package Manager
This library contains integration classes to send Amazon EventBridge to any number of supported AWS Services. Instances of these classes should be passed...
Read more >Top 5 @aws-cdk/aws-events-targets Code Examples | Snyk
To help you get started, we've selected a few @aws-cdk/aws-events-targets examples, ... Duration.minutes(1), environment: { CHANGELOGS_TABLE_NAME: props.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Looks good @demus. One runtime check I might add is to verify that the log group name starts with
/aws/events/
and raise a runtime error if not.I guess this is due to that CloudFormation issue, but just today found this out since I had created CloudWatch Log Group as EventBridge Rule target manually in the console, but could not replicate it with CDK.
Just my two cents, it’d be a really useful feature to have!