From 78a1ab2f853f66480ff7b240d083aa22883471ab Mon Sep 17 00:00:00 2001 From: Thijs Daniels Date: Thu, 19 Sep 2024 17:05:45 +0200 Subject: [PATCH] feat: expose autoscaling config --- .changeset/weak-planets-impress.md | 5 ++ .../src/constructs/DockerCluster.ts | 48 +++++++++++-------- 2 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 .changeset/weak-planets-impress.md diff --git a/.changeset/weak-planets-impress.md b/.changeset/weak-planets-impress.md new file mode 100644 index 00000000..caa9910b --- /dev/null +++ b/.changeset/weak-planets-impress.md @@ -0,0 +1,5 @@ +--- +"@codedazur/cdk-docker-cluster": patch +--- + +If a minimum and maximum task count is provided, the cluster will now autoscale at 75% cpu or memory utilization by default. diff --git a/packages/cdk-docker-cluster/src/constructs/DockerCluster.ts b/packages/cdk-docker-cluster/src/constructs/DockerCluster.ts index 85960c55..4e816b0c 100644 --- a/packages/cdk-docker-cluster/src/constructs/DockerCluster.ts +++ b/packages/cdk-docker-cluster/src/constructs/DockerCluster.ts @@ -13,6 +13,7 @@ import { AssetImageProps, Cluster, ContainerImage, + ScalableTaskCount, Secret, } from "aws-cdk-lib/aws-ecs"; import { @@ -87,6 +88,7 @@ export class DockerCluster extends Construct { public readonly domain?: string; public readonly image: ContainerImage; public readonly service: ApplicationLoadBalancedFargateService; + public readonly autoScaling?: ScalableTaskCount; public readonly siteDistribution: SiteDistribution; constructor( @@ -102,6 +104,7 @@ export class DockerCluster extends Construct { : this.createImage(this.props.source); this.service = this.createService(); + this.autoScaling = this.createAutoScaling(this.service); this.siteDistribution = this.createSiteDistribution(); } @@ -148,28 +151,33 @@ export class DockerCluster extends Construct { }, }); - if (typeof this.props.service?.tasks === "object") { - const autoScaling = service.service.autoScaleTaskCount({ - minCapacity: this.props.service?.tasks.minimum, - maxCapacity: this.props.service?.tasks.maximum, - }); - - autoScaling.scaleOnMemoryUtilization("MemoryScaling", { - targetUtilizationPercent: - this.props.service?.tasks.threshold?.memory ?? 75, - scaleInCooldown: this.props.service?.tasks.cooldown?.in, - scaleOutCooldown: this.props.service?.tasks.cooldown?.out, - }); - - autoScaling.scaleOnCpuUtilization("CpuScaling", { - targetUtilizationPercent: - this.props.service?.tasks.threshold?.cpu ?? 75, - scaleInCooldown: this.props.service?.tasks.cooldown?.in, - scaleOutCooldown: this.props.service?.tasks.cooldown?.out, - }); + return service; + } + + protected createAutoScaling(service: ApplicationLoadBalancedFargateService) { + if (typeof this.props.service?.tasks !== "object") { + return; } - return service; + const autoScaling = service.service.autoScaleTaskCount({ + minCapacity: this.props.service?.tasks.minimum, + maxCapacity: this.props.service?.tasks.maximum, + }); + + autoScaling.scaleOnMemoryUtilization("MemoryScaling", { + targetUtilizationPercent: + this.props.service?.tasks.threshold?.memory ?? 75, + scaleInCooldown: this.props.service?.tasks.cooldown?.in, + scaleOutCooldown: this.props.service?.tasks.cooldown?.out, + }); + + autoScaling.scaleOnCpuUtilization("CpuScaling", { + targetUtilizationPercent: this.props.service?.tasks.threshold?.cpu ?? 75, + scaleInCooldown: this.props.service?.tasks.cooldown?.in, + scaleOutCooldown: this.props.service?.tasks.cooldown?.out, + }); + + return autoScaling; } protected createSiteDistribution() {