Skip to content

Commit

Permalink
Merge pull request #213 from guardian/jl/asg-defaults
Browse files Browse the repository at this point in the history
Allow imageId and instanceType to be passed into ASG
  • Loading branch information
Jamie Lynch authored Jan 29, 2021
2 parents ebc619a + 10502b0 commit 181170f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
42 changes: 39 additions & 3 deletions src/constructs/autoscaling/asg.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import "@aws-cdk/assert/jest";
import { SynthUtils } from "@aws-cdk/assert/lib/synth-utils";
import { Vpc } from "@aws-cdk/aws-ec2";
import { InstanceType, Vpc } from "@aws-cdk/aws-ec2";
import { ApplicationProtocol } from "@aws-cdk/aws-elasticloadbalancingv2";
import { Stack } from "@aws-cdk/core";
import { simpleGuStackForTesting } from "../../../test/utils/simple-gu-stack";
import type { SynthedStack } from "../../../test/utils/synthed-stack";
import { GuAmiParameter } from "../core";
import { GuSecurityGroup } from "../ec2";
import { GuApplicationTargetGroup } from "../loadbalancing";
import type { GuAutoScalingGroupProps } from "./asg";
Expand All @@ -21,7 +22,7 @@ describe("The GuAutoScalingGroup", () => {
userData: "user data",
};

test("adds the AMI parameter", () => {
test("adds the AMI parameter if no imageId prop provided", () => {
const stack = simpleGuStackForTesting();

new GuAutoScalingGroup(stack, "AutoscalingGroup", { ...defaultProps, osType: 1 });
Expand All @@ -40,7 +41,28 @@ describe("The GuAutoScalingGroup", () => {
});
});

test("adds the instanceType parameter", () => {
test("does not add the AMI parameter if an imageId prop provided", () => {
const stack = simpleGuStackForTesting();

new GuAutoScalingGroup(stack, "AutoscalingGroup", {
...defaultProps,
osType: 1,
imageId: new GuAmiParameter(stack, "CustomAMI", {}),
});

const json = SynthUtils.toCloudFormation(stack) as SynthedStack;

expect(Object.keys(json.Parameters)).not.toContain("AMI");
expect(Object.keys(json.Parameters)).toContain("CustomAMI");

expect(stack).toHaveResource("AWS::AutoScaling::LaunchConfiguration", {
ImageId: {
Ref: "CustomAMI",
},
});
});

test("adds the instanceType parameter if none provided", () => {
const stack = simpleGuStackForTesting();

new GuAutoScalingGroup(stack, "AutoscalingGroup", defaultProps);
Expand All @@ -60,6 +82,20 @@ describe("The GuAutoScalingGroup", () => {
});
});

test("does not create the instanceType parameter if value is provided", () => {
const stack = simpleGuStackForTesting();

new GuAutoScalingGroup(stack, "AutoscalingGroup", { ...defaultProps, instanceType: new InstanceType("t3.small") });

const json = SynthUtils.toCloudFormation(stack) as SynthedStack;

expect(Object.keys(json.Parameters)).not.toContain(InstanceType);

expect(stack).toHaveResource("AWS::AutoScaling::LaunchConfiguration", {
InstanceType: "t3.small",
});
});

test("correctly sets the user data using prop", () => {
const stack = simpleGuStackForTesting();

Expand Down
16 changes: 8 additions & 8 deletions src/constructs/autoscaling/asg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { GuAmiParameter, GuInstanceTypeParameter } from "../core";
// https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys
export interface GuAutoScalingGroupProps
extends Omit<AutoScalingGroupProps, "imageId" | "osType" | "machineImage" | "instanceType" | "userData"> {
instanceType?: InstanceType;
imageId?: GuAmiParameter;
osType?: OperatingSystemType;
machineImage?: MachineImage;
userData: string;
Expand All @@ -21,27 +23,25 @@ export interface GuAutoScalingGroupProps

export class GuAutoScalingGroup extends AutoScalingGroup {
constructor(scope: GuStack, id: string, props: GuAutoScalingGroupProps) {
const imageId = new GuAmiParameter(scope, "AMI", {
description: "AMI ID",
});

const instanceType = new GuInstanceTypeParameter(scope);

// We need to override getImage() so that we can pass in the AMI as a parameter
// Otherwise, MachineImage.lookup({ name: 'some str' }) would work as long
// as the name is hard-coded
function getImage(): MachineImageConfig {
return {
osType: props.osType ?? OperatingSystemType.LINUX,
userData: UserData.custom(props.userData),
imageId: imageId.valueAsString,
imageId:
props.imageId?.valueAsString ??
new GuAmiParameter(scope, "AMI", {
description: "AMI ID",
}).valueAsString,
};
}

const mergedProps = {
...props,
machineImage: { getImage: getImage },
instanceType: new InstanceType(instanceType.valueAsString),
instanceType: props.instanceType ?? new InstanceType(new GuInstanceTypeParameter(scope).valueAsString),
userData: UserData.custom(props.userData),
};

Expand Down

0 comments on commit 181170f

Please sign in to comment.