Skip to content

Commit

Permalink
fix(codedeploy): ServerDeploymentGroup takes AutoScalingGroup instead…
Browse files Browse the repository at this point in the history
… of IAutoScalingGroup (#9252)

Fixes #9175

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
skinny85 authored Aug 8, 2020
1 parent a348f9b commit 9ff55ae
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
3 changes: 3 additions & 0 deletions allowed-breaking-changes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ removed:@aws-cdk/cdk-assets-schema.FileDestination
removed:@aws-cdk/cdk-assets-schema.FileSource
removed:@aws-cdk/cdk-assets-schema.ManifestFile
removed:@aws-cdk/cdk-assets-schema.FileAssetPackaging

changed-type:@aws-cdk/aws-codedeploy.IServerDeploymentGroup.autoScalingGroups
changed-type:@aws-cdk/aws-codedeploy.ServerDeploymentGroup.autoScalingGroups
31 changes: 23 additions & 8 deletions packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,9 @@ abstract class AutoScalingGroupBase extends Resource implements IAutoScalingGrou

public abstract autoScalingGroupName: string;
public abstract autoScalingGroupArn: string;
public abstract readonly osType: ec2.OperatingSystemType;
protected albTargetGroup?: elbv2.ApplicationTargetGroup;
public readonly grantPrincipal: iam.IPrincipal = new iam.UnknownPrincipal({ resource: this });

/**
* Send a message to either an SQS queue or SNS topic when instances launch or terminate
Expand Down Expand Up @@ -490,6 +492,10 @@ abstract class AutoScalingGroupBase extends Resource implements IAutoScalingGrou
public scaleOnMetric(id: string, props: BasicStepScalingPolicyProps): StepScalingPolicy {
return new StepScalingPolicy(this, id, { ...props, autoScalingGroup: this });
}

public addUserData(..._commands: string[]): void {
// do nothing
}
}

/**
Expand All @@ -508,8 +514,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
elb.ILoadBalancerTarget,
ec2.IConnectable,
elbv2.IApplicationLoadBalancerTarget,
elbv2.INetworkLoadBalancerTarget,
iam.IGrantable {
elbv2.INetworkLoadBalancerTarget {

public static fromAutoScalingGroupName(scope: Construct, id: string, autoScalingGroupName: string): IAutoScalingGroup {
class Import extends AutoScalingGroupBase {
Expand All @@ -519,6 +524,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
resource: 'autoScalingGroup:*:autoScalingGroupName',
resourceName: this.autoScalingGroupName,
});
public readonly osType = ec2.OperatingSystemType.UNKNOWN;
}

return new Import(scope, id);
Expand Down Expand Up @@ -760,11 +766,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
return { targetType: elbv2.TargetType.INSTANCE };
}

/**
* Add command to the startup script of fleet instances.
* The command must be in the scripting language supported by the fleet's OS (i.e. Linux/Windows).
*/
public addUserData(...commands: string[]) {
public addUserData(...commands: string[]): void {
this.userData.addCommands(...commands);
}

Expand Down Expand Up @@ -1121,7 +1123,7 @@ function validatePercentage(x?: number): number | undefined {
/**
* An AutoScalingGroup
*/
export interface IAutoScalingGroup extends IResource {
export interface IAutoScalingGroup extends IResource, iam.IGrantable {
/**
* The name of the AutoScalingGroup
* @attribute
Expand All @@ -1134,6 +1136,19 @@ export interface IAutoScalingGroup extends IResource {
*/
readonly autoScalingGroupArn: string;

/**
* The operating system family that the instances in this auto-scaling group belong to.
* Is 'UNKNOWN' for imported ASGs.
*/
readonly osType: ec2.OperatingSystemType;

/**
* Add command to the startup script of fleet instances.
* The command must be in the scripting language supported by the fleet's OS (i.e. Linux/Windows).
* Does nothing for imported ASGs.
*/
addUserData(...commands: string[]): void;

/**
* Send a message to either an SQS queue or SNS topic when instances launch or terminate
*/
Expand Down
14 changes: 7 additions & 7 deletions packages/@aws-cdk/aws-codedeploy/lib/server/deployment-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface IServerDeploymentGroup extends cdk.IResource {
*/
readonly deploymentGroupArn: string;
readonly deploymentConfig: IServerDeploymentConfig;
readonly autoScalingGroups?: autoscaling.AutoScalingGroup[];
readonly autoScalingGroups?: autoscaling.IAutoScalingGroup[];
}

/**
Expand Down Expand Up @@ -69,7 +69,7 @@ abstract class ServerDeploymentGroupBase extends cdk.Resource implements IServer
public abstract readonly deploymentGroupName: string;
public abstract readonly deploymentGroupArn: string;
public readonly deploymentConfig: IServerDeploymentConfig;
public abstract readonly autoScalingGroups?: autoscaling.AutoScalingGroup[];
public abstract readonly autoScalingGroups?: autoscaling.IAutoScalingGroup[];

constructor(scope: cdk.Construct, id: string, deploymentConfig?: IServerDeploymentConfig, props?: cdk.ResourceProps) {
super(scope, id, props);
Expand Down Expand Up @@ -171,7 +171,7 @@ export interface ServerDeploymentGroupProps {
*
* @default []
*/
readonly autoScalingGroups?: autoscaling.AutoScalingGroup[];
readonly autoScalingGroups?: autoscaling.IAutoScalingGroup[];

/**
* If you've provided any auto-scaling groups with the {@link #autoScalingGroups} property,
Expand Down Expand Up @@ -258,7 +258,7 @@ export class ServerDeploymentGroup extends ServerDeploymentGroupBase {
public readonly deploymentGroupArn: string;
public readonly deploymentGroupName: string;

private readonly _autoScalingGroups: autoscaling.AutoScalingGroup[];
private readonly _autoScalingGroups: autoscaling.IAutoScalingGroup[];
private readonly installAgent: boolean;
private readonly codeDeployBucket: s3.IBucket;
private readonly alarms: cloudwatch.IAlarm[];
Expand Down Expand Up @@ -333,16 +333,16 @@ export class ServerDeploymentGroup extends ServerDeploymentGroupBase {
this.alarms.push(alarm);
}

public get autoScalingGroups(): autoscaling.AutoScalingGroup[] | undefined {
public get autoScalingGroups(): autoscaling.IAutoScalingGroup[] | undefined {
return this._autoScalingGroups.slice();
}

private addCodeDeployAgentInstallUserData(asg: autoscaling.AutoScalingGroup): void {
private addCodeDeployAgentInstallUserData(asg: autoscaling.IAutoScalingGroup): void {
if (!this.installAgent) {
return;
}

this.codeDeployBucket.grantRead(asg.role, 'latest/*');
this.codeDeployBucket.grantRead(asg, 'latest/*');

switch (asg.osType) {
case ec2.OperatingSystemType.LINUX:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class CodeDeployServerDeployAction extends Action {

// grant the ASG Role permissions to read from the Pipeline Bucket
for (const asg of this.deploymentGroup.autoScalingGroups || []) {
options.bucket.grantRead(asg.role);
options.bucket.grantRead(asg);
}

// the Action's Role needs to read from the Bucket to get artifacts
Expand Down
5 changes: 5 additions & 0 deletions packages/@aws-cdk/aws-ec2/lib/machine-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ export class GenericWindowsImage implements IMachineImage {
export enum OperatingSystemType {
LINUX,
WINDOWS,
/**
* Used when the type of the operating system is not known
* (for example, for imported Auto-Scaling Groups).
*/
UNKNOWN,
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-ec2/lib/user-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export abstract class UserData {
switch (os) {
case OperatingSystemType.LINUX: return UserData.forLinux();
case OperatingSystemType.WINDOWS: return UserData.forWindows();
case OperatingSystemType.UNKNOWN: throw new Error('Cannot determine UserData for unknown operating system type');
}
}

Expand Down Expand Up @@ -275,4 +276,4 @@ class CustomUserData extends UserData {
public addSignalOnExitCommand(): void {
throw new Error('CustomUserData does not support addSignalOnExitCommand, use UserData.forLinux() or UserData.forWindows() instead.');
}
}
}

0 comments on commit 9ff55ae

Please sign in to comment.