From cafcc1196b6aad6d93fee0b360d2c05ca68b070b Mon Sep 17 00:00:00 2001 From: Clare Liguori Date: Thu, 8 Nov 2018 02:47:25 -0800 Subject: [PATCH] feat(aws-ecs): Add desired count to LoadBalanced[Fargate|EC2]Service (#1111) --- packages/@aws-cdk/aws-ecs/README.md | 2 +- .../aws-ecs/lib/load-balanced-ecs-service.ts | 8 ++++ .../load-balanced-fargate-service-applet.ts | 8 ++++ .../lib/load-balanced-fargate-service.ts | 8 ++++ packages/@aws-cdk/aws-ecs/test/test.l3s.ts | 39 +++++++++++++++++-- 5 files changed, 60 insertions(+), 5 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs/README.md b/packages/@aws-cdk/aws-ecs/README.md index dd6ab6f81859a..dd405828938c9 100644 --- a/packages/@aws-cdk/aws-ecs/README.md +++ b/packages/@aws-cdk/aws-ecs/README.md @@ -172,7 +172,7 @@ obtained from either DockerHub or from ECR repositories: DockerHub. * `ecs.ContaienrImage.fromEcrRepository(repo, tag)`: use the given ECR repository as the image to start. -* `ecs.ContainerImage.fromAsset({ directory: './image' })`: build and upload an +* `ecs.ContainerImage.fromAsset(this, 'Image', { directory: './image' })`: build and upload an image directly from a `Dockerfile` in your source directory. ### Service diff --git a/packages/@aws-cdk/aws-ecs/lib/load-balanced-ecs-service.ts b/packages/@aws-cdk/aws-ecs/lib/load-balanced-ecs-service.ts index 6979ecacb8bfd..e3657437dff73 100644 --- a/packages/@aws-cdk/aws-ecs/lib/load-balanced-ecs-service.ts +++ b/packages/@aws-cdk/aws-ecs/lib/load-balanced-ecs-service.ts @@ -54,6 +54,13 @@ export interface LoadBalancedEc2ServiceProps { * @default true */ publicLoadBalancer?: boolean; + + /** + * Number of desired copies of running tasks + * + * @default 1 + */ + desiredCount?: number; } /** @@ -81,6 +88,7 @@ export class LoadBalancedEc2Service extends cdk.Construct { const service = new Ec2Service(this, "Service", { cluster: props.cluster, + desiredCount: props.desiredCount || 1, taskDefinition, }); diff --git a/packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service-applet.ts b/packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service-applet.ts index 1e3d00bb983de..1049a19bf7645 100644 --- a/packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service-applet.ts +++ b/packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service-applet.ts @@ -70,6 +70,13 @@ export interface LoadBalancedFargateServiceAppletProps extends cdk.StackProps { * @default false */ publicTasks?: boolean; + + /** + * Number of desired copies of running tasks + * + * @default 1 + */ + desiredCount?: number; } /** @@ -91,6 +98,7 @@ export class LoadBalancedFargateServiceApplet extends cdk.Stack { publicLoadBalancer: props.publicLoadBalancer, publicTasks: props.publicTasks, image: DockerHub.image(props.image), + desiredCount: props.desiredCount, }); } } \ No newline at end of file diff --git a/packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service.ts index 2287296468ea4..5ee7bba7769b3 100644 --- a/packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service.ts @@ -76,6 +76,13 @@ export interface LoadBalancedFargateServiceProps { * @default false */ publicTasks?: boolean; + + /** + * Number of desired copies of running tasks + * + * @default 1 + */ + desiredCount?: number; } /** @@ -103,6 +110,7 @@ export class LoadBalancedFargateService extends cdk.Construct { const assignPublicIp = props.publicTasks !== undefined ? props.publicTasks : false; const service = new FargateService(this, "Service", { cluster: props.cluster, + desiredCount: props.desiredCount || 1, taskDefinition, assignPublicIp }); diff --git a/packages/@aws-cdk/aws-ecs/test/test.l3s.ts b/packages/@aws-cdk/aws-ecs/test/test.l3s.ts index 900116a34fdb9..9ef60be7cba3d 100644 --- a/packages/@aws-cdk/aws-ecs/test/test.l3s.ts +++ b/packages/@aws-cdk/aws-ecs/test/test.l3s.ts @@ -16,12 +16,18 @@ export = { new ecs.LoadBalancedEc2Service(stack, 'Service', { cluster, memoryLimitMiB: 1024, - image: ecs.DockerHub.image('test') + image: ecs.DockerHub.image('test'), + desiredCount: 2 }); - // THEN - stack containers a load balancer + // THEN - stack containers a load balancer and a service expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::LoadBalancer')); + expect(stack).to(haveResource("AWS::ECS::Service", { + DesiredCount: 2, + LaunchType: "EC2", + })); + test.done(); }, @@ -34,12 +40,37 @@ export = { // WHEN new ecs.LoadBalancedFargateService(stack, 'Service', { cluster, - image: ecs.DockerHub.image('test') + image: ecs.DockerHub.image('test'), + desiredCount: 2 }); - // THEN - stack containers a load balancer + // THEN - stack contains a load balancer and a service expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::LoadBalancer')); + expect(stack).to(haveResource("AWS::ECS::Service", { + DesiredCount: 2, + LaunchType: "FARGATE", + })); + + test.done(); + }, + + 'test Fargateloadbalanced applet'(test: Test) { + // WHEN + const app = new cdk.App(); + const stack = new ecs.LoadBalancedFargateServiceApplet(app, 'Service', { + image: 'test', + desiredCount: 2 + }); + + // THEN - stack contains a load balancer and a service + expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::LoadBalancer')); + + expect(stack).to(haveResource("AWS::ECS::Service", { + DesiredCount: 2, + LaunchType: "FARGATE", + })); + test.done(); } };