class AsgCapacityProvider (construct)
Language | Type name |
---|---|
.NET | Amazon.CDK.AWS.ECS.AsgCapacityProvider |
Java | software.amazon.awscdk.services.ecs.AsgCapacityProvider |
Python | aws_cdk.aws_ecs.AsgCapacityProvider |
TypeScript (source) | @aws-cdk/aws-ecs » AsgCapacityProvider |
Implements
IConstruct
, 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,
});
const autoScalingGroup = new autoscaling.AutoScalingGroup(this, 'ASG', {
vpc,
instanceType: new ec2.InstanceType('t2.micro'),
machineImage: ecs.EcsOptimizedImage.amazonLinux2(),
minCapacity: 0,
maxCapacity: 100,
});
const capacityProvider = new ecs.AsgCapacityProvider(this, 'AsgCapacityProvider', {
autoScalingGroup,
});
cluster.addAsgCapacityProvider(capacityProvider);
const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef');
taskDefinition.addContainer('web', {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
memoryReservationMiB: 256,
});
new ecs.Ec2Service(this, 'EC2Service', {
cluster,
taskDefinition,
capacityProviderStrategies: [
{
capacityProvider: capacityProvider.capacityProviderName,
weight: 1,
},
],
});
Initializer
new AsgCapacityProvider(scope: Construct, id: string, props: AsgCapacityProviderProps)
Parameters
- scope
Construct
- id
string
- props
Asg
Capacity Provider Props
Construct Props
Name | Type | Description |
---|---|---|
auto | IAuto | The autoscaling group to add as a Capacity Provider. |
can | boolean | Specifies whether the containers can access the container instance role. |
capacity | string | The name of the capacity provider. |
enable | boolean | Whether to enable managed scaling. |
enable | boolean | Whether to enable managed termination protection. |
machine | Machine | What type of machine image this is. |
maximum | number | Maximum scaling step size. |
minimum | number | Minimum scaling step size. |
spot | boolean | Specify whether to enable Automated Draining for Spot Instances running Amazon ECS Services. |
target | number | Target capacity percent. |
task | Duration | The time period to wait before force terminating an instance that is draining. |
topic | 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. |
autoScalingGroup
Type:
IAuto
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)
Whether to enable managed scaling.
enableManagedTerminationProtection?
Type:
boolean
(optional, default: true)
Whether to enable managed termination protection.
machineImageType?
Type:
Machine
(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.
taskDrainTime?
⚠️ Deprecated: The lifecycle draining hook is not configured if using the EC2 Capacity Provider. Enable managed termination protection instead.
Type:
Duration
(optional, default: Duration.minutes(5))
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.
topicEncryptionKey?
Type:
IKey
(optional, default: The SNS Topic will not be encrypted.)
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.
Properties
Name | Type | Description |
---|---|---|
auto | Auto | Auto Scaling Group. |
capacity | string | Capacity provider name. |
machine | Machine | Auto Scaling Group machineImageType. |
node | Construct | The construct tree node associated with this construct. |
can | boolean | Specifies whether the containers can access the container instance role. |
enable | boolean | Whether managed termination protection is enabled. |
autoScalingGroup
Type:
Auto
Auto Scaling Group.
capacityProviderName
Type:
string
Capacity provider name.
machineImageType
Type:
Machine
Auto Scaling Group machineImageType.
node
Type:
Construct
The construct tree node associated with this construct.
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
Name | Description |
---|---|
to | Returns a string representation of this construct. |
String()
topublic toString(): string
Returns
string
Returns a string representation of this construct.