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(events-targets): add CloudWatch LogGroup Target #10598

Merged
merged 23 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
16820d7
feat(aws-events-targets): add CloudWatch LogGroup Target
Sep 30, 2020
1ebf625
Rename handler prop to logGroup and add documentation about LogGroup …
Oct 1, 2020
a837a96
Merge branch 'master' into DaWyz/events-target-loggroup
DaWyz Oct 1, 2020
ba11ea0
Add log group name validation
DaWyz Oct 9, 2020
30c045a
Fixing documentation
DaWyz Oct 9, 2020
ca31660
Merge branch 'master' into DaWyz/events-target-loggroup
DaWyz Oct 21, 2020
d042c70
Merge branch 'master' into DaWyz/events-target-loggroup
DaWyz Oct 24, 2020
36ef4d5
Merge branch 'master' into DaWyz/events-target-loggroup
DaWyz Oct 25, 2020
b970107
Add support for resource policy for LogGroup
DaWyz Oct 25, 2020
4c71270
fix integ json for loggroup target
DaWyz Oct 26, 2020
3b59ec3
Improve code and fix nits
DaWyz Oct 28, 2020
9e9e425
Remove like to issue as it's not related to events targets
DaWyz Oct 28, 2020
e41e241
Merge branch 'master' into DaWyz/events-target-loggroup
DaWyz Nov 6, 2020
13c9ae6
Merge branch 'master' into DaWyz/events-target-loggroup
DaWyz Nov 10, 2020
d1bf75b
Fix nits and udpate aws-events-targets README.md
DaWyz Nov 15, 2020
90c19bb
fix readme
DaWyz Nov 15, 2020
b334fda
use node.addr for log-group-resource policyName
DaWyz Nov 15, 2020
9f0da1b
default PolicyName to use cdk.Names.uniqueId() instead of node.addr()
DaWyz Nov 15, 2020
5cfa807
Removing policyName value in favor of the uniqueId
DaWyz Nov 15, 2020
4ec36bc
Merge branch 'master' into DaWyz/events-target-loggroup
DaWyz Nov 15, 2020
fa8f30f
Merge branch 'master' into DaWyz/events-target-loggroup
DaWyz Nov 15, 2020
3251d58
update integration test expectation
DaWyz Nov 15, 2020
58c1cdb
Merge branch 'master' into DaWyz/events-target-loggroup
mergify[bot] Nov 18, 2020
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
41 changes: 41 additions & 0 deletions packages/@aws-cdk/aws-events-targets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,48 @@ Currently supported are:
* Queue a Batch job
* Make an AWS API call
* Put a record to a Kinesis stream
* Log an event into a LogGroup

See the README of the `@aws-cdk/aws-events` library for more information on
EventBridge.

## LogGroup

Use the `LogGroup` target to log your events in a CloudWatch LogGroup.

The LogGroup name must start with `/aws/events/`.

```ts
const rule = new Rule(this, 'rule', {
eventPattern: {
source: [stack.account],
shivlaks marked this conversation as resolved.
Show resolved Hide resolved
},
});

const logGroup = new LogGroup(this, 'MyLogGroup', {
logGroupName: '/aws/events/MyLogGroup',
});

rule.addTarget(targets.LogGroup(logGroup));
shivlaks marked this conversation as resolved.
Show resolved Hide resolved
```

You can also use an [InputTransformer](https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_InputTransformer.html).

```ts
const rule = new Rule(this, 'rule', {
eventPattern: {
source: [stack.account],
},
});

const logGroup = new LogGroup(this, 'MyLogGroup', {
logGroupName: '/aws/events/MyLogGroup',
});

rule.addTarget(new targets.LogGroup(logGroup, {
event: events.RuleTargetInput.fromObject({
status: events.EventField.fromPath('$.detail.status'),
instanceId: events.EventField.fromPath('$.detail.instance-id'),
})
}));
```
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-events-targets/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './ecs-task-properties';
export * from './ecs-task';
export * from './state-machine';
export * from './kinesis-stream';
export * from './log-group';
40 changes: 40 additions & 0 deletions packages/@aws-cdk/aws-events-targets/lib/log-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as events from '@aws-cdk/aws-events';
import * as logs from '@aws-cdk/aws-logs';
import { Token } from '@aws-cdk/core';
/**
* Customize the CloudWatch LogGroup Event Target
*/
export interface LogGroupProps {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can a role also be supplied? - some of our other event targets accept a role / create a singleton role for the target.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not possible to pass a role when using a LogGroup as a target. I tried it initially and got an CloudFormation error back.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotcha, thanks for clarifying

/**
* The event to send to the CloudWatch LogGroup
*
* This will be the event logged into the CloudWatch LogGroup
*
* @default - the entire EventBridge event
*/
readonly event?: events.RuleTargetInput;
}

/**
* Use an AWS CloudWatch LogGroup as an event rule target.
*
* The LogGroup name must start with /aws/events/.
*/
export class LogGroup implements events.IRuleTarget {
DaWyz marked this conversation as resolved.
Show resolved Hide resolved
constructor(private readonly logGroup: logs.ILogGroup, private readonly props: LogGroupProps = {}) {}

/**
* Returns a RuleTarget that can be used to log an event into a CloudWatch LogGroup
*/
public bind(_rule: events.IRule, _id?: string): events.RuleTargetConfig {
if (!Token.isUnresolved(this.logGroup.logGroupName) && !this.logGroup.logGroupName.startsWith('/aws/events/')) {
throw new Error('Target LogGroup name must start with "/aws/events/"');
}

return {
id: '',
arn: `arn:aws:logs:${this.logGroup.stack.region}:${this.logGroup.stack.account}:log-group:${this.logGroup.logGroupName}`,
DaWyz marked this conversation as resolved.
Show resolved Hide resolved
input: this.props.event,
};
}
}
12 changes: 7 additions & 5 deletions packages/@aws-cdk/aws-events-targets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,39 +81,41 @@
"pkglint": "0.0.0"
},
"dependencies": {
"@aws-cdk/aws-batch": "0.0.0",
"@aws-cdk/aws-codebuild": "0.0.0",
"@aws-cdk/aws-codepipeline": "0.0.0",
"@aws-cdk/aws-ec2": "0.0.0",
"@aws-cdk/aws-ecs": "0.0.0",
"@aws-cdk/aws-events": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-kinesis": "0.0.0",
"@aws-cdk/aws-lambda": "0.0.0",
"@aws-cdk/aws-logs": "0.0.0",
"@aws-cdk/aws-sns": "0.0.0",
"@aws-cdk/aws-sns-subscriptions": "0.0.0",
"@aws-cdk/aws-sqs": "0.0.0",
"@aws-cdk/aws-stepfunctions": "0.0.0",
"@aws-cdk/aws-batch": "0.0.0",
"@aws-cdk/aws-kinesis": "0.0.0",
"@aws-cdk/core": "0.0.0",
"constructs": "^3.0.4"
},
"homepage": "https://github.com/aws/aws-cdk",
"peerDependencies": {
"@aws-cdk/aws-batch": "0.0.0",
"@aws-cdk/aws-codebuild": "0.0.0",
"@aws-cdk/aws-codepipeline": "0.0.0",
"@aws-cdk/aws-ec2": "0.0.0",
"@aws-cdk/aws-ecs": "0.0.0",
"@aws-cdk/aws-events": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-kinesis": "0.0.0",
"@aws-cdk/aws-lambda": "0.0.0",
"@aws-cdk/aws-logs": "0.0.0",
"@aws-cdk/aws-sns": "0.0.0",
"@aws-cdk/aws-sns-subscriptions": "0.0.0",
"@aws-cdk/aws-sqs": "0.0.0",
"@aws-cdk/aws-stepfunctions": "0.0.0",
"@aws-cdk/aws-batch": "0.0.0",
"@aws-cdk/core": "0.0.0",
"constructs": "^3.0.4",
"@aws-cdk/aws-kinesis": "0.0.0"
"constructs": "^3.0.4"
},
"engines": {
"node": ">= 10.13.0 <13 || >=13.7.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"Resources": {
"loggroupB02AAEB1": {
"Type": "AWS::Logs::LogGroup",
"Properties": {
"LogGroupName": "/aws/events/MyLogGroupName",
"RetentionInDays": 731
},
"UpdateReplacePolicy": "Retain",
"DeletionPolicy": "Retain"
},
"TimerBF6F831F": {
"Type": "AWS::Events::Rule",
"Properties": {
"ScheduleExpression": "rate(1 minute)",
"State": "ENABLED",
"Targets": [
{
"Arn": {
"Fn::Join": [
"",
[
"arn:aws:logs:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":log-group:",
{
"Ref": "loggroupB02AAEB1"
}
]
]
},
"Id": "Target0"
}
]
}
},
"Timer2B6F162E9": {
"Type": "AWS::Events::Rule",
"Properties": {
"ScheduleExpression": "rate(2 minutes)",
"State": "ENABLED",
"Targets": [
{
"Arn": {
"Fn::Join": [
"",
[
"arn:aws:logs:",
{
"Ref": "AWS::Region"
},
":",
{
"Ref": "AWS::AccountId"
},
":log-group:",
{
"Ref": "loggroupB02AAEB1"
}
]
]
},
"Id": "Target0",
"InputTransformer": {
"InputPathsMap": {
"f1": "$"
},
"InputTemplate": "{\"data\":<f1>}"
}
}
]
}
}
}
}
29 changes: 29 additions & 0 deletions packages/@aws-cdk/aws-events-targets/test/logs/integ.log-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as events from '@aws-cdk/aws-events';
import * as logs from '@aws-cdk/aws-logs';
import * as cdk from '@aws-cdk/core';
import * as targets from '../../lib';

const app = new cdk.App();

const stack = new cdk.Stack(app, 'log-group-events');

const logGroup = new logs.LogGroup(stack, 'log-group', {
logGroupName: '/aws/events/MyLogGroupName',
});

const timer = new events.Rule(stack, 'Timer', {
schedule: events.Schedule.rate(cdk.Duration.minutes(1)),
});
timer.addTarget(new targets.LogGroup(logGroup));

const timer2 = new events.Rule(stack, 'Timer2', {
schedule: events.Schedule.rate(cdk.Duration.minutes(2)),
});
timer2.addTarget(new targets.LogGroup(logGroup, {
event: events.RuleTargetInput.fromObject({
data: events.EventField.fromPath('$'),
}),
}));

app.synth();

123 changes: 123 additions & 0 deletions packages/@aws-cdk/aws-events-targets/test/logs/log-group.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import '@aws-cdk/assert/jest';
import * as events from '@aws-cdk/aws-events';
import * as logs from '@aws-cdk/aws-logs';
import * as cdk from '@aws-cdk/core';
import * as targets from '../../lib';


test('use log group as an event rule target', () => {
// GIVEN
const stack = new cdk.Stack();
const logGroup = new logs.LogGroup(stack, 'MyLogGroup', {
logGroupName: '/aws/events/MyLogGroup',
});
const rule1 = new events.Rule(stack, 'Rule', {
schedule: events.Schedule.rate(cdk.Duration.minutes(1)),
});

// WHEN
rule1.addTarget(new targets.LogGroup(logGroup));

// THEN
expect(stack).toHaveResource('AWS::Events::Rule', {
ScheduleExpression: 'rate(1 minute)',
State: 'ENABLED',
Targets: [
{
Arn: {
'Fn::Join': [
'',
[
'arn:aws:logs:',
{
Ref: 'AWS::Region',
},
':',
{
Ref: 'AWS::AccountId',
},
':log-group:',
{
Ref: 'MyLogGroup5C0DAD85',
},
],
],
},
Id: 'Target0',
},
],
});
});


test.only('log group used as an event rule target must have a name starting with "/aws/events/"', () => {
// GIVEN
const logGroupName = '/awdss/events/MyLogGroup';
const stack = new cdk.Stack();

const rule1 = new events.Rule(stack, 'Rule', {
schedule: events.Schedule.rate(cdk.Duration.minutes(1)),
});

// WHEN
let addTargetWrapper = () => {
rule1.addTarget(new targets.LogGroup(logs.LogGroup.fromLogGroupName(stack, 'MyLogGroupImported', logGroupName)));
};

// THEN
expect(addTargetWrapper).toThrowError('Target LogGroup name must start with "/aws/events/"');
});

test('use log group as an event rule target with rule target input', () => {
// GIVEN
const stack = new cdk.Stack();
const logGroup = new logs.LogGroup(stack, 'MyLogGroup', {
logGroupName: '/aws/events/MyLogGroup',
});
const rule1 = new events.Rule(stack, 'Rule', {
schedule: events.Schedule.rate(cdk.Duration.minutes(1)),
});

// WHEN
rule1.addTarget(new targets.LogGroup(logGroup, {
event: events.RuleTargetInput.fromObject({
data: events.EventField.fromPath('$'),
}),
}));

// THEN
expect(stack).toHaveResource('AWS::Events::Rule', {
ScheduleExpression: 'rate(1 minute)',
State: 'ENABLED',
Targets: [
{
Arn: {
'Fn::Join': [
'',
[
'arn:aws:logs:',
{
Ref: 'AWS::Region',
},
':',
{
Ref: 'AWS::AccountId',
},
':log-group:',
{
Ref: 'MyLogGroup5C0DAD85',
},
],
],
},
Id: 'Target0',
InputTransformer: {
InputPathsMap: {
f1: '$',
},
InputTemplate: '{"data":<f1>}',
},
},
],
});
});
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-events/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ event when the pipeline changes it's state.
Service](https://docs.aws.amazon.com/eventbridge/latest/userguide/event-types.html).
* __Targets__: A target processes events. Targets can include Amazon EC2
instances, AWS Lambda functions, Kinesis streams, Amazon ECS tasks, Step
Functions state machines, Amazon SNS topics, Amazon SQS queues, and built-in
Functions state machines, Amazon SNS topics, Amazon SQS queues, Amazon CloudWatch LogGroups, and built-in
targets. A target receives events in JSON format.
* __Rules__: A rule matches incoming events and routes them to targets for
processing. A single rule can route to multiple targets, all of which are
Expand Down