From a6827b8e4989c64cd4439e733561c910af72007f Mon Sep 17 00:00:00 2001 From: Jamie Lynch Date: Thu, 28 Jan 2021 14:14:10 +0000 Subject: [PATCH] take an instanceType prop or default to new param --- src/constructs/autoscaling/asg.test.ts | 18 ++++++++++++++++-- src/constructs/autoscaling/asg.ts | 5 ++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/constructs/autoscaling/asg.test.ts b/src/constructs/autoscaling/asg.test.ts index 94beb24d19..cfbf7b2967 100644 --- a/src/constructs/autoscaling/asg.test.ts +++ b/src/constructs/autoscaling/asg.test.ts @@ -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"; @@ -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); @@ -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(); diff --git a/src/constructs/autoscaling/asg.ts b/src/constructs/autoscaling/asg.ts index 65e791b0ba..5c8fde85c3 100644 --- a/src/constructs/autoscaling/asg.ts +++ b/src/constructs/autoscaling/asg.ts @@ -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 { + instanceType?: InstanceType; osType?: OperatingSystemType; machineImage?: MachineImage; userData: string; @@ -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 @@ -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), };