aws-cdk-lib.aws_ecs.PlacementStrategy

class PlacementStrategy

LanguageType name
.NETAmazon.CDK.AWS.ECS.PlacementStrategy
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awsecs#PlacementStrategy
Javasoftware.amazon.awscdk.services.ecs.PlacementStrategy
Pythonaws_cdk.aws_ecs.PlacementStrategy
TypeScript (source)aws-cdk-lib » aws_ecs » PlacementStrategy

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

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

NameDescription
toJson()Return the placement JSON.
static packedBy(resource)Places tasks on the container instances with the least available capacity of the specified resource.
static packedByCpu()Places tasks on container instances with the least available amount of CPU capacity.
static packedByMemory()Places tasks on container instances with the least available amount of memory capacity.
static randomly()Places tasks randomly.
static spreadAcross(...fields)Places tasks evenly based on the specified value.
static spreadAcrossInstances()Places tasks evenly across all container instances in the cluster.

toJson()

public toJson(): PlacementStrategyProperty[]

Returns

  • PlacementStrategyProperty[]

Return the placement JSON.


static packedBy(resource)

public static packedBy(resource: BinPackResource): PlacementStrategy

Parameters

  • resource BinPackResource

Returns

  • PlacementStrategy

Places tasks on the container instances with the least available capacity of the specified resource.


static packedByCpu()

public static packedByCpu(): PlacementStrategy

Returns

  • PlacementStrategy

Places tasks on container instances with the least available amount of CPU capacity.

This minimizes the number of instances in use.


static packedByMemory()

public static packedByMemory(): PlacementStrategy

Returns

  • PlacementStrategy

Places tasks on container instances with the least available amount of memory capacity.

This minimizes the number of instances in use.


static randomly()

public static randomly(): PlacementStrategy

Returns

  • PlacementStrategy

Places tasks randomly.


static spreadAcross(...fields)

public static spreadAcross(...fields: string[]): PlacementStrategy

Parameters

  • fields string

Returns

  • PlacementStrategy

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.


static spreadAcrossInstances()

public static spreadAcrossInstances(): PlacementStrategy

Returns

  • PlacementStrategy

Places tasks evenly across all container instances in the cluster.