aws-cdk-lib.aws_ecs.AsgCapacityProvider

class AsgCapacityProvider (construct)

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

Implements IConstruct, IDependable

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.

Example

declare const vpc: ec2.Vpc;

const cluster = new ecs.Cluster(this, 'Cluster', {
  vpc,
});

// Either add default capacity
cluster.addCapacity('DefaultAutoScalingGroupCapacity', {
  instanceType: new ec2.InstanceType("t2.xlarge"),
  desiredCapacity: 3,
});

// Or add customized capacity. Be sure to start the Amazon ECS-optimized AMI.
const autoScalingGroup = new autoscaling.AutoScalingGroup(this, 'ASG', {
  vpc,
  instanceType: new ec2.InstanceType('t2.xlarge'),
  machineImage: ecs.EcsOptimizedImage.amazonLinux(),
  // Or use Amazon ECS-Optimized Amazon Linux 2 AMI
  // machineImage: EcsOptimizedImage.amazonLinux2(),
  desiredCapacity: 3,
  // ... other options here ...
});

const capacityProvider = new ecs.AsgCapacityProvider(this, 'AsgCapacityProvider', {
  autoScalingGroup,
});
cluster.addAsgCapacityProvider(capacityProvider);

Initializer

new AsgCapacityProvider(scope: Construct, id: string, props: AsgCapacityProviderProps)

Parameters

  • scope Construct
  • id string
  • props AsgCapacityProviderProps

Construct Props

NameTypeDescription
autoScalingGroupIAutoScalingGroupThe autoscaling group to add as a Capacity Provider.
canContainersAccessInstanceRole?booleanSpecifies whether the containers can access the container instance role.
capacityProviderName?stringThe name of the capacity provider.
enableManagedScaling?booleanWhen enabled the scale-in and scale-out actions of the cluster's Auto Scaling Group will be managed for you.
enableManagedTerminationProtection?booleanWhen enabled the Auto Scaling Group will only terminate EC2 instances that no longer have running non-daemon tasks.
machineImageType?MachineImageTypeWhat type of machine image this is.
maximumScalingStepSize?numberMaximum scaling step size.
minimumScalingStepSize?numberMinimum scaling step size.
spotInstanceDraining?booleanSpecify whether to enable Automated Draining for Spot Instances running Amazon ECS Services.
targetCapacityPercent?numberTarget capacity percent.
topicEncryptionKey?IKeyIf 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.

autoScalingGroup

Type: IAutoScalingGroup

The autoscaling group to add as a Capacity Provider.


canContainersAccessInstanceRole?

Type: boolean (optional, default: false)

Specifies whether the containers can access the container instance role.


capacityProviderName?

Type: string (optional, default: CloudFormation-generated name)

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.


enableManagedScaling?

Type: boolean (optional, default: true)

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.


enableManagedTerminationProtection?

Type: boolean (optional, default: true)

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.


machineImageType?

Type: MachineImageType (optional, default: Automatically determined from machineImage, if available, otherwise MachineImageType.AMAZON_LINUX_2.)

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.


maximumScalingStepSize?

Type: number (optional, default: 1000)

Maximum scaling step size.

In most cases this should be left alone.


minimumScalingStepSize?

Type: number (optional, default: 1)

Minimum scaling step size.

In most cases this should be left alone.


spotInstanceDraining?

Type: boolean (optional, default: false)

Specify whether to enable Automated Draining for Spot Instances running Amazon ECS Services.

For more information, see Using Spot Instances.


targetCapacityPercent?

Type: number (optional, default: 100)

Target capacity percent.

In most cases this should be left alone.


topicEncryptionKey?

Type: IKey (optional, default: The SNS Topic will not be encrypted.)

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.

Properties

NameTypeDescription
autoScalingGroupAutoScalingGroupAuto Scaling Group.
capacityProviderNamestringCapacity provider name.
machineImageTypeMachineImageTypeAuto Scaling Group machineImageType.
nodeNodeThe tree node.
canContainersAccessInstanceRole?booleanSpecifies whether the containers can access the container instance role.
enableManagedTerminationProtection?booleanWhether managed termination protection is enabled.

autoScalingGroup

Type: AutoScalingGroup

Auto Scaling Group.


capacityProviderName

Type: string

Capacity provider name.


machineImageType

Type: MachineImageType

Auto Scaling Group machineImageType.


node

Type: Node

The tree node.


canContainersAccessInstanceRole?

Type: boolean (optional, default: false)

Specifies whether the containers can access the container instance role.


enableManagedTerminationProtection?

Type: boolean (optional)

Whether managed termination protection is enabled.

Methods

NameDescription
toString()Returns a string representation of this construct.

toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.