diff --git a/.changeset/lazy-schools-shop.md b/.changeset/lazy-schools-shop.md new file mode 100644 index 00000000..e1996fd1 --- /dev/null +++ b/.changeset/lazy-schools-shop.md @@ -0,0 +1,5 @@ +--- +"@codedazur/cdk-docker-cluster": minor +--- + +The DockerCluster now supports auto-scaling. diff --git a/package-lock.json b/package-lock.json index 0dc3bc73..06db4220 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23934,7 +23934,7 @@ }, "packages/cdk-docker-cluster": { "name": "@codedazur/cdk-docker-cluster", - "version": "0.0.0", + "version": "0.1.0", "license": "MIT", "dependencies": { "@codedazur/cdk-cache-invalidator": "*" @@ -24039,7 +24039,7 @@ }, "packages/cdk-next-app": { "name": "@codedazur/cdk-next-app", - "version": "0.1.0", + "version": "0.2.0", "license": "MIT", "dependencies": { "@codedazur/cdk-docker-cluster": "*" diff --git a/packages/cdk-docker-cluster/src/constructs/DockerCluster.ts b/packages/cdk-docker-cluster/src/constructs/DockerCluster.ts index 8b59143c..9a6b9477 100644 --- a/packages/cdk-docker-cluster/src/constructs/DockerCluster.ts +++ b/packages/cdk-docker-cluster/src/constructs/DockerCluster.ts @@ -13,7 +13,12 @@ export interface DockerClusterProps { path: string; secrets?: Record; port?: number; - tasks?: number; + tasks?: + | number + | { + minimum: number; + maximum: number; + }; cpu?: ApplicationLoadBalancedFargateServiceProps["cpu"]; memory?: ApplicationLoadBalancedFargateServiceProps["memoryLimitMiB"]; } @@ -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), @@ -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 }), },