Skip to content

Commit 0096e67

Browse files
feat(ecs-patterns): PlacementStrategy and PlacementConstraint for many patterns (#19612)
I've added PlacementStrategy and PlacementConstraint to - ApplicationLoadBalancedEc2Service - ApplicationMultipleTargetGroupsEc2Service - NetworkLoadBalancedEc2Service - NetworkMultipleTargetGroupsEc2Service - QueueProcessingEc2Service and pass it to AWS ECS related service. fixes #19225 ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [ ] Did you use `cdk-integ` to deploy the infrastructure and generate the snapshot (i.e. `cdk-integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent c62eeb7 commit 0096e67

7 files changed

+109
-5
lines changed

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Ec2Service, Ec2TaskDefinition } from '@aws-cdk/aws-ecs';
1+
import { Ec2Service, Ec2TaskDefinition, PlacementConstraint, PlacementStrategy } from '@aws-cdk/aws-ecs';
22
import * as cxapi from '@aws-cdk/cx-api';
33
import { Construct } from 'constructs';
44
import { ApplicationLoadBalancedServiceBase, ApplicationLoadBalancedServiceBaseProps } from '../base/application-load-balanced-service-base';
@@ -63,6 +63,22 @@ export interface ApplicationLoadBalancedEc2ServiceProps extends ApplicationLoadB
6363
* @default - No memory reserved.
6464
*/
6565
readonly memoryReservationMiB?: number;
66+
67+
/**
68+
* The placement constraints to use for tasks in the service. For more information, see
69+
* [Amazon ECS Task Placement Constraints](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-constraints.html).
70+
*
71+
* @default - No constraints.
72+
*/
73+
readonly placementConstraints?: PlacementConstraint[];
74+
75+
/**
76+
* The placement strategies to use for tasks in the service. For more information, see
77+
* [Amazon ECS Task Placement Strategies](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-strategies.html).
78+
*
79+
* @default - No strategies.
80+
*/
81+
readonly placementStrategies?: PlacementStrategy[];
6682
}
6783

6884
/**
@@ -135,6 +151,8 @@ export class ApplicationLoadBalancedEc2Service extends ApplicationLoadBalancedSe
135151
cloudMapOptions: props.cloudMapOptions,
136152
deploymentController: props.deploymentController,
137153
circuitBreaker: props.circuitBreaker,
154+
placementConstraints: props.placementConstraints,
155+
placementStrategies: props.placementStrategies,
138156
});
139157
this.addServiceAsTarget(this.service);
140158
}

packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-multiple-target-groups-ecs-service.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Ec2Service, Ec2TaskDefinition } from '@aws-cdk/aws-ecs';
1+
import { Ec2Service, Ec2TaskDefinition, PlacementConstraint, PlacementStrategy } from '@aws-cdk/aws-ecs';
22
import { ApplicationTargetGroup } from '@aws-cdk/aws-elasticloadbalancingv2';
33
import * as cxapi from '@aws-cdk/cx-api';
44
import { Construct } from 'constructs';
@@ -58,6 +58,22 @@ export interface ApplicationMultipleTargetGroupsEc2ServiceProps extends Applicat
5858
* @default - No memory reserved.
5959
*/
6060
readonly memoryReservationMiB?: number;
61+
62+
/**
63+
* The placement constraints to use for tasks in the service. For more information, see
64+
* [Amazon ECS Task Placement Constraints](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-constraints.html).
65+
*
66+
* @default - No constraints.
67+
*/
68+
readonly placementConstraints?: PlacementConstraint[];
69+
70+
/**
71+
* The placement strategies to use for tasks in the service. For more information, see
72+
* [Amazon ECS Task Placement Strategies](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-strategies.html).
73+
*
74+
* @default - No strategies.
75+
*/
76+
readonly placementStrategies?: PlacementStrategy[];
6177
}
6278

6379
/**
@@ -150,6 +166,8 @@ export class ApplicationMultipleTargetGroupsEc2Service extends ApplicationMultip
150166
propagateTags: props.propagateTags,
151167
enableECSManagedTags: props.enableECSManagedTags,
152168
cloudMapOptions: props.cloudMapOptions,
169+
placementConstraints: props.placementConstraints,
170+
placementStrategies: props.placementStrategies,
153171
});
154172
}
155173
}

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Ec2Service, Ec2TaskDefinition } from '@aws-cdk/aws-ecs';
1+
import { Ec2Service, Ec2TaskDefinition, PlacementConstraint, PlacementStrategy } from '@aws-cdk/aws-ecs';
22
import * as cxapi from '@aws-cdk/cx-api';
33
import { Construct } from 'constructs';
44
import { NetworkLoadBalancedServiceBase, NetworkLoadBalancedServiceBaseProps } from '../base/network-load-balanced-service-base';
@@ -61,6 +61,22 @@ export interface NetworkLoadBalancedEc2ServiceProps extends NetworkLoadBalancedS
6161
* @default - No memory reserved.
6262
*/
6363
readonly memoryReservationMiB?: number;
64+
65+
/**
66+
* The placement constraints to use for tasks in the service. For more information, see
67+
* [Amazon ECS Task Placement Constraints](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-constraints.html).
68+
*
69+
* @default - No constraints.
70+
*/
71+
readonly placementConstraints?: PlacementConstraint[];
72+
73+
/**
74+
* The placement strategies to use for tasks in the service. For more information, see
75+
* [Amazon ECS Task Placement Strategies](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-strategies.html).
76+
*
77+
* @default - No strategies.
78+
*/
79+
readonly placementStrategies?: PlacementStrategy[];
6480
}
6581

6682
/**
@@ -133,6 +149,8 @@ export class NetworkLoadBalancedEc2Service extends NetworkLoadBalancedServiceBas
133149
cloudMapOptions: props.cloudMapOptions,
134150
deploymentController: props.deploymentController,
135151
circuitBreaker: props.circuitBreaker,
152+
placementConstraints: props.placementConstraints,
153+
placementStrategies: props.placementStrategies,
136154
});
137155
this.addServiceAsTarget(this.service);
138156
}

packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-multiple-target-groups-ecs-service.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Ec2Service, Ec2TaskDefinition } from '@aws-cdk/aws-ecs';
1+
import { Ec2Service, Ec2TaskDefinition, PlacementConstraint, PlacementStrategy } from '@aws-cdk/aws-ecs';
22
import { NetworkTargetGroup } from '@aws-cdk/aws-elasticloadbalancingv2';
33
import * as cxapi from '@aws-cdk/cx-api';
44
import { Construct } from 'constructs';
@@ -57,6 +57,22 @@ export interface NetworkMultipleTargetGroupsEc2ServiceProps extends NetworkMulti
5757
* @default - No memory reserved.
5858
*/
5959
readonly memoryReservationMiB?: number;
60+
61+
/**
62+
* The placement constraints to use for tasks in the service. For more information, see
63+
* [Amazon ECS Task Placement Constraints](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-constraints.html).
64+
*
65+
* @default - No constraints.
66+
*/
67+
readonly placementConstraints?: PlacementConstraint[];
68+
69+
/**
70+
* The placement strategies to use for tasks in the service. For more information, see
71+
* [Amazon ECS Task Placement Strategies](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-strategies.html).
72+
*
73+
* @default - No strategies.
74+
*/
75+
readonly placementStrategies?: PlacementStrategy[];
6076
}
6177

6278
/**
@@ -150,6 +166,8 @@ export class NetworkMultipleTargetGroupsEc2Service extends NetworkMultipleTarget
150166
propagateTags: props.propagateTags,
151167
enableECSManagedTags: props.enableECSManagedTags,
152168
cloudMapOptions: props.cloudMapOptions,
169+
placementConstraints: props.placementConstraints,
170+
placementStrategies: props.placementStrategies,
153171
});
154172
}
155173
}

packages/@aws-cdk/aws-ecs-patterns/lib/ecs/queue-processing-ecs-service.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Ec2Service, Ec2TaskDefinition } from '@aws-cdk/aws-ecs';
1+
import { Ec2Service, Ec2TaskDefinition, PlacementConstraint, PlacementStrategy } from '@aws-cdk/aws-ecs';
22
import * as cxapi from '@aws-cdk/cx-api';
33
import { Construct } from 'constructs';
44
import { QueueProcessingServiceBase, QueueProcessingServiceBaseProps } from '../base/queue-processing-service-base';
@@ -67,6 +67,22 @@ export interface QueueProcessingEc2ServiceProps extends QueueProcessingServiceBa
6767
* @default - QueueProcessingContainer
6868
*/
6969
readonly containerName?: string;
70+
71+
/**
72+
* The placement constraints to use for tasks in the service. For more information, see
73+
* [Amazon ECS Task Placement Constraints](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-constraints.html).
74+
*
75+
* @default - No constraints.
76+
*/
77+
readonly placementConstraints?: PlacementConstraint[];
78+
79+
/**
80+
* The placement strategies to use for tasks in the service. For more information, see
81+
* [Amazon ECS Task Placement Strategies](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-strategies.html).
82+
*
83+
* @default - No strategies.
84+
*/
85+
readonly placementStrategies?: PlacementStrategy[];
7086
}
7187

7288
/**
@@ -124,6 +140,8 @@ export class QueueProcessingEc2Service extends QueueProcessingServiceBase {
124140
deploymentController: props.deploymentController,
125141
circuitBreaker: props.circuitBreaker,
126142
capacityProviderStrategies: props.capacityProviderStrategies,
143+
placementConstraints: props.placementConstraints,
144+
placementStrategies: props.placementStrategies,
127145
});
128146

129147
this.configureAutoscalingForService(this.service);

packages/@aws-cdk/aws-ecs-patterns/test/ec2/l3s-v2.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
Ec2TaskDefinition,
1212
PropagatedTagSource,
1313
Protocol,
14+
PlacementStrategy,
15+
PlacementConstraint,
1416
} from '@aws-cdk/aws-ecs';
1517
import { ApplicationProtocol, SslPolicy } from '@aws-cdk/aws-elasticloadbalancingv2';
1618
import { CompositePrincipal, Role, ServicePrincipal } from '@aws-cdk/aws-iam';
@@ -163,6 +165,8 @@ describe('When Application Load Balancer', () => {
163165
protocol: Protocol.TCP,
164166
},
165167
],
168+
placementStrategies: [PlacementStrategy.spreadAcrossInstances(), PlacementStrategy.packedByCpu(), PlacementStrategy.randomly()],
169+
placementConstraints: [PlacementConstraint.memberOf('attribute:ecs.instance-type =~ m5a.*')],
166170
});
167171

168172
// THEN
@@ -189,6 +193,8 @@ describe('When Application Load Balancer', () => {
189193
],
190194
PropagateTags: 'SERVICE',
191195
ServiceName: 'myService',
196+
PlacementConstraints: [{ Type: 'memberOf', Expression: 'attribute:ecs.instance-type =~ m5a.*' }],
197+
PlacementStrategies: [{ Field: 'instanceId', Type: 'spread' }, { Field: 'cpu', Type: 'binpack' }, { Type: 'random' }],
192198
});
193199

194200
Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {
@@ -1042,6 +1048,8 @@ describe('When Network Load Balancer', () => {
10421048
listener: 'listener2',
10431049
},
10441050
],
1051+
placementStrategies: [PlacementStrategy.spreadAcrossInstances(), PlacementStrategy.packedByCpu(), PlacementStrategy.randomly()],
1052+
placementConstraints: [PlacementConstraint.memberOf('attribute:ecs.instance-type =~ m5a.*')],
10451053
});
10461054

10471055
// THEN
@@ -1069,6 +1077,8 @@ describe('When Network Load Balancer', () => {
10691077
PropagateTags: 'SERVICE',
10701078
SchedulingStrategy: 'REPLICA',
10711079
ServiceName: 'myService',
1080+
PlacementConstraints: [{ Type: 'memberOf', Expression: 'attribute:ecs.instance-type =~ m5a.*' }],
1081+
PlacementStrategies: [{ Field: 'instanceId', Type: 'spread' }, { Field: 'cpu', Type: 'binpack' }, { Type: 'random' }],
10721082
});
10731083

10741084
Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {

packages/@aws-cdk/aws-ecs-patterns/test/ec2/queue-processing-ecs-service.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ testDeprecated('test ECS queue worker service construct - with optional props',
234234
family: 'ecs-task-family',
235235
circuitBreaker: { rollback: true },
236236
gpuCount: 256,
237+
placementStrategies: [ecs.PlacementStrategy.spreadAcrossInstances(), ecs.PlacementStrategy.packedByCpu(), ecs.PlacementStrategy.randomly()],
238+
placementConstraints: [ecs.PlacementConstraint.memberOf('attribute:ecs.instance-type =~ m5a.*')],
237239
});
238240

239241
// THEN - QueueWorker is of EC2 launch type, an SQS queue is created and all optional properties are set.
@@ -252,6 +254,8 @@ testDeprecated('test ECS queue worker service construct - with optional props',
252254
DeploymentController: {
253255
Type: 'ECS',
254256
},
257+
PlacementConstraints: [{ Type: 'memberOf', Expression: 'attribute:ecs.instance-type =~ m5a.*' }],
258+
PlacementStrategies: [{ Field: 'instanceId', Type: 'spread' }, { Field: 'cpu', Type: 'binpack' }, { Type: 'random' }],
255259
});
256260

257261
Template.fromStack(stack).hasResourceProperties('AWS::SQS::Queue', {

0 commit comments

Comments
 (0)