Skip to content

Commit

Permalink
take an instanceType prop or default to new param
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Lynch committed Jan 28, 2021
1 parent ebc619a commit a6827b8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
18 changes: 16 additions & 2 deletions src/constructs/autoscaling/asg.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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";
Expand Down Expand Up @@ -40,7 +40,7 @@ describe("The GuAutoScalingGroup", () => {
});
});

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

new GuAutoScalingGroup(stack, "AutoscalingGroup", defaultProps);
Expand All @@ -60,6 +60,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
5 changes: 2 additions & 3 deletions src/constructs/autoscaling/asg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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;
osType?: OperatingSystemType;
machineImage?: MachineImage;
userData: string;
Expand All @@ -25,8 +26,6 @@ export class GuAutoScalingGroup extends AutoScalingGroup {
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
Expand All @@ -41,7 +40,7 @@ export class GuAutoScalingGroup extends AutoScalingGroup {
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 a6827b8

Please sign in to comment.