Skip to content

Commit

Permalink
use imageId prop if passed in but default to creating parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Lynch committed Jan 28, 2021
1 parent a6827b8 commit 2005fa7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
16 changes: 15 additions & 1 deletion src/constructs/autoscaling/asg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,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,6 +40,20 @@ describe("The GuAutoScalingGroup", () => {
});
});

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

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

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

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

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

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

Expand Down
11 changes: 6 additions & 5 deletions src/constructs/autoscaling/asg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { GuAmiParameter, GuInstanceTypeParameter } from "../core";
export interface GuAutoScalingGroupProps
extends Omit<AutoScalingGroupProps, "imageId" | "osType" | "machineImage" | "instanceType" | "userData"> {
instanceType?: InstanceType;
imageId?: string;
osType?: OperatingSystemType;
machineImage?: MachineImage;
userData: string;
Expand All @@ -22,18 +23,18 @@ export interface GuAutoScalingGroupProps

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

// 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 ??
new GuAmiParameter(scope, "AMI", {
description: "AMI ID",
}).valueAsString,
};
}

Expand Down

0 comments on commit 2005fa7

Please sign in to comment.