Skip to content

Commit

Permalink
feat(autoscaling): bring your own IAM role (#1727)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Elad Ben-Israel authored Feb 11, 2019
1 parent 016a5d6 commit 2016b8d
Show file tree
Hide file tree
Showing 4 changed files with 675 additions and 2 deletions.
19 changes: 17 additions & 2 deletions packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ export interface AutoScalingGroupProps extends CommonAutoScalingGroupProps {
* AMI to launch
*/
machineImage: ec2.IMachineImageSource;

/**
* An IAM role to associate with the instance profile assigned to this Auto Scaling Group.
*
* The role must be assumable by the service principal `ec2.amazonaws.com`:
*
* @example
*
* const role = new iam.Role(this, 'MyRole', {
* assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com')
* });
*
* @default A role will automatically be created, it can be accessed via the `role` property
*/
role?: iam.IRole;
}

/**
Expand Down Expand Up @@ -187,7 +202,7 @@ export class AutoScalingGroup extends cdk.Construct implements IAutoScalingGroup
/**
* The IAM role assumed by instances of this fleet.
*/
public readonly role: iam.Role;
public readonly role: iam.IRole;

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

this.role = new iam.Role(this, 'InstanceRole', {
this.role = props.role || new iam.Role(this, 'InstanceRole', {
assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com')
});

Expand Down
Loading

0 comments on commit 2016b8d

Please sign in to comment.