Skip to content

Commit 28942d2

Browse files
authored
fix: update all 'onXxx' methods to be CloudWatch Events (#2609)
Make all CloudWatch events methods consistent. All methods called 'onXxx()' are now guaranteed to be CloudWatch Events triggers, all non-CWE methods that started with 'on' are now called differently. Introduce the 'onCloudTrailEvent' convention, to clearly signal that the indicated event depend on the presence of a CloudTrail Trail to capture it (as opposed to being intrinsically supported by the service itself). Introduce awslint rules to enforce these conventions. Fixes #2278. BREAKING CHANGES * All `onXxx()` CloudWatch Event methods now have the signature: ```ts resource.onEvent('SomeId', { target: new SomeTarget(...), // options }); ``` * CloudWatch: rename `onAlarm()` => `addAlarmAction()`, `onOk` => `addOkAction`, `onInsufficientData` => `addInsufficientDataAction`. * AutoScaling: rename `onLifecycleTransition` => `addLifecycleHook()`. * LambdaDeploymentGroup: rename `onPreHook` => `addPreHook`, `onPostHook` => `addPostHook`. * UserPool: rename all `onXxx` methods => `addXxxTrigger`. * Repository: rename `onImagePushed` => `onCloudTrailImagePushed`. * Bucket: rename `onEvent` => `addEventNotification`, `onObjectCreated` => `addObjectCreatedNotification`, `onObjectRemoved` => `addObjectRemovedNotification`, `onPutObject` => `onCloudTrailPutObject`.
1 parent 45abfcd commit 28942d2

File tree

54 files changed

+1090
-285
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1090
-285
lines changed

packages/@aws-cdk/aws-applicationautoscaling/lib/step-scaling-policy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class StepScalingPolicy extends cdk.Construct {
108108
evaluationPeriods: 1,
109109
threshold,
110110
});
111-
this.lowerAlarm.onAlarm(this.lowerAction);
111+
this.lowerAlarm.addAlarmAction(this.lowerAction);
112112
}
113113

114114
if (alarms.upperAlarmIntervalIndex !== undefined) {
@@ -138,7 +138,7 @@ export class StepScalingPolicy extends cdk.Construct {
138138
evaluationPeriods: 1,
139139
threshold,
140140
});
141-
this.upperAlarm.onAlarm(this.upperAction);
141+
this.upperAlarm.addAlarmAction(this.upperAction);
142142
}
143143
}
144144
}

packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ abstract class AutoScalingGroupBase extends Resource implements IAutoScalingGrou
193193
/**
194194
* Send a message to either an SQS queue or SNS topic when instances launch or terminate
195195
*/
196-
public onLifecycleTransition(id: string, props: BasicLifecycleHookProps): LifecycleHook {
196+
public addLifecycleHook(id: string, props: BasicLifecycleHookProps): LifecycleHook {
197197
return new LifecycleHook(this, `LifecycleHook${id}`, {
198198
autoScalingGroup: this,
199199
...props
@@ -701,7 +701,7 @@ export interface IAutoScalingGroup extends IResource {
701701
/**
702702
* Send a message to either an SQS queue or SNS topic when instances launch or terminate
703703
*/
704-
onLifecycleTransition(id: string, props: BasicLifecycleHookProps): LifecycleHook;
704+
addLifecycleHook(id: string, props: BasicLifecycleHookProps): LifecycleHook;
705705

706706
/**
707707
* Scale out or in based on time

packages/@aws-cdk/aws-autoscaling/lib/step-scaling-policy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export class StepScalingPolicy extends cdk.Construct {
109109
evaluationPeriods: 1,
110110
threshold,
111111
});
112-
this.lowerAlarm.onAlarm(this.lowerAction);
112+
this.lowerAlarm.addAlarmAction(this.lowerAction);
113113
}
114114

115115
if (alarms.upperAlarmIntervalIndex !== undefined) {
@@ -139,7 +139,7 @@ export class StepScalingPolicy extends cdk.Construct {
139139
evaluationPeriods: 1,
140140
threshold,
141141
});
142-
this.upperAlarm.onAlarm(this.upperAction);
142+
this.upperAlarm.addAlarmAction(this.upperAction);
143143
}
144144
}
145145
}

packages/@aws-cdk/aws-autoscaling/test/test.lifecyclehooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export = {
1818
});
1919

2020
// WHEN
21-
asg.onLifecycleTransition('Transition', {
21+
asg.addLifecycleHook('Transition', {
2222
notificationTarget: new FakeNotificationTarget(),
2323
lifecycleTransition: autoscaling.LifecycleTransition.InstanceLaunching,
2424
defaultResult: autoscaling.DefaultResult.Abandon,
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Some sample CloudTrail events
2+
3+
For reference.
4+
5+
6+
## S3
7+
8+
PutObject
9+
10+
{
11+
"eventSource": "s3.amazonaws.com",
12+
"resources": [
13+
{
14+
"ARN": "arn:aws:s3:::BUCKETNAME/OBJECTKEY",
15+
"type": "AWS::S3::Object"
16+
},
17+
{
18+
"accountId": "123456789012",
19+
"ARN": "arn:aws:s3:::BUCKETNAME",
20+
"type": "AWS::S3::Bucket"
21+
}
22+
],
23+
"eventTime": "2019-05-22T08:38:05Z",
24+
"userAgent": "[aws-cli/1.16.96 Python/2.7.12 Linux/4.4.0-146-generic botocore/1.12.86]",
25+
"readOnly": false,
26+
"recipientAccountId": "123456789012",
27+
"awsRegion": "eu-west-1",
28+
"requestID": "CF9748DFDC5FB0A4",
29+
"additionalEventData": {
30+
"CipherSuite": "ECDHE-RSA-AES128-GCM-SHA256",
31+
"bytesTransferredOut": 0,
32+
"AuthenticationMethod": "AuthHeader",
33+
"x-amz-id-2": "hRJMAs5p4ALZIabP4ATIL53npWU61+N6LYWj02gdQtR0ymKSySzVXUSZx7ydv7tRJwk+XMaPerM=",
34+
"bytesTransferredIn": 197,
35+
"SignatureVersion": "SigV4"
36+
},
37+
"eventType": "AwsApiCall",
38+
"eventID": "3074546e-1bfa-4973-8502-b1bb4d0bda1a",
39+
"eventVersion": "1.05",
40+
"eventName": "PutObject",
41+
"sourceIPAddress": "1.2.3.4",
42+
"userIdentity": {
43+
"accountId": "123456789012",
44+
"type": "AssumedRole",
45+
"principalId": "AROAJBNCAL3UTR5C42U4M:user-SomeRole",
46+
"accessKeyId": "AZYCAIJERO6H7",
47+
"sessionContext": {
48+
"attributes": {
49+
"mfaAuthenticated": "false",
50+
"creationDate": "2019-05-22T08:10:21Z"
51+
},
52+
"sessionIssuer": {
53+
"accountId": "123456789012",
54+
"type": "Role",
55+
"principalId": "AROAJBNCAL3UTR5C42U4M",
56+
"userName": "SomeRole",
57+
"arn": "arn:aws:iam::123456789012:role/SomeRole"
58+
}
59+
},
60+
"arn": "arn:aws:sts::123456789012:assumed-role/SomeRole/user-SomeRole"
61+
},
62+
"responseElements": null,
63+
"requestParameters": {
64+
"bucketName": "BUCKETNAME",
65+
"Host": "BUCKETNAME.s3.eu-west-1.amazonaws.com",
66+
"key": "OBJECTKEY"
67+
}
68+
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,16 @@ export class Trail extends Resource {
222222
}
223223

224224
/**
225-
* Create an event rule for when an event is recorded by any trail.
225+
* Create an event rule for when an event is recorded by any Trail in the account.
226226
*
227-
* Note that the event doesn't necessarily have to come from this
228-
* trail. Be sure to filter the event properly using an event pattern.
227+
* Note that the event doesn't necessarily have to come from this Trail, it can
228+
* be captured from any one.
229+
*
230+
* Be sure to filter the event further down using an event pattern.
229231
*/
230-
public onEvent(name: string, target?: events.IRuleTarget, options?: events.RuleProps) {
231-
const rule = new events.Rule(this, name, options);
232-
rule.addTarget(target);
232+
public onCloudTrailEvent(id: string, options: events.OnEventOptions): events.Rule {
233+
const rule = new events.Rule(this, id, options);
234+
rule.addTarget(options.target);
233235
rule.addEventPattern({
234236
detailType: ['AWS API Call via CloudTrail']
235237
});

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,13 @@ export = {
197197
const trail = new Trail(stack, 'MyAmazingCloudTrail', { managementEvents: ReadWriteType.WriteOnly });
198198

199199
// WHEN
200-
trail.onEvent('DoEvents', {
201-
bind: () => ({
202-
arn: 'arn',
203-
id: 'myid'
204-
})
200+
trail.onCloudTrailEvent('DoEvents', {
201+
target: {
202+
bind: () => ({
203+
arn: 'arn',
204+
id: 'myid'
205+
})
206+
}
205207
});
206208

207209
// THEN

packages/@aws-cdk/aws-cloudwatch/lib/alarm.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export class Alarm extends Resource implements IAlarm {
155155
*
156156
* Typically the ARN of an SNS topic or ARN of an AutoScaling policy.
157157
*/
158-
public onAlarm(...actions: IAlarmAction[]) {
158+
public addAlarmAction(...actions: IAlarmAction[]) {
159159
if (this.alarmActionArns === undefined) {
160160
this.alarmActionArns = [];
161161
}
@@ -168,7 +168,7 @@ export class Alarm extends Resource implements IAlarm {
168168
*
169169
* Typically the ARN of an SNS topic or ARN of an AutoScaling policy.
170170
*/
171-
public onInsufficientData(...actions: IAlarmAction[]) {
171+
public addInsufficientDataAction(...actions: IAlarmAction[]) {
172172
if (this.insufficientDataActionArns === undefined) {
173173
this.insufficientDataActionArns = [];
174174
}
@@ -181,7 +181,7 @@ export class Alarm extends Resource implements IAlarm {
181181
*
182182
* Typically the ARN of an SNS topic or ARN of an AutoScaling policy.
183183
*/
184-
public onOk(...actions: IAlarmAction[]) {
184+
public addOkAction(...actions: IAlarmAction[]) {
185185
if (this.okActionArns === undefined) {
186186
this.okActionArns = [];
187187
}

packages/@aws-cdk/aws-cloudwatch/test/test.alarm.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ export = {
7272
evaluationPeriods: 2
7373
});
7474

75-
alarm.onAlarm(new TestAlarmAction('A'));
76-
alarm.onInsufficientData(new TestAlarmAction('B'));
77-
alarm.onOk(new TestAlarmAction('C'));
75+
alarm.addAlarmAction(new TestAlarmAction('A'));
76+
alarm.addInsufficientDataAction(new TestAlarmAction('B'));
77+
alarm.addOkAction(new TestAlarmAction('C'));
7878

7979
// THEN
8080
expect(stack).to(haveResource('AWS::CloudWatch::Alarm', {

packages/@aws-cdk/aws-codebuild/lib/project.ts

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ export interface IProject extends IResource, iam.IGrantable {
3232
/** The IAM service Role of this Project. Undefined for imported Projects. */
3333
readonly role?: iam.IRole;
3434

35+
/**
36+
* Defines a CloudWatch event rule triggered when something happens with this project.
37+
*
38+
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html
39+
*/
40+
onEvent(id: string, options: events.OnEventOptions): events.Rule;
41+
3542
/**
3643
* Defines a CloudWatch event rule triggered when the build project state
3744
* changes. You can filter specific build status events using an event
@@ -57,30 +64,30 @@ export interface IProject extends IResource, iam.IGrantable {
5764
*
5865
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html
5966
*/
60-
onStateChange(name: string, target?: events.IRuleTarget, options?: events.RuleProps): events.Rule;
67+
onStateChange(id: string, options: events.OnEventOptions): events.Rule;
6168

6269
/**
6370
* Defines a CloudWatch event rule that triggers upon phase change of this
6471
* build project.
6572
*
6673
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html
6774
*/
68-
onPhaseChange(name: string, target?: events.IRuleTarget, options?: events.RuleProps): events.Rule;
75+
onPhaseChange(id: string, options: events.OnEventOptions): events.Rule;
6976

7077
/**
7178
* Defines an event rule which triggers when a build starts.
7279
*/
73-
onBuildStarted(name: string, target?: events.IRuleTarget, options?: events.RuleProps): events.Rule;
80+
onBuildStarted(id: string, options: events.OnEventOptions): events.Rule;
7481

7582
/**
7683
* Defines an event rule which triggers when a build fails.
7784
*/
78-
onBuildFailed(name: string, target?: events.IRuleTarget, options?: events.RuleProps): events.Rule;
85+
onBuildFailed(id: string, options: events.OnEventOptions): events.Rule;
7986

8087
/**
8188
* Defines an event rule which triggers when a build completes successfully.
8289
*/
83-
onBuildSucceeded(name: string, target?: events.IRuleTarget, options?: events.RuleProps): events.Rule;
90+
onBuildSucceeded(id: string, options: events.OnEventOptions): events.Rule;
8491

8592
/**
8693
* @returns a CloudWatch metric associated with this build project.
@@ -157,6 +164,23 @@ abstract class ProjectBase extends Resource implements IProject {
157164
/** The IAM service Role of this Project. */
158165
public abstract readonly role?: iam.IRole;
159166

167+
/**
168+
* Defines a CloudWatch event rule triggered when something happens with this project.
169+
*
170+
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html
171+
*/
172+
public onEvent(id: string, options: events.OnEventOptions): events.Rule {
173+
const rule = new events.Rule(this, id, options);
174+
rule.addTarget(options.target);
175+
rule.addEventPattern({
176+
source: ['aws.codebuild'],
177+
detail: {
178+
'project-name': [this.projectName]
179+
}
180+
});
181+
return rule;
182+
}
183+
160184
/**
161185
* Defines a CloudWatch event rule triggered when the build project state
162186
* changes. You can filter specific build status events using an event
@@ -182,17 +206,10 @@ abstract class ProjectBase extends Resource implements IProject {
182206
*
183207
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html
184208
*/
185-
public onStateChange(name: string, target?: events.IRuleTarget, options?: events.RuleProps) {
186-
const rule = new events.Rule(this, name, options);
187-
rule.addTarget(target);
209+
public onStateChange(id: string, options: events.OnEventOptions) {
210+
const rule = this.onEvent(id, options);
188211
rule.addEventPattern({
189-
source: ['aws.codebuild'],
190212
detailType: ['CodeBuild Build State Change'],
191-
detail: {
192-
'project-name': [
193-
this.projectName
194-
]
195-
}
196213
});
197214
return rule;
198215
}
@@ -203,17 +220,10 @@ abstract class ProjectBase extends Resource implements IProject {
203220
*
204221
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html
205222
*/
206-
public onPhaseChange(name: string, target?: events.IRuleTarget, options?: events.RuleProps) {
207-
const rule = new events.Rule(this, name, options);
208-
rule.addTarget(target);
223+
public onPhaseChange(id: string, options: events.OnEventOptions) {
224+
const rule = this.onEvent(id, options);
209225
rule.addEventPattern({
210-
source: ['aws.codebuild'],
211226
detailType: ['CodeBuild Build Phase Change'],
212-
detail: {
213-
'project-name': [
214-
this.projectName
215-
]
216-
}
217227
});
218228
return rule;
219229
}
@@ -224,8 +234,8 @@ abstract class ProjectBase extends Resource implements IProject {
224234
* To access fields from the event in the event target input,
225235
* use the static fields on the `StateChangeEvent` class.
226236
*/
227-
public onBuildStarted(name: string, target?: events.IRuleTarget, options?: events.RuleProps) {
228-
const rule = this.onStateChange(name, target, options);
237+
public onBuildStarted(id: string, options: events.OnEventOptions) {
238+
const rule = this.onStateChange(id, options);
229239
rule.addEventPattern({
230240
detail: {
231241
'build-status': ['IN_PROGRESS']
@@ -240,8 +250,8 @@ abstract class ProjectBase extends Resource implements IProject {
240250
* To access fields from the event in the event target input,
241251
* use the static fields on the `StateChangeEvent` class.
242252
*/
243-
public onBuildFailed(name: string, target?: events.IRuleTarget, options?: events.RuleProps) {
244-
const rule = this.onStateChange(name, target, options);
253+
public onBuildFailed(id: string, options: events.OnEventOptions) {
254+
const rule = this.onStateChange(id, options);
245255
rule.addEventPattern({
246256
detail: {
247257
'build-status': ['FAILED']
@@ -256,8 +266,8 @@ abstract class ProjectBase extends Resource implements IProject {
256266
* To access fields from the event in the event target input,
257267
* use the static fields on the `StateChangeEvent` class.
258268
*/
259-
public onBuildSucceeded(name: string, target?: events.IRuleTarget, options?: events.RuleProps) {
260-
const rule = this.onStateChange(name, target, options);
269+
public onBuildSucceeded(id: string, options: events.OnEventOptions) {
270+
const rule = this.onStateChange(id, options);
261271
rule.addEventPattern({
262272
detail: {
263273
'build-status': ['SUCCEEDED']

0 commit comments

Comments
 (0)