AsgCapacityProvider

class aws_cdk.aws_ecs.AsgCapacityProvider(scope, id, *, auto_scaling_group, capacity_provider_name=None, enable_managed_scaling=None, enable_managed_termination_protection=None, maximum_scaling_step_size=None, minimum_scaling_step_size=None, target_capacity_percent=None, can_containers_access_instance_role=None, machine_image_type=None, spot_instance_draining=None, task_drain_time=None, topic_encryption_key=None)

Bases: Construct

An Auto Scaling Group Capacity Provider.

This allows an ECS cluster to target a specific EC2 Auto Scaling Group for the placement of tasks. Optionally (and recommended), ECS can manage the number of instances in the ASG to fit the tasks, and can ensure that instances are not prematurely terminated while there are still tasks running on them.

ExampleMetadata:

infused

Example:

# vpc: ec2.Vpc


cluster = ecs.Cluster(self, "Cluster",
    vpc=vpc
)

auto_scaling_group = autoscaling.AutoScalingGroup(self, "ASG",
    vpc=vpc,
    instance_type=ec2.InstanceType("t2.micro"),
    machine_image=ecs.EcsOptimizedImage.amazon_linux2(),
    min_capacity=0,
    max_capacity=100
)

capacity_provider = ecs.AsgCapacityProvider(self, "AsgCapacityProvider",
    auto_scaling_group=auto_scaling_group
)
cluster.add_asg_capacity_provider(capacity_provider)

task_definition = ecs.Ec2TaskDefinition(self, "TaskDef")

task_definition.add_container("web",
    image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"),
    memory_reservation_mi_b=256
)

ecs.Ec2Service(self, "EC2Service",
    cluster=cluster,
    task_definition=task_definition,
    capacity_provider_strategies=[ecs.CapacityProviderStrategy(
        capacity_provider=capacity_provider.capacity_provider_name,
        weight=1
    )
    ]
)
Parameters:
  • scope (Construct)

  • id (str)

  • auto_scaling_group (IAutoScalingGroup) – The autoscaling group to add as a Capacity Provider.

  • capacity_provider_name (Optional[str]) – The name of the capacity provider. If a name is specified, it cannot start with aws, ecs, or fargate. If no name is specified, a default name in the CFNStackName-CFNResourceName-RandomString format is used. Default: CloudFormation-generated name

  • enable_managed_scaling (Optional[bool]) – Whether to enable managed scaling. Default: true

  • enable_managed_termination_protection (Optional[bool]) – Whether to enable managed termination protection. Default: true

  • maximum_scaling_step_size (Union[int, float, None]) – Maximum scaling step size. In most cases this should be left alone. Default: 1000

  • minimum_scaling_step_size (Union[int, float, None]) – Minimum scaling step size. In most cases this should be left alone. Default: 1

  • target_capacity_percent (Union[int, float, None]) – Target capacity percent. In most cases this should be left alone. Default: 100

  • can_containers_access_instance_role (Optional[bool]) – Specifies whether the containers can access the container instance role. Default: false

  • machine_image_type (Optional[MachineImageType]) – What type of machine image this is. Depending on the setting, different UserData will automatically be added to the AutoScalingGroup to configure it properly for use with ECS. If you create an AutoScalingGroup yourself and are adding it via addAutoScalingGroup(), you must specify this value. If you are adding an autoScalingGroup via addCapacity, this value will be determined from the machineImage you pass. Default: - Automatically determined from machineImage, if available, otherwise MachineImageType.AMAZON_LINUX_2.

  • spot_instance_draining (Optional[bool]) – Specify whether to enable Automated Draining for Spot Instances running Amazon ECS Services. For more information, see Using Spot Instances. Default: false

  • task_drain_time (Optional[Duration]) – (deprecated) The time period to wait before force terminating an instance that is draining. This creates a Lambda function that is used by a lifecycle hook for the AutoScalingGroup that will delay instance termination until all ECS tasks have drained from the instance. Set to 0 to disable task draining. Set to 0 to disable task draining. Default: Duration.minutes(5)

  • topic_encryption_key (Optional[IKey]) – If {@link AddAutoScalingGroupCapacityOptions.taskDrainTime} is non-zero, then the ECS cluster creates an SNS Topic to as part of a system to drain instances of tasks when the instance is being shut down. If this property is provided, then this key will be used to encrypt the contents of that SNS Topic. See SNS Data Encryption for more information. Default: The SNS Topic will not be encrypted.

Methods

to_string()

Returns a string representation of this construct.

Return type:

str

Attributes

auto_scaling_group

Auto Scaling Group.

can_containers_access_instance_role

Specifies whether the containers can access the container instance role.

Default:

false

capacity_provider_name

Capacity provider name.

Default:

Chosen by CloudFormation

enable_managed_termination_protection

Whether managed termination protection is enabled.

machine_image_type

Auto Scaling Group machineImageType.

node

The construct tree node associated with this construct.

Static Methods

classmethod is_construct(x)

Return whether the given object is a Construct.

Parameters:

x (Any)

Return type:

bool