question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[aws-events-targets] Support CloudWatch Logs as a target

See original GitHub issue

Although 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:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
otterleycommented, Aug 25, 2020

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.

1reaction
aripalocommented, Sep 21, 2020

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found