diff --git a/src/constructs/autoscaling/asg.ts b/src/constructs/autoscaling/asg.ts index 261514435..21679f7da 100644 --- a/src/constructs/autoscaling/asg.ts +++ b/src/constructs/autoscaling/asg.ts @@ -3,7 +3,7 @@ import { AutoScalingGroup } from "@aws-cdk/aws-autoscaling"; import type { ISecurityGroup, MachineImage, MachineImageConfig } from "@aws-cdk/aws-ec2"; import { InstanceType, OperatingSystemType, UserData } from "@aws-cdk/aws-ec2"; import type { ApplicationTargetGroup } from "@aws-cdk/aws-elasticloadbalancingv2"; -import type { GuStack, GuStageVariable } from "../core"; +import type { GuStack, GuStageDependentValue } from "../core"; import { GuAmiParameter, GuInstanceTypeParameter } from "../core"; // Since we want to override the types of what gets passed in for the below props, @@ -55,24 +55,24 @@ interface AwsAsgCapacityProps { maxCapacity: number; } -function wireCapacityProps(scope: GuStack, capacity: GuAsgCapacityProps): AwsAsgCapacityProps { +function wireCapacityProps(stack: GuStack, capacity: GuAsgCapacityProps): AwsAsgCapacityProps { const minInstancesKey = "minInstances"; const maxInstancesKey = "maxInstances"; - const minInstances: GuStageVariable = { + const minInstances: GuStageDependentValue = { variableName: minInstancesKey, codeValue: capacity.minimumCodeInstances, prodValue: capacity.minimumProdInstances, }; - const maxInstances: GuStageVariable = { + const maxInstances: GuStageDependentValue = { variableName: maxInstancesKey, codeValue: capacity.maximumCodeInstances ?? capacity.minimumCodeInstances * 2, prodValue: capacity.maximumProdInstances ?? capacity.minimumProdInstances * 2, }; - scope.mappings.addStageVariable(minInstances); - scope.mappings.addStageVariable(maxInstances); + stack.setStageDependentValue(minInstances); + stack.setStageDependentValue(maxInstances); return { - minCapacity: (scope.mappings.findInMap(scope.stage, minInstancesKey) as unknown) as number, - maxCapacity: (scope.mappings.findInMap(scope.stage, maxInstancesKey) as unknown) as number, + minCapacity: stack.getStageDependentValue(minInstancesKey), + maxCapacity: stack.getStageDependentValue(maxInstancesKey), }; } diff --git a/src/constructs/core/mappings.ts b/src/constructs/core/mappings.ts index 1dc3300eb..123881efd 100644 --- a/src/constructs/core/mappings.ts +++ b/src/constructs/core/mappings.ts @@ -1,17 +1,13 @@ import { CfnMapping } from "@aws-cdk/core"; import type { GuStack } from "./stack"; -export interface GuStageVariable { +export interface GuStageDependentValue { variableName: string; codeValue: unknown; prodValue: unknown; } export class GuStageMapping extends CfnMapping { - addStageVariable(stageVariable: GuStageVariable): void { - this.setValue("CODE", stageVariable.variableName, stageVariable.codeValue); - this.setValue("PROD", stageVariable.variableName, stageVariable.prodValue); - } constructor(scope: GuStack, id: string = "stage-mapping") { super(scope, id); } diff --git a/src/constructs/core/stack.ts b/src/constructs/core/stack.ts index 19e6a4846..53a08b238 100644 --- a/src/constructs/core/stack.ts +++ b/src/constructs/core/stack.ts @@ -1,6 +1,7 @@ import type { App, StackProps } from "@aws-cdk/core"; import { Stack, Tags } from "@aws-cdk/core"; import { TrackingTag } from "../../constants/library-info"; +import type { GuStageDependentValue } from "./mappings"; import { GuStageMapping } from "./mappings"; import { GuStackParameter, GuStageParameter } from "./parameters"; @@ -63,6 +64,15 @@ export class GuStack extends Stack { return this._mappings ?? (this._mappings = new GuStageMapping(this)); } + setStageDependentValue(stageVariable: GuStageDependentValue): void { + this.mappings.setValue("CODE", stageVariable.variableName, stageVariable.codeValue); + this.mappings.setValue("PROD", stageVariable.variableName, stageVariable.prodValue); + } + + getStageDependentValue(key: string): T { + return (this.mappings.findInMap(this.stage, key) as unknown) as T; + } + /** * A helper function to add a tag to all resources in a stack. *