11import * as cdk from 'aws-cdk-lib' ;
2+ import { ScalableTarget , CfnScalableTarget , CfnScalingPolicy } from 'aws-cdk-lib/aws-applicationautoscaling' ;
23import * as ec2 from 'aws-cdk-lib/aws-ec2' ;
34import * as ecs from 'aws-cdk-lib/aws-ecs' ;
45import { NetworkLoadBalancedTaskImageOptions } from 'aws-cdk-lib/aws-ecs-patterns' ;
@@ -8,7 +9,6 @@ import * as logs from 'aws-cdk-lib/aws-logs';
89import * as cloudMap from 'aws-cdk-lib/aws-servicediscovery' ;
910import { Construct } from 'constructs' ;
1011import { AuthProvider } from './supabase-auth-provider' ;
11- import { SupabaseDatabase } from './supabase-db' ;
1212import { FargateStack } from './supabase-stack' ;
1313
1414interface SupabaseTaskImageOptions extends NetworkLoadBalancedTaskImageOptions {
@@ -30,6 +30,7 @@ export interface BaseFargateServiceProps {
3030export interface AutoScalingFargateServiceProps extends BaseFargateServiceProps {
3131 minTaskCount ?: number ;
3232 maxTaskCount ?: number ;
33+ highAvailability ?: cdk . CfnCondition ;
3334}
3435
3536export interface TargetGroupProps {
@@ -162,55 +163,50 @@ export class BaseFargateService extends Construct {
162163}
163164
164165export class AutoScalingFargateService extends BaseFargateService {
165- readonly cfnParameters : {
166- taskSize : cdk . CfnParameter ;
167- minTaskCount : cdk . CfnParameter ;
168- maxTaskCount : cdk . CfnParameter ;
169- } ;
166+ readonly taskSize : cdk . CfnParameter ;
167+
170168 constructor ( scope : FargateStack , id : string , props : AutoScalingFargateServiceProps ) {
171169 super ( scope , id , props ) ;
172170
173- const { minTaskCount, maxTaskCount } = props ;
174-
175- this . cfnParameters = {
176- taskSize : new cdk . CfnParameter ( this , 'TaskSize' , {
177- description : 'Fargare task size' ,
178- type : 'String' ,
179- default : 'micro' ,
180- allowedValues : [ 'micro' , 'small' , 'medium' , 'large' , 'xlarge' , '2xlarge' , '4xlarge' ] ,
181- } ) ,
182- minTaskCount : new cdk . CfnParameter ( this , 'MinTaskCount' , {
183- description : 'Minimum fargate task count' ,
184- type : 'Number' ,
185- default : ( typeof minTaskCount == 'undefined' ) ? 1 : minTaskCount ,
186- minValue : 0 ,
187- } ) ,
188- maxTaskCount : new cdk . CfnParameter ( this , 'MaxTaskCount' , {
189- description : 'Maximum fargate task count' ,
190- type : 'Number' ,
191- default : ( typeof maxTaskCount == 'undefined' ) ? 20 : maxTaskCount ,
192- minValue : 0 ,
193- } ) ,
194- } ;
195-
196- const cpu = scope . taskSizeMapping . findInMap ( this . cfnParameters . taskSize . valueAsString , 'cpu' ) ;
197- const memory = scope . taskSizeMapping . findInMap ( this . cfnParameters . taskSize . valueAsString , 'memory' ) ;
198-
199- ( this . service . taskDefinition . node . defaultChild as ecs . CfnTaskDefinition ) . addPropertyOverride ( 'Cpu' , cpu ) ;
200- ( this . service . taskDefinition . node . defaultChild as ecs . CfnTaskDefinition ) . addPropertyOverride ( 'Memory' , memory ) ;
201-
202- const serviceDisabled = new cdk . CfnCondition ( this , 'ServiceDisabled' , { expression : cdk . Fn . conditionEquals ( this . cfnParameters . minTaskCount , '0' ) } ) ;
203- ( this . service . node . defaultChild as ecs . CfnService ) . addPropertyOverride ( 'DesiredCount' , cdk . Fn . conditionIf ( serviceDisabled . logicalId , 0 , cdk . Aws . NO_VALUE ) ) ;
171+ const { minTaskCount, maxTaskCount, highAvailability } = props ;
172+
173+ this . taskSize = new cdk . CfnParameter ( this , 'TaskSize' , {
174+ description : 'Fargare task size' ,
175+ type : 'String' ,
176+ default : 'medium' ,
177+ allowedValues : [ 'none' , 'micro' , 'small' , 'medium' , 'large' , 'xlarge' , '2xlarge' , '4xlarge' ] ,
178+ } ) ;
179+
180+ /** CFn task definition to override */
181+ const taskDef = this . service . taskDefinition . node . defaultChild as ecs . CfnTaskDefinition ;
182+
183+ const cpu = scope . taskSizeMapping . findInMap ( this . taskSize . valueAsString , 'cpu' ) ;
184+ const memory = scope . taskSizeMapping . findInMap ( this . taskSize . valueAsString , 'memory' ) ;
185+
186+ taskDef . addPropertyOverride ( 'Cpu' , cpu ) ;
187+ taskDef . addPropertyOverride ( 'Memory' , memory ) ;
204188
205189 const autoScaling = this . service . autoScaleTaskCount ( {
206- minCapacity : this . cfnParameters . minTaskCount . valueAsNumber ,
207- maxCapacity : this . cfnParameters . maxTaskCount . valueAsNumber ,
190+ minCapacity : minTaskCount ?? 2 ,
191+ maxCapacity : maxTaskCount ?? 20 ,
208192 } ) ;
209193
210194 autoScaling . scaleOnCpuUtilization ( 'ScaleOnCpu' , {
211195 targetUtilizationPercent : 50 ,
212196 scaleInCooldown : cdk . Duration . seconds ( 60 ) ,
213197 scaleOutCooldown : cdk . Duration . seconds ( 60 ) ,
214198 } ) ;
199+
200+ /** CFn condition for ECS service */
201+ const serviceEnabled = new cdk . CfnCondition ( this , 'ServiceEnabled' , { expression : cdk . Fn . conditionNot ( cdk . Fn . conditionEquals ( this . taskSize , 'none' ) ) } ) ;
202+ ( this . service . node . defaultChild as ecs . CfnService ) . addPropertyOverride ( 'DesiredCount' , cdk . Fn . conditionIf ( serviceEnabled . logicalId , cdk . Aws . NO_VALUE , 0 ) ) ;
203+
204+ if ( typeof highAvailability != 'undefined' ) {
205+ /** CFn condition for auto-scaling */
206+ const autoScalingEnabled = new cdk . CfnCondition ( this , 'AutoScalingEnabled' , { expression : cdk . Fn . conditionAnd ( serviceEnabled , highAvailability ) } ) ;
207+ const target = autoScaling . node . findChild ( 'Target' ) as ScalableTarget ;
208+ ( target . node . defaultChild as CfnScalableTarget ) . cfnOptions . condition = autoScalingEnabled ;
209+ ( target . node . findChild ( 'ScaleOnCpu' ) . node . defaultChild as CfnScalingPolicy ) . cfnOptions . condition = autoScalingEnabled ;
210+ }
215211 }
216212}
0 commit comments