AsgCapacityProvider

class aws_cdk.aws_ecs.AsgCapacityProvider(scope, id, *, auto_scaling_group, capacity_provider_name=None, enable_managed_draining=None, enable_managed_scaling=None, enable_managed_termination_protection=None, instance_warmup_period=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, 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
)

# Either add default capacity
cluster.add_capacity("DefaultAutoScalingGroupCapacity",
    instance_type=ec2.InstanceType("t2.xlarge"),
    desired_capacity=3
)

# Or add customized capacity. Be sure to start the Amazon ECS-optimized AMI.
auto_scaling_group = autoscaling.AutoScalingGroup(self, "ASG",
    vpc=vpc,
    instance_type=ec2.InstanceType("t2.xlarge"),
    machine_image=ecs.EcsOptimizedImage.amazon_linux(),
    # Or use Amazon ECS-Optimized Amazon Linux 2 AMI
    # machineImage: EcsOptimizedImage.amazonLinux2(),
    desired_capacity=3
)

capacity_provider = ecs.AsgCapacityProvider(self, "AsgCapacityProvider",
    auto_scaling_group=auto_scaling_group
)
cluster.add_asg_capacity_provider(capacity_provider)
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. If the stack name starts with aws, ecs, or fargate, a unique resource name is generated that starts with cp-. Default: CloudFormation-generated name

  • enable_managed_draining (Optional[bool]) – Managed instance draining facilitates graceful termination of Amazon ECS instances. This allows your service workloads to stop safely and be rescheduled to non-terminating instances. Infrastructure maintenance and updates are preformed without disruptions to workloads. To use managed instance draining, set enableManagedDraining to true. Default: true

  • enable_managed_scaling (Optional[bool]) – When enabled the scale-in and scale-out actions of the cluster’s Auto Scaling Group will be managed for you. This means your cluster will automatically scale instances based on the load your tasks put on the cluster. For more information, see Using Managed Scaling in the ECS Developer Guide. Default: true

  • enable_managed_termination_protection (Optional[bool]) – When enabled the Auto Scaling Group will only terminate EC2 instances that no longer have running non-daemon tasks. Scale-in protection will be automatically enabled on instances. When all non-daemon tasks are stopped on an instance, ECS initiates the scale-in process and turns off scale-in protection for the instance. The Auto Scaling Group can then terminate the instance. For more information see Managed termination protection in the ECS Developer Guide. Managed scaling must also be enabled. Default: true

  • instance_warmup_period (Union[int, float, None]) – The period of time, in seconds, after a newly launched Amazon EC2 instance can contribute to CloudWatch metrics for Auto Scaling group. Must be between 0 and 10000. Default: 300

  • 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

  • topic_encryption_key (Optional[IKey]) – If 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_draining

Whether managed draining is enabled.

enable_managed_termination_protection

Whether managed termination protection is enabled.

machine_image_type

Auto Scaling Group machineImageType.

node

The tree node.

Static Methods

classmethod is_construct(x)

Checks if x is a construct.

Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.

Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.

Parameters:

x (Any) – Any object.

Return type:

bool

Returns:

true if x is an object created from a class which extends Construct.