Skip to content

Commit

Permalink
feat(cdk-docker-cluster): support auto-scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsdaniels committed May 2, 2024
1 parent c6f0658 commit 603c25a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-schools-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codedazur/cdk-docker-cluster": minor
---

The DockerCluster now supports auto-scaling.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 19 additions & 4 deletions packages/cdk-docker-cluster/src/constructs/DockerCluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ export interface DockerClusterProps {
path: string;
secrets?: Record<string, string>;
port?: number;
tasks?: number;
tasks?:
| number
| {
minimum: number;
maximum: number;
};
cpu?: ApplicationLoadBalancedFargateServiceProps["cpu"];
memory?: ApplicationLoadBalancedFargateServiceProps["memoryLimitMiB"];
}
Expand All @@ -32,11 +37,14 @@ export class DockerCluster extends Construct {
platform: Platform.LINUX_AMD64,
});

const service = new ApplicationLoadBalancedFargateService(this, "Service", {
const desiredTasks =
typeof props.tasks === "number" ? props.tasks : props.tasks?.minimum;

const fargate = new ApplicationLoadBalancedFargateService(this, "Service", {
cluster: new Cluster(this, "Cluster"),
cpu: props.cpu,
memoryLimitMiB: props.memory,
desiredCount: props.tasks,
desiredCount: desiredTasks,
taskImageOptions: {
// image: ContainerImage.fromEcrRepository(image.repository, image.imageTag),
image: ContainerImage.fromDockerImageAsset(image),
Expand All @@ -50,9 +58,16 @@ export class DockerCluster extends Construct {
// @todo publicLoadBalancer: false,
});

if (typeof props.tasks === "object") {
fargate.service.autoScaleTaskCount({
minCapacity: props.tasks.minimum,
maxCapacity: props.tasks.maximum,
});
}

const distribution = new Distribution(this, "Distribution", {
defaultBehavior: {
origin: new LoadBalancerV2Origin(service.loadBalancer, {
origin: new LoadBalancerV2Origin(fargate.loadBalancer, {
protocolPolicy: OriginProtocolPolicy.HTTP_ONLY, // @todo OriginProtocolPolicy.HTTPS_ONLY
}),
},
Expand Down

0 comments on commit 603c25a

Please sign in to comment.