Skip to content

Commit 7e6ad84

Browse files
authoredNov 21, 2018
fix(aws-autoscaling): Add hook ordering dependency (#1218)
Add an ordering dependency between the LifecycleHook and the role policy that grants permissions to perform the action (publish to a topic or queue). Without this ordering dependency, the template might fail to deploy. Fixes #1212.
1 parent d3b8448 commit 7e6ad84

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed
 

‎packages/@aws-cdk/aws-autoscaling/lib/lifecycle-hook.ts

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import iam = require('@aws-cdk/aws-iam');
33
import cdk = require('@aws-cdk/cdk');
44
import { IAutoScalingGroup } from './auto-scaling-group';
55
import { cloudformation } from './autoscaling.generated';
6+
import { LazyDependable } from './util';
67

78
/**
89
* Basic properties for a lifecycle hook
@@ -95,6 +96,14 @@ export class LifecycleHook extends cdk.Construct implements api.ILifecycleHook {
9596
roleArn: this.role.roleArn,
9697
});
9798

99+
// A LifecycleHook resource is going to do a permissions test upon creation,
100+
// so we have to make sure the role has full permissions before creating the
101+
// lifecycle hook.
102+
// The LazyDependable makes sure we take a dependency on anything that gets
103+
// added to the dependencyElements array, even if it happens after this
104+
// point.
105+
resource.addDependency(new LazyDependable(this.role));
106+
98107
this.lifecycleHookName = resource.lifecycleHookName;
99108
}
100109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import cdk = require('@aws-cdk/cdk');
2+
/**
3+
* Allow lazy evaluation of a list of dependables
4+
*/
5+
export class LazyDependable implements cdk.IDependable {
6+
constructor(private readonly dependableSource: cdk.IDependable) {
7+
}
8+
9+
public get dependencyElements(): cdk.IDependable[] {
10+
return this.dependableSource.dependencyElements;
11+
}
12+
}

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, haveResource } from '@aws-cdk/assert';
1+
import { expect, haveResource, ResourcePart } from '@aws-cdk/assert';
22
import autoscaling_api = require('@aws-cdk/aws-autoscaling-api');
33
import ec2 = require('@aws-cdk/aws-ec2');
44
import iam = require('@aws-cdk/aws-iam');
@@ -31,6 +31,14 @@ export = {
3131
NotificationTargetARN: "target:arn",
3232
}));
3333

34+
// Lifecycle Hook has a dependency on the policy object
35+
expect(stack).to(haveResource('AWS::AutoScaling::LifecycleHook', {
36+
DependsOn: [
37+
"ASGLifecycleHookTransitionRole3AAA6BB7",
38+
"ASGLifecycleHookTransitionRoleDefaultPolicy2E50C7DB"
39+
]
40+
}, ResourcePart.CompleteDefinition));
41+
3442
expect(stack).to(haveResource('AWS::IAM::Role', {
3543
AssumeRolePolicyDocument: {
3644
Statement: [

‎packages/@aws-cdk/aws-ecs/test/ec2/integ.lb-awsvpc-nw.expected.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,11 @@
686686
"Arn"
687687
]
688688
}
689-
}
689+
},
690+
"DependsOn": [
691+
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookRoleA38EC83B",
692+
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookRoleDefaultPolicy75002F88"
693+
]
690694
},
691695
"TaskDefTaskRole1EDB4A67": {
692696
"Type": "AWS::IAM::Role",

‎packages/@aws-cdk/aws-ecs/test/ec2/integ.lb-bridge-nw.expected.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,11 @@
707707
"Arn"
708708
]
709709
}
710-
}
710+
},
711+
"DependsOn": [
712+
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookRoleA38EC83B",
713+
"EcsClusterDefaultAutoScalingGroupLifecycleHookDrainHookRoleDefaultPolicy75002F88"
714+
]
711715
},
712716
"TaskDefTaskRole1EDB4A67": {
713717
"Type": "AWS::IAM::Role",

0 commit comments

Comments
 (0)