PlacementStrategy Class
The placement strategies to use for tasks in the service. For more information, see Amazon ECS Task Placement Strategies.
Tasks will preferentially be placed on instances that match these rules.
Example
ts
const vpc = ec2.Vpc.fromLookup(this, 'Vpc', {
isDefault: true,
});
const cluster = new ecs.Cluster(this, 'Ec2Cluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', {
instanceType: new ec2.InstanceType('t2.micro'),
vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC },
});
const taskDefinition = new ecs.TaskDefinition(this, 'TD', {
compatibility: ecs.Compatibility.EC2,
});
taskDefinition.addContainer('TheContainer', {
image: ecs.ContainerImage.fromRegistry('foo/bar'),
memoryLimitMiB: 256,
});
const runTask = new tasks.EcsRunTask(this, 'Run', {
integrationPattern: sfn.IntegrationPattern.RUN_JOB,
cluster,
taskDefinition,
launchTarget: new tasks.EcsEc2LaunchTarget({
placementStrategies: [
ecs.PlacementStrategy.spreadAcrossInstances(),
ecs.PlacementStrategy.packedByCpu(),
ecs.PlacementStrategy.randomly(),
],
placementConstraints: [
ecs.PlacementConstraint.memberOf('blieptuut'),
],
}),
propagatedTagSource: ecs.PropagatedTagSource.TASK_DEFINITION,
});
Methods
packedBy(resource: BinPackResource): PlacementStrategy // chainable@staticmethod def packed_by(resource: BinPackResource) -> PlacementStrategypublic static PlacementStrategy packedBy(BinPackResource resource)public static PlacementStrategy PackedBy(BinPackResource resource)func PackedBy(Resource BinPackResource) PlacementStrategystatic#Places tasks on the container instances with the least available capacity of the specified resource.
packedByCpu(): PlacementStrategy // chainable@staticmethod def packed_by_cpu() -> PlacementStrategypublic static PlacementStrategy packedByCpu()public static PlacementStrategy PackedByCpu()func PackedByCpu() PlacementStrategystatic#Places tasks on container instances with the least available amount of CPU capacity.
This minimizes the number of instances in use.
packedByMemory(): PlacementStrategy // chainable@staticmethod def packed_by_memory() -> PlacementStrategypublic static PlacementStrategy packedByMemory()public static PlacementStrategy PackedByMemory()func PackedByMemory() PlacementStrategystatic#Places tasks on container instances with the least available amount of memory capacity.
This minimizes the number of instances in use.
randomly(): PlacementStrategy // chainable@staticmethod def randomly() -> PlacementStrategypublic static PlacementStrategy randomly()public static PlacementStrategy Randomly()func Randomly() PlacementStrategystatic#Places tasks randomly.
spreadAcross(...fields: string[]): PlacementStrategy // chainable@staticmethod def spread_across(fields: str) -> PlacementStrategypublic static PlacementStrategy spreadAcross(String fields)public static PlacementStrategy SpreadAcross(string fields)func SpreadAcross(Fields *string) PlacementStrategystatic#Places tasks evenly based on the specified value.
You can use one of the built-in attributes found on BuiltInAttributes or supply your own custom instance attributes. If more than one attribute is supplied, spreading is done in order.
spreadAcrossInstances(): PlacementStrategy // chainable@staticmethod def spread_across_instances() -> PlacementStrategypublic static PlacementStrategy spreadAcrossInstances()public static PlacementStrategy SpreadAcrossInstances()func SpreadAcrossInstances() PlacementStrategystatic#Places tasks evenly across all container instances in the cluster.
toJson(): CfnService.PlacementStrategyProperty[]def to_json(self, ) -> typing.List[PlacementStrategyProperty]public List<PlacementStrategyProperty> toJson()public PlacementStrategyProperty[] ToJson()func ToJson() *[]PlacementStrategyProperty#Return the placement JSON.