aws-cdk-lib.aws_ecs.PlacementConstraint

class PlacementConstraint

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

The placement constraints to use for tasks in the service. For more information, see Amazon ECS Task Placement Constraints.

Tasks will only 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 distinctInstances()Use distinctInstance to ensure that each task in a particular group is running on a different container instance.
static memberOf(...expressions)Use memberOf to restrict the selection to a group of valid candidates specified by a query expression.

toJson()

public toJson(): PlacementConstraintProperty[]

Returns

  • PlacementConstraintProperty[]

Return the placement JSON.


static distinctInstances()

public static distinctInstances(): PlacementConstraint

Returns

  • PlacementConstraint

Use distinctInstance to ensure that each task in a particular group is running on a different container instance.


static memberOf(...expressions)

public static memberOf(...expressions: string[]): PlacementConstraint

Parameters

  • expressions string

Returns

  • PlacementConstraint

Use memberOf to restrict the selection to a group of valid candidates specified by a query expression.

Multiple expressions can be specified. For more information, see Cluster Query Language.

You can specify multiple expressions in one call. The tasks will only be placed on instances matching all expressions.

See also: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cluster-query-language.html