Skip to content

Commit

Permalink
feat: expose autoscaling config
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsdaniels committed Sep 19, 2024
1 parent 5a3a979 commit 78a1ab2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-planets-impress.md
Original file line number Diff line number Diff line change
@@ -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.
48 changes: 28 additions & 20 deletions packages/cdk-docker-cluster/src/constructs/DockerCluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
AssetImageProps,
Cluster,
ContainerImage,
ScalableTaskCount,
Secret,
} from "aws-cdk-lib/aws-ecs";
import {
Expand Down Expand Up @@ -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(
Expand All @@ -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();
}

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 78a1ab2

Please sign in to comment.