aws / aws/aws-cdk

(aws-ecs): Deleting a stack with a Cluster and an ASG capacity provider fails

Open
#19,275 12 comments 7 reactions 0 assignees View on GitHub
@aws-cdk/aws-ecs bug effort/small p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
1d 19h
Merged PRs (30d)
71

Description

### What is the problem?

If a stack contains a ECS cluster with an ASG capacity provider, deleting the stack will fail due to an apparent circular dependency.

### Reproduction Steps

Minimal stack:

```
class ClusterWithCapacity(cdk.Stack):
def __init__(
self,
scope: Construct,
id: str,
**kwargs,
) -> None:
super().__init__(scope, id, **kwargs)
vpc = ec2.Vpc(self, "vpc")
ecs_cluster = ecs.Cluster(self, "cluster", vpc=vpc)

asg = autoscaling.AutoScalingGroup(
self,
"asg",
vpc=vpc,
instance_type=ec2.InstanceType.of("t3-small"),
machine_image=ecs.EcsOptimizedImage.amazon_linux2(),
)

capacity_provider = ecs.AsgCapacityProvider(
self,
"capacity_provider",
auto_scaling_group=asg,
)

ecs_cluster.add_asg_capacity_provider(provider=capacity_provider)
```

Deploy the stack and try deleting it.

### What did you expect to happen?

The stack is deleted.

### What actually happened?

The capacity provider fails to delete with the following error:
`Resource handler returned message: "Invalid request provided: Cannot delete a capacity provider while it is associated with a cluster."`

### CDK CLI Version

2.15.0

### Framework Version

_No response_

### Node.js Version

17.6.0

### OS

MacOS

### Language

Python

### Language Version

_No response_

### Other information

This hints that the cluster needs to be deleted first, but the capacity provider actually depends on the cluster.

Creating an explicit dependency `ecs_cluster.node.add_dependency(capacity_provider)` results in a circular dependency.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the minimal Python stack using the ECS Cluster, AutoScalingGroup, and AsgCapacityProvider entry points shown in the issue, then inspect the corresponding aws-cdk ECS construct implementation. Done means the stack can be deleted without the capacity provider error or a circular dependency.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, python, 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.