Skip to content

Commit fab0b69

Browse files
authored
Merge branch 'master' into shivlaks/sfn-merge-task-and-state-lambda
2 parents 9134c43 + 0567a23 commit fab0b69

File tree

4 files changed

+88
-7
lines changed

4 files changed

+88
-7
lines changed

packages/@aws-cdk/aws-cloudtrail/README.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,33 @@ trail.addLambdaEventSelector(["arn:aws:lambda"]);
9292

9393
// Add an event selector to log data events for the provided Lambda functions.
9494
trail.addLambdaEventSelector([lambdaFunction.functionArn]);
95-
```
95+
```
96+
97+
Using the `Trail.onEvent()` API, an EventBridge rule can be created that gets triggered for
98+
every event logged in CloudTrail.
99+
To only use the events that are of interest, either from a particular service, specific account or
100+
time range, they can be filtered down using the APIs available in `aws-events`. The following code
101+
filters events for S3 from a specific AWS account and triggers a lambda function. See [Events delivered via
102+
CloudTrail](https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html#events-for-services-not-listed)
103+
to learn more about the event structure for events from CloudTrail.
104+
105+
```ts
106+
import * as cloudtrail from '@aws-cdk/aws-cloudtrail';
107+
import * as eventTargets from '@aws-cdk/aws-events-targets';
108+
import * as lambda from '@aws-cdk/aws-lambda';
109+
110+
const myFunctionHandler = new lambda.Function(this, 'MyFunction', {
111+
code: lambda.Code.fromAsset('resource/myfunction');
112+
runtime: lambda.Runtime.NODEJS_12_X,
113+
handler: 'index.handler',
114+
});
115+
116+
const eventRule = Trail.onEvent(this, 'MyCloudWatchEvent', {
117+
target: new eventTargets.LambdaFunction(myFunctionHandler),
118+
});
119+
120+
eventRule.addEventPattern({
121+
account: '123456789012',
122+
source: 'aws.s3',
123+
});
124+
```

packages/@aws-cdk/aws-cloudtrail/lib/cloudtrail.ts

+20-6
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,23 @@ export enum ReadWriteType {
140140
*/
141141
export class Trail extends Resource {
142142

143+
/**
144+
* Create an event rule for when an event is recorded by any Trail in the account.
145+
*
146+
* Note that the event doesn't necessarily have to come from this Trail, it can
147+
* be captured from any one.
148+
*
149+
* Be sure to filter the event further down using an event pattern.
150+
*/
151+
public static onEvent(scope: Construct, id: string, options: events.OnEventOptions = {}): events.Rule {
152+
const rule = new events.Rule(scope, id, options);
153+
rule.addTarget(options.target);
154+
rule.addEventPattern({
155+
detailType: ['AWS API Call via CloudTrail'],
156+
});
157+
return rule;
158+
}
159+
143160
/**
144161
* ARN of the CloudTrail trail
145162
* i.e. arn:aws:cloudtrail:us-east-2:123456789012:trail/myCloudTrail
@@ -313,14 +330,11 @@ export class Trail extends Resource {
313330
* be captured from any one.
314331
*
315332
* Be sure to filter the event further down using an event pattern.
333+
*
334+
* @deprecated - use Trail.onEvent()
316335
*/
317336
public onCloudTrailEvent(id: string, options: events.OnEventOptions = {}): events.Rule {
318-
const rule = new events.Rule(this, id, options);
319-
rule.addTarget(options.target);
320-
rule.addEventPattern({
321-
detailType: ['AWS API Call via CloudTrail'],
322-
});
323-
return rule;
337+
return Trail.onEvent(this, id, options);
324338
}
325339
}
326340

packages/@aws-cdk/aws-cloudtrail/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@
9393
"@aws-cdk/core": "0.0.0",
9494
"constructs": "^3.0.2"
9595
},
96+
"awslint": {
97+
"exclude": [
98+
"events-method-signature:@aws-cdk/aws-cloudtrail.Trail.onEvent"
99+
]
100+
},
96101
"engines": {
97102
"node": ">= 10.13.0 <13 || >=13.7.0"
98103
},

packages/@aws-cdk/aws-cloudtrail/test/cloudtrail.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,37 @@ describe('cloudtrail', () => {
323323
});
324324
});
325325
});
326+
327+
describe('onEvent', () => {
328+
test('add an event rule', () => {
329+
// GIVEN
330+
const stack = getTestStack();
331+
332+
// WHEN
333+
Trail.onEvent(stack, 'DoEvents', {
334+
target: {
335+
bind: () => ({
336+
id: '',
337+
arn: 'arn',
338+
}),
339+
},
340+
});
341+
342+
// THEN
343+
expect(stack).toHaveResource('AWS::Events::Rule', {
344+
EventPattern: {
345+
'detail-type': [
346+
'AWS API Call via CloudTrail',
347+
],
348+
},
349+
State: 'ENABLED',
350+
Targets: [
351+
{
352+
Arn: 'arn',
353+
Id: 'Target0',
354+
},
355+
],
356+
});
357+
});
358+
});
326359
});

0 commit comments

Comments
 (0)