Skip to content

Commit

Permalink
Merge pull request #267 from guardian/aa-asg-userdata
Browse files Browse the repository at this point in the history
feat: tweak type of GuASG's userData
  • Loading branch information
akash1810 authored Feb 23, 2021
2 parents eabaf5c + f0cd0c9 commit 574fe9c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
12 changes: 7 additions & 5 deletions src/constructs/autoscaling/asg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ import { GuAmiParameter } from "../core";
import { GuSecurityGroup } from "../ec2";
import { GuApplicationTargetGroup } from "../loadbalancing";
import type { GuAutoScalingGroupProps } from "./asg";
import { GuAutoScalingGroup } from "./asg";
import { GuAutoScalingGroup, GuUserData } from "./";

describe("The GuAutoScalingGroup", () => {
const vpc = Vpc.fromVpcAttributes(new Stack(), "VPC", {
vpcId: "test",
availabilityZones: [""],
publicSubnetIds: [""],
});

const { userData } = new GuUserData().addCommands(...["service some-dependency start", "service my-app start"]);

const defaultProps: GuAutoScalingGroupProps = {
vpc,
userData: "user data",
userData,
stageDependentProps: {
[Stage.CODE]: {
minimumInstances: 1,
Expand All @@ -34,7 +37,7 @@ describe("The GuAutoScalingGroup", () => {
test("adds the AMI parameter if no imageId prop provided", () => {
const stack = simpleGuStackForTesting();

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

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

Expand All @@ -55,7 +58,6 @@ describe("The GuAutoScalingGroup", () => {

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

Expand Down Expand Up @@ -112,7 +114,7 @@ describe("The GuAutoScalingGroup", () => {

expect(stack).toHaveResource("AWS::AutoScaling::LaunchConfiguration", {
UserData: {
"Fn::Base64": "user data",
"Fn::Base64": "#!/bin/bash\nservice some-dependency start\nservice my-app start",
},
});
});
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 @@ -27,9 +27,8 @@ export interface GuAutoScalingGroupProps
stageDependentProps: GuStageDependentAsgProps;
instanceType?: InstanceType;
imageId?: GuAmiParameter;
osType?: OperatingSystemType;
machineImage?: MachineImage;
userData: string;
userData: UserData | string;
additionalSecurityGroups?: ISecurityGroup[];
targetGroup?: ApplicationTargetGroup;
overrideId?: boolean;
Expand Down Expand Up @@ -84,13 +83,15 @@ function wireStageDependentProps(stack: GuStack, stageDependentProps: GuStageDep

export class GuAutoScalingGroup extends AutoScalingGroup {
constructor(scope: GuStack, id: string, props: GuAutoScalingGroupProps) {
const userData = props.userData instanceof UserData ? props.userData : UserData.custom(props.userData);

// 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),
osType: OperatingSystemType.LINUX,
userData,
imageId:
props.imageId?.valueAsString ??
new GuAmiParameter(scope, "AMI", {
Expand All @@ -104,7 +105,7 @@ export class GuAutoScalingGroup extends AutoScalingGroup {
...wireStageDependentProps(scope, props.stageDependentProps),
machineImage: { getImage: getImage },
instanceType: props.instanceType ?? new InstanceType(new GuInstanceTypeParameter(scope).valueAsString),
userData: UserData.custom(props.userData),
userData,

// Do not use the default AWS security group which allows egress on any port.
// Favour HTTPS only egress rules by default.
Expand Down
1 change: 1 addition & 0 deletions src/constructs/autoscaling/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./asg";
export * from "./user-data";
20 changes: 20 additions & 0 deletions src/constructs/autoscaling/user-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { S3DownloadOptions } from "@aws-cdk/aws-ec2";
import { UserData } from "@aws-cdk/aws-ec2";

export class GuUserData {
private _userData = UserData.forLinux();

get userData(): UserData {
return this._userData;
}

addCommands(...commands: string[]): GuUserData {
this._userData.addCommands(...commands);
return this;
}

addS3DownloadCommand(params: S3DownloadOptions): GuUserData {
this._userData.addS3DownloadCommand(params);
return this;
}
}

0 comments on commit 574fe9c

Please sign in to comment.