- Newest
- Most votes
- Most comments
The issue you're experiencing with the EC2 Auto Scaling group and EC2 instances not being deleted when you delete the CDK stack is likely due to the configuration of your Auto Scaling group and capacity provider. There are a few factors contributing to this behavior:
-
Instance protection: You're using the
protectNewInstancesFromScaleIn()method on your Auto Scaling group. This protects instances from being terminated during scale-in events, which can interfere with the deletion process. -
Managed termination protection: In your capacity provider definition, you've set
enableManagedTerminationProtection: true. This feature protects instances that are part of an ECS task from being terminated, which can prevent the Auto Scaling group from being deleted. -
Spot instances: You're using spot instances (
spotPrice: '0.5'), which may have different termination behaviors compared to on-demand instances.
To ensure that your Auto Scaling group and EC2 instances are properly deleted when you delete the stack, you can try the following modifications:
-
Remove the
protectNewInstancesFromScaleIn()call if you don't need this protection. -
Set
enableManagedTerminationProtection: falsein your capacity provider definition. -
Consider setting a lower
desiredCapacityor even setting it to 0 before deleting the stack. -
If you're using the
--force-deleteoption when deleting the stack, be aware that it might not work as expected with protected instances.
If you still encounter issues after making these changes, you may need to manually set the desired capacity of the Auto Scaling group to 0 before deleting the stack, or use the AWS CLI or SDK to force-delete the Auto Scaling group and terminate any remaining instances.
Remember that when working with Auto Scaling groups and capacity providers, it's important to carefully consider the protection and termination settings to ensure proper cleanup when resources are no longer needed.
Sources
Delete your Auto Scaling infrastructure - Amazon EC2 Auto Scaling
DeleteAutoScalingGroup - Amazon EC2 Auto Scaling
How to use EC2 Auto Scaling with EC2 Spot Instances
Relevant content
- asked 3 years ago
- asked 3 years ago
