AsgCapacityProvider: Cannot disable drainhooks after taskDrainTime got deprecated
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the bug
Previously in CDKv1 [1] there was a taskDrainTime property which could be used to disable drainhooks. After it got removed, there is no way to remove the drainhooks without enabling "enableManagedTerminationProtection".
From the code here:
` if (!options.taskDrainTime || options.taskDrainTime.toSeconds() !== 0) {
new InstanceDrainHook(autoScalingGroup, 'DrainECSHook', {
autoScalingGroup,
cluster: this,
drainTime: options.taskDrainTime,
topicEncryptionKey: options.topicEncryptionKey,
});
}`
It look like v2 is still using taskDrainTime to decide whether or not to disable the drain hook which doesn't seem right. Could you add taskDrainTime or a diff option to disable the drainhook?
[1] https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-ecs.AsgCapacityProviderProps.html
[2] https://github.com/aws/aws-cdk/blob/main/packages/@aws-cdk/aws-ecs/lib/cluster.ts#L559
### Expected Behavior
For there to be no drainhook or an option to disable the drainhook.
### Current Behavior
The code looks for the deprecated property "taskDrainTime" to decide make a drainhook or not. The only way to disable it is to enable enableManagedTerminationProtection which cause the value of taskDrainTime to be 0.
### Reproduction Steps
When you run this it will create a drain hook.
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as autoscaling from 'aws-cdk-lib/aws-autoscaling';
import * as ecs from 'aws-cdk-lib/aws-ecs';
export class CdktestStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// VPC
const vpc = new ec2.Vpc(this, 'TheVPC', {
ipAddresses: ec2.IpAddresses.cidr('10.0.0.0/16'),
})
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: autoScalingGroup,
enableManagedTerminationProtection: false,
enableManagedScaling: false,
});
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,
},
],
});
}
}
### Possible Solution
Add an option to disable the drainhook.
### Additional Information/Context
_No response_
### CDK CLI Version
2.69.0 (build 60a5b2a)
### Framework Version
_No response_
### Node.js Version
v18.15.0
### OS
Windows 11
### Language
Typescript
### Language Version
_No response_
### Other information
_No response_
Contributor guide
Research direction
Start at packages/@aws-cdk/aws-ecs/lib/cluster.ts around the referenced drain-hook condition and trace how AsgCapacityProvider options reach it. Review the existing AsgCapacityProvider API and related tests before deciding where the behavior is specified. Done means users have an explicit way to create the capacity provider without a drain hook, with coverage for that case and existing behavior preserved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, infrastructure
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100