diff --git a/dist/fargate.d.ts b/dist/fargate.d.ts index 6e21f86..c24248c 100644 --- a/dist/fargate.d.ts +++ b/dist/fargate.d.ts @@ -14,6 +14,8 @@ export interface TaskConfiguration { healthCheckGracePeriod?: cdk.Duration; cpu?: number; desiredCount?: number; + autoScalingCpuTarget?: number; + maxCount?: number; environment?: Record; secrets?: Record; } diff --git a/dist/fargate.js b/dist/fargate.js index a2c144f..9e2c879 100644 --- a/dist/fargate.js +++ b/dist/fargate.js @@ -61,6 +61,7 @@ class FargateService extends cdk.NestedStack { const image = imageVersion === DEFAULT_VERSION ? ecs.ContainerImage.fromRegistry(DEFAULT_IMAGE) : ecs.ContainerImage.fromEcrRepository(repository, imageVersion); + const desiredCount = taskConfiguration?.desiredCount || 1; this.service = new ecs_patterns.ApplicationLoadBalancedFargateService(this, stack.getResourceID('AdminService'), { assignPublicIp: true, cluster: cluster, @@ -69,7 +70,7 @@ class FargateService extends cdk.NestedStack { memoryLimitMiB: taskConfiguration?.memoryLimitMiB || 512, healthCheckGracePeriod: taskConfiguration?.healthCheckGracePeriod || cdk.Duration.seconds(60), cpu: taskConfiguration?.cpu || 256, - desiredCount: taskConfiguration?.desiredCount || 1, + desiredCount, taskImageOptions: { image, environment: taskConfiguration?.environment || {}, @@ -80,6 +81,17 @@ class FargateService extends cdk.NestedStack { onePerAz: true, }, }); + // Setup AutoScaling policy + if (taskConfiguration.autoScalingCpuTarget) { + // Default max capacity to double desired unless specified + const maxCapacity = taskConfiguration?.maxCount || desiredCount * 2; + const scaling = this.service.service.autoScaleTaskCount({ maxCapacity }); + scaling.scaleOnCpuUtilization(stack.getResourceID('CpuScaling'), { + targetUtilizationPercent: taskConfiguration.autoScalingCpuTarget, + scaleInCooldown: cdk.Duration.seconds(60), + scaleOutCooldown: cdk.Duration.seconds(60), + }); + } // Hack to fix subnets issue // https://github.com/aws/aws-cdk/issues/5892#issuecomment-701993883 const cfnLoadBalancer = this.service.loadBalancer.node diff --git a/package-lock.json b/package-lock.json index 981c745..944e276 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@pixelfusion/aws-scripts", - "version": "1.4.2", + "version": "1.4.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@pixelfusion/aws-scripts", - "version": "1.4.2", + "version": "1.4.3", "license": "ISC", "dependencies": { "@aws-sdk/client-secrets-manager": "^3.348.0", diff --git a/src/fargate.ts b/src/fargate.ts index 1baeeb9..e92f3cc 100644 --- a/src/fargate.ts +++ b/src/fargate.ts @@ -26,6 +26,8 @@ export interface TaskConfiguration { healthCheckGracePeriod?: cdk.Duration cpu?: number desiredCount?: number + autoScalingCpuTarget?: number + maxCount?: number environment?: Record secrets?: Record } @@ -100,6 +102,7 @@ export class FargateService extends cdk.NestedStack { ? ecs.ContainerImage.fromRegistry(DEFAULT_IMAGE) : ecs.ContainerImage.fromEcrRepository(repository, imageVersion) + const desiredCount = taskConfiguration?.desiredCount || 1 this.service = new ecs_patterns.ApplicationLoadBalancedFargateService( this, stack.getResourceID('AdminService'), @@ -112,7 +115,7 @@ export class FargateService extends cdk.NestedStack { healthCheckGracePeriod: taskConfiguration?.healthCheckGracePeriod || cdk.Duration.seconds(60), cpu: taskConfiguration?.cpu || 256, - desiredCount: taskConfiguration?.desiredCount || 1, + desiredCount, taskImageOptions: { image, environment: taskConfiguration?.environment || {}, @@ -125,6 +128,18 @@ export class FargateService extends cdk.NestedStack { }, ) + // Setup AutoScaling policy + if (taskConfiguration.autoScalingCpuTarget) { + // Default max capacity to double desired unless specified + const maxCapacity = taskConfiguration?.maxCount || desiredCount * 2 + const scaling = this.service.service.autoScaleTaskCount({ maxCapacity }) + scaling.scaleOnCpuUtilization(stack.getResourceID('CpuScaling'), { + targetUtilizationPercent: taskConfiguration.autoScalingCpuTarget, + scaleInCooldown: cdk.Duration.seconds(60), + scaleOutCooldown: cdk.Duration.seconds(60), + }) + } + // Hack to fix subnets issue // https://github.com/aws/aws-cdk/issues/5892#issuecomment-701993883 const cfnLoadBalancer = this.service.loadBalancer.node