Skip to content

Commit cafcc11

Browse files
clareliguoririx0rrr
authored andcommitted
feat(aws-ecs): Add desired count to LoadBalanced[Fargate|EC2]Service (#1111)
1 parent e404316 commit cafcc11

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

packages/@aws-cdk/aws-ecs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ obtained from either DockerHub or from ECR repositories:
172172
DockerHub.
173173
* `ecs.ContaienrImage.fromEcrRepository(repo, tag)`: use the given ECR repository as the image
174174
to start.
175-
* `ecs.ContainerImage.fromAsset({ directory: './image' })`: build and upload an
175+
* `ecs.ContainerImage.fromAsset(this, 'Image', { directory: './image' })`: build and upload an
176176
image directly from a `Dockerfile` in your source directory.
177177

178178
### Service

packages/@aws-cdk/aws-ecs/lib/load-balanced-ecs-service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ export interface LoadBalancedEc2ServiceProps {
5454
* @default true
5555
*/
5656
publicLoadBalancer?: boolean;
57+
58+
/**
59+
* Number of desired copies of running tasks
60+
*
61+
* @default 1
62+
*/
63+
desiredCount?: number;
5764
}
5865

5966
/**
@@ -81,6 +88,7 @@ export class LoadBalancedEc2Service extends cdk.Construct {
8188

8289
const service = new Ec2Service(this, "Service", {
8390
cluster: props.cluster,
91+
desiredCount: props.desiredCount || 1,
8492
taskDefinition,
8593
});
8694

packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service-applet.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ export interface LoadBalancedFargateServiceAppletProps extends cdk.StackProps {
7070
* @default false
7171
*/
7272
publicTasks?: boolean;
73+
74+
/**
75+
* Number of desired copies of running tasks
76+
*
77+
* @default 1
78+
*/
79+
desiredCount?: number;
7380
}
7481

7582
/**
@@ -91,6 +98,7 @@ export class LoadBalancedFargateServiceApplet extends cdk.Stack {
9198
publicLoadBalancer: props.publicLoadBalancer,
9299
publicTasks: props.publicTasks,
93100
image: DockerHub.image(props.image),
101+
desiredCount: props.desiredCount,
94102
});
95103
}
96104
}

packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ export interface LoadBalancedFargateServiceProps {
7676
* @default false
7777
*/
7878
publicTasks?: boolean;
79+
80+
/**
81+
* Number of desired copies of running tasks
82+
*
83+
* @default 1
84+
*/
85+
desiredCount?: number;
7986
}
8087

8188
/**
@@ -103,6 +110,7 @@ export class LoadBalancedFargateService extends cdk.Construct {
103110
const assignPublicIp = props.publicTasks !== undefined ? props.publicTasks : false;
104111
const service = new FargateService(this, "Service", {
105112
cluster: props.cluster,
113+
desiredCount: props.desiredCount || 1,
106114
taskDefinition,
107115
assignPublicIp
108116
});

packages/@aws-cdk/aws-ecs/test/test.l3s.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ export = {
1616
new ecs.LoadBalancedEc2Service(stack, 'Service', {
1717
cluster,
1818
memoryLimitMiB: 1024,
19-
image: ecs.DockerHub.image('test')
19+
image: ecs.DockerHub.image('test'),
20+
desiredCount: 2
2021
});
2122

22-
// THEN - stack containers a load balancer
23+
// THEN - stack containers a load balancer and a service
2324
expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::LoadBalancer'));
2425

26+
expect(stack).to(haveResource("AWS::ECS::Service", {
27+
DesiredCount: 2,
28+
LaunchType: "EC2",
29+
}));
30+
2531
test.done();
2632
},
2733

@@ -34,12 +40,37 @@ export = {
3440
// WHEN
3541
new ecs.LoadBalancedFargateService(stack, 'Service', {
3642
cluster,
37-
image: ecs.DockerHub.image('test')
43+
image: ecs.DockerHub.image('test'),
44+
desiredCount: 2
3845
});
3946

40-
// THEN - stack containers a load balancer
47+
// THEN - stack contains a load balancer and a service
4148
expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::LoadBalancer'));
4249

50+
expect(stack).to(haveResource("AWS::ECS::Service", {
51+
DesiredCount: 2,
52+
LaunchType: "FARGATE",
53+
}));
54+
55+
test.done();
56+
},
57+
58+
'test Fargateloadbalanced applet'(test: Test) {
59+
// WHEN
60+
const app = new cdk.App();
61+
const stack = new ecs.LoadBalancedFargateServiceApplet(app, 'Service', {
62+
image: 'test',
63+
desiredCount: 2
64+
});
65+
66+
// THEN - stack contains a load balancer and a service
67+
expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::LoadBalancer'));
68+
69+
expect(stack).to(haveResource("AWS::ECS::Service", {
70+
DesiredCount: 2,
71+
LaunchType: "FARGATE",
72+
}));
73+
4374
test.done();
4475
}
4576
};

0 commit comments

Comments
 (0)