Class PlacementStrategy

All Implemented Interfaces:

@Generated(value="jsii-pacmak/1.84.0 (build 5404dcf)", date="2023-06-19T16:30:47.427Z") @Stability(Stable) public class PlacementStrategy extends
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.


 IVpc vpc = Vpc.fromLookup(this, "Vpc", VpcLookupOptions.builder()
 Cluster cluster = Cluster.Builder.create(this, "Ec2Cluster").vpc(vpc).build();
 cluster.addCapacity("DefaultAutoScalingGroup", AddCapacityOptions.builder()
         .instanceType(new InstanceType("t2.micro"))
 TaskDefinition taskDefinition = TaskDefinition.Builder.create(this, "TD")
 taskDefinition.addContainer("TheContainer", ContainerDefinitionOptions.builder()
 EcsRunTask runTask = EcsRunTask.Builder.create(this, "Run")
                 .placementStrategies(List.of(PlacementStrategy.spreadAcrossInstances(), PlacementStrategy.packedByCpu(), PlacementStrategy.randomly()))
  • Constructor Details

    • PlacementStrategy

      protected PlacementStrategy( objRef)
    • PlacementStrategy

      protected PlacementStrategy( initializationMode)
  • Method Details

    • packedBy

      @Stability(Stable) @NotNull public static PlacementStrategy packedBy(@NotNull BinPackResource resource)
      Places tasks on the container instances with the least available capacity of the specified resource.

      resource - This parameter is required.
    • packedByCpu

      @Stability(Stable) @NotNull public static PlacementStrategy packedByCpu()
      Places tasks on container instances with the least available amount of CPU capacity.

      This minimizes the number of instances in use.

    • packedByMemory

      @Stability(Stable) @NotNull public static PlacementStrategy packedByMemory()
      Places tasks on container instances with the least available amount of memory capacity.

      This minimizes the number of instances in use.

    • randomly

      @Stability(Stable) @NotNull public static PlacementStrategy randomly()
      Places tasks randomly.
    • spreadAcross

      @Stability(Stable) @NotNull public static PlacementStrategy spreadAcross(@NotNull @NotNull String... fields)
      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.

      Default: attributes instanceId

      fields - This parameter is required.
    • spreadAcrossInstances

      @Stability(Stable) @NotNull public static PlacementStrategy spreadAcrossInstances()
      Places tasks evenly across all container instances in the cluster.
    • toJson

      @Stability(Stable) @NotNull public List<CfnService.PlacementStrategyProperty> toJson()
      Return the placement JSON.