aws-ecs: `ManagedInstancesCapacityProvider` causes stack deletion failure
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
The new [`ManagedInstancesCapacityProvider`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.ManagedInstancesCapacityProvider.html) suffers from the same circular dependency issues during stack deletion that were previously reported for ASG capacity provider in \#19275 and \#18179. When attempting to delete a CloudFormation stack containing an ECS cluster with a Managed Instance capacity provider, the deletion fails due to the capacity provider being unable to disassociate from the cluster while instances are still registered.
## Reproduction Steps
1. Create a CDK stack with an ECS cluster using the Managed Instance capacity provider
2. Deploy the stack successfully
3. Attempt to delete the stack using `cdk destroy`
### Expected behavior
The stack should delete cleanly, with resources removed in the correct dependency order.
### Actual behavior
1. CloudFormation stack deletion fails with `EAI_AGAIN` DNS resolution errors
```
Error: getaddrinfo EAI_AGAIN cloudformation.eu-central-1.amazonaws.com
at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:122:26) {
errno: -3001,
code: 'EAI_AGAIN',
syscall: 'getaddrinfo',
hostname: 'cloudformation.eu-central-1.amazonaws.com',
}
```
2. Force deletion of the stack removes it from CloudFormation but retains all resources
## Root Cause
The circular dependency occurs because:
- The ECS cluster cannot be deleted while the Managed Instance capacity provider is attached to it
- The container instances remain in `Deregistering` state indefinitely, preventing cluster cleanup
- The EC2 instances are protected from direct termination by ECS managed instances, preventing manual cleanup even with admin permissions
The capacity provider holds all other resource deletions hostage: once manually deleted, it triggers a cascade deletion of all previously pending deletion resources (container instances deregister, EC2 instances terminate, cluster deletes).
## Related Issues
- #19275
- #18179
The workarounds suggested in these issues (custom resources to force-delete ASGs) may need to be adapted for Managed Instances, or the CDK construct library should handle this automatically.
## Environment
### AWS CDK Library version (aws-cdk-lib)
2.223.0
### AWS CDK CLI version
2.1031.2
### Node.js Version
24.11.0
### OS
Ubuntu 24.04
### Language
TypeScript
### Language Version
No response
### Other information
No response
Contributor guide
Research direction
Start by reproducing the failure with an ECS cluster using ManagedInstancesCapacityProvider and running cdk destroy. Read related issues #19275 and #18179 for the existing ASG capacity-provider workarounds, then determine how Managed Instances should be disassociated so the stack deletes cleanly without retained resources.
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