PlacementStrategy

class aws_cdk.aws_ecs.PlacementStrategy(*args: Any, **kwargs)

Bases: object

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.

ExampleMetadata:

infused

Example:

vpc = ec2.Vpc.from_lookup(self, "Vpc",
    is_default=True
)

cluster = ecs.Cluster(self, "Ec2Cluster", vpc=vpc)
cluster.add_capacity("DefaultAutoScalingGroup",
    instance_type=ec2.InstanceType("t2.micro"),
    vpc_subnets=ec2.SubnetSelection(subnet_type=ec2.SubnetType.PUBLIC)
)

task_definition = ecs.TaskDefinition(self, "TD",
    compatibility=ecs.Compatibility.EC2
)

task_definition.add_container("TheContainer",
    image=ecs.ContainerImage.from_registry("foo/bar"),
    memory_limit_mi_b=256
)

run_task = tasks.EcsRunTask(self, "Run",
    integration_pattern=sfn.IntegrationPattern.RUN_JOB,
    cluster=cluster,
    task_definition=task_definition,
    launch_target=tasks.EcsEc2LaunchTarget(
        placement_strategies=[
            ecs.PlacementStrategy.spread_across_instances(),
            ecs.PlacementStrategy.packed_by_cpu(),
            ecs.PlacementStrategy.randomly()
        ],
        placement_constraints=[
            ecs.PlacementConstraint.member_of("blieptuut")
        ]
    ),
    propagated_tag_source=ecs.PropagatedTagSource.TASK_DEFINITION
)

Methods

to_json()

Return the placement JSON.

Return type:

List[PlacementStrategyProperty]

Static Methods

classmethod packed_by(resource)

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

Parameters:

resource (BinPackResource) –

Return type:

PlacementStrategy

classmethod packed_by_cpu()

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

This minimizes the number of instances in use.

Return type:

PlacementStrategy

classmethod packed_by_memory()

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

This minimizes the number of instances in use.

Return type:

PlacementStrategy

classmethod randomly()

Places tasks randomly.

Return type:

PlacementStrategy

classmethod spread_across(*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.

Parameters:

fields (str) –

Default:

attributes instanceId

Return type:

PlacementStrategy

classmethod spread_across_instances()

Places tasks evenly across all container instances in the cluster.

Return type:

PlacementStrategy