Skip to content

Commit 2016b8d

Browse files
author
Elad Ben-Israel
authored
feat(autoscaling): bring your own IAM role (#1727)
Allow specifying an IAM role (`IRole`) when defining an AutoScalingGroup. This allows either passing a role created in the same stack or passing in an imported role. Fixes #1701
1 parent 016a5d6 commit 2016b8d

File tree

4 files changed

+675
-2
lines changed

4 files changed

+675
-2
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,21 @@ export interface AutoScalingGroupProps extends CommonAutoScalingGroupProps {
159159
* AMI to launch
160160
*/
161161
machineImage: ec2.IMachineImageSource;
162+
163+
/**
164+
* An IAM role to associate with the instance profile assigned to this Auto Scaling Group.
165+
*
166+
* The role must be assumable by the service principal `ec2.amazonaws.com`:
167+
*
168+
* @example
169+
*
170+
* const role = new iam.Role(this, 'MyRole', {
171+
* assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com')
172+
* });
173+
*
174+
* @default A role will automatically be created, it can be accessed via the `role` property
175+
*/
176+
role?: iam.IRole;
162177
}
163178

164179
/**
@@ -187,7 +202,7 @@ export class AutoScalingGroup extends cdk.Construct implements IAutoScalingGroup
187202
/**
188203
* The IAM role assumed by instances of this fleet.
189204
*/
190-
public readonly role: iam.Role;
205+
public readonly role: iam.IRole;
191206

192207
/**
193208
* Name of the AutoScalingGroup
@@ -217,7 +232,7 @@ export class AutoScalingGroup extends cdk.Construct implements IAutoScalingGroup
217232
this.securityGroups.push(this.securityGroup);
218233
this.apply(new cdk.Tag(NAME_TAG, this.node.path));
219234

220-
this.role = new iam.Role(this, 'InstanceRole', {
235+
this.role = props.role || new iam.Role(this, 'InstanceRole', {
221236
assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com')
222237
});
223238

0 commit comments

Comments
 (0)