Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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

Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,28 @@
"DependsOn": [
"TaskDefTaskRole1EDB4A67"
]
},
"TestEmptyService1DB8D95A": {
"Type": "AWS::ECS::Service",
"Properties": {
"Cluster": {
"Ref": "EcsCluster97242B84"
},
"DeploymentConfiguration": {
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"EnableECSManagedTags": false,
"LaunchType": "EC2",
"PlacementStrategies": [],
"SchedulingStrategy": "REPLICA",
"TaskDefinition": {
"Ref": "TaskDef54694570"
}
},
"DependsOn": [
"TaskDefTaskRole1EDB4A67"
]
}
},
"Parameters": {
Expand Down

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

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

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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class EcsStack extends cdk.Stack {
ecs.PlacementStrategy.packedByMemory(),
],
});

new ecs.Ec2Service(this, 'Test_Empty', {
cluster,
taskDefinition,
placementStrategies: [],
});
}
}

Expand Down
13 changes: 7 additions & 6 deletions packages/aws-cdk-lib/aws-ecs/lib/ec2/ec2-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class Ec2Service extends BaseService implements IEc2Service {
}

private constraints?: CfnService.PlacementConstraintProperty[];
private readonly strategies: CfnService.PlacementStrategyProperty[];
private strategies?: CfnService.PlacementStrategyProperty[];
private readonly daemon: boolean;
private readonly availabilityZoneRebalancingEnabled: boolean;

Expand Down Expand Up @@ -212,15 +212,13 @@ export class Ec2Service extends BaseService implements IEc2Service {
cluster: props.cluster.clusterName,
taskDefinition: props.deploymentController?.type === DeploymentControllerType.EXTERNAL ? undefined : props.taskDefinition.taskDefinitionArn,
placementConstraints: Lazy.any({ produce: () => this.constraints }),
placementStrategies: Lazy.any({ produce: () => this.strategies }, { omitEmptyArray: true }),
placementStrategies: Lazy.any({ produce: () => this.strategies }),
schedulingStrategy: props.daemon ? 'DAEMON' : 'REPLICA',
availabilityZoneRebalancing: props.availabilityZoneRebalancing,
}, props.taskDefinition);
// Enhanced CDK Analytics Telemetry
addConstructMetadata(this, props);

this.constraints = undefined;
this.strategies = [];
this.daemon = props.daemon || false;
this.availabilityZoneRebalancingEnabled = props.availabilityZoneRebalancing === AvailabilityZoneRebalancing.ENABLED;

Expand Down Expand Up @@ -249,7 +247,9 @@ export class Ec2Service extends BaseService implements IEc2Service {
if (props.placementConstraints) {
this.addPlacementConstraints(...props.placementConstraints);
}
this.addPlacementStrategies(...props.placementStrategies || []);
if (props.placementStrategies) {
this.addPlacementStrategies(...props.placementStrategies);
}

this.node.addValidation({
validate: () => !this.taskDefinition.defaultContainer ? ['A TaskDefinition must have at least one essential container'] : [],
Expand All @@ -272,13 +272,14 @@ export class Ec2Service extends BaseService implements IEc2Service {
throw new ValidationError("Can't configure placement strategies when daemon=true", this);
}

if (strategies.length > 0 && this.strategies.length === 0 && this.availabilityZoneRebalancingEnabled) {
if (strategies.length > 0 && !this.strategies && this.availabilityZoneRebalancingEnabled) {
const [placement] = strategies[0].toJson();
if (placement.type !== 'spread' || placement.field !== BuiltInAttributes.AVAILABILITY_ZONE) {
throw new ValidationError(`AvailabilityZoneBalancing.ENABLED requires that the first placement strategy, if any, be 'spread across "${BuiltInAttributes.AVAILABILITY_ZONE}"'`, this);
}
}

this.strategies = [];
for (const strategy of strategies) {
this.strategies.push(...strategy.toJson());
}
Expand Down
25 changes: 25 additions & 0 deletions packages/aws-cdk-lib/aws-ecs/test/ec2/ec2-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2307,6 +2307,31 @@ describe('ec2 service', () => {
});
});

test('with empty [] placement strategies', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
addDefaultCapacityProvider(cluster, stack, vpc);
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');

taskDefinition.addContainer('web', {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
memoryLimitMiB: 512,
});

new ecs.Ec2Service(stack, 'Ec2Service', {
cluster,
taskDefinition,
placementStrategies: [],
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
PlacementStrategies: Match.arrayEquals([]),
});
});

testDeprecated('with both propagateTags and propagateTaskTagsFrom defined', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down
Loading