Skip to content

Commit 96bb8ce

Browse files
authored
fix(ecs): Allow scheduling DAEMON services even if no EC2 capacity attached to cluster (#25306) (#25328)
It can be useful to allow DAEMON services to be scheduled to a cluster even if there is no EC2 capacity attached to it yet, under the assumption that capacity will be attached to the cluster later. We no longer fail validation if this happens. Fixes #25306 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 432af34 commit 96bb8ce

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Diff for: packages/aws-cdk-lib/aws-ecs/lib/ec2/ec2-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ export class Ec2Service extends BaseService implements IEc2Service {
249249
*/
250250
private validateEc2Service(): string[] {
251251
const ret = new Array<string>();
252-
if (!this.cluster.hasEc2Capacity) {
252+
if (!this.daemon && !this.cluster.hasEc2Capacity) {
253253
ret.push('Cluster for this service needs Ec2 capacity. Call addXxxCapacity() on the cluster.');
254254
}
255255
return ret;

Diff for: packages/aws-cdk-lib/aws-ecs/test/ec2/ec2-service.test.ts

+43
Original file line numberDiff line numberDiff line change
@@ -2093,8 +2093,51 @@ describe('ec2 service', () => {
20932093
expect(() => {
20942094
service.addPlacementStrategies(PlacementStrategy.packedBy(ecs.BinPackResource.MEMORY));
20952095
}).toThrow();
2096+
});
2097+
2098+
test('throws an exception if non-DAEMON service is added but no EC2 capacity is associated with the cluster', () => {
2099+
// GIVEN
2100+
const stack = new cdk.Stack();
2101+
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
2102+
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
2103+
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');
2104+
2105+
taskDefinition.addContainer('web', {
2106+
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
2107+
memoryLimitMiB: 512,
2108+
});
2109+
2110+
new ecs.Ec2Service(stack, 'Ec2Service', {
2111+
cluster,
2112+
taskDefinition,
2113+
});
20962114

2115+
expect(() => {
2116+
Template.fromStack(stack);
2117+
}).toThrow(/Cluster for this service needs Ec2 capacity/);
2118+
});
20972119

2120+
test('does not throw an exception if DAEMON service is added but no EC2 capacity is associated with the cluster', () => {
2121+
// GIVEN
2122+
const stack = new cdk.Stack();
2123+
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
2124+
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
2125+
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');
2126+
2127+
taskDefinition.addContainer('web', {
2128+
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
2129+
memoryLimitMiB: 512,
2130+
});
2131+
2132+
new ecs.Ec2Service(stack, 'Ec2Service', {
2133+
cluster,
2134+
taskDefinition,
2135+
daemon: true,
2136+
});
2137+
2138+
expect(() => {
2139+
Template.fromStack(stack);
2140+
}).not.toThrow();
20982141
});
20992142
});
21002143

0 commit comments

Comments
 (0)