Skip to content

Commit

Permalink
feat(cdk-docker-cluster): autoscaling thresholds and cooldowns
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsdaniels committed Sep 19, 2024
1 parent f33889f commit 5a3a979
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-fishes-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codedazur/cdk-docker-cluster": minor
---

Autoscaling thresholds and cooldowns can now be configured.
60 changes: 52 additions & 8 deletions packages/cdk-docker-cluster/src/constructs/DockerCluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
SiteDistribution,
SiteDistributionProps,
} from "@codedazur/cdk-site-distribution";
import { App } from "aws-cdk-lib";
import { App, Duration } from "aws-cdk-lib";
import {
AllowedMethods,
OriginProtocolPolicy,
Expand All @@ -25,12 +25,7 @@ export interface DockerClusterProps {
source: string | SourceProps | ContainerImage;
service?: {
port?: number;
tasks?:
| number
| {
minimum: number;
maximum: number;
};
tasks?: number | AutoScalingConfig;
cpu?: ApplicationLoadBalancedFargateServiceProps["cpu"];
memory?: ApplicationLoadBalancedFargateServiceProps["memoryLimitMiB"];
environment?: Record<string, string>;
Expand All @@ -39,6 +34,41 @@ export interface DockerClusterProps {
distribution?: Omit<SiteDistributionProps, "origin">;
}

interface AutoScalingConfig {
/**
* The minimum number of tasks to scale out to.
*/
minimum: number;
/**
* The maximum number of tasks to scale out to.
*/
maximum: number;
threshold?: {
/**
* The memory utilization percentage above which the service will scale out.
* @default 75
*/
memory?: number;
/**
* The CPU utilization percentage above which the service will scale out.
* @default 75
*/
cpu?: number;
};
cooldown?: {
/**
* The cooldown period for scaling in.
* @default Duration.seconds(300)
*/
in?: Duration;
/**
* The cooldown period for scaling out.
* @default Duration.seconds(300)
*/
out?: Duration;
};
}

interface SourceProps {
directory: string;
exclude?: string[];
Expand Down Expand Up @@ -119,10 +149,24 @@ export class DockerCluster extends Construct {
});

if (typeof this.props.service?.tasks === "object") {
service.service.autoScaleTaskCount({
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;
Expand Down

0 comments on commit 5a3a979

Please sign in to comment.