Skip to content

ECS Autoscaling Group and EC2 Instance not deleted by delete stack CDK typescript

0

In my Stack I have a Autoscaling group with an EC2 Instance. If I delete the stack this two resource will stay and I have to delete them manualy. I expected that CDK Stack will get deleteled complete. The code for this inside my stack is:

		const ec2AutoScalingGroup = new AutoScalingGroup(this, 'todo-app-asg', {
			vpc: this.mainVpc,
			instanceType: new InstanceType('t3a.2xlarge'), 
			machineImage: EcsOptimizedImage.amazonLinux2(AmiHardwareType.STANDARD),
			desiredCapacity: 2,
			minCapacity: 1,
			maxCapacity: 3,
			spotPrice: '0.5',
		  });		
		  ec2AutoScalingGroup.protectNewInstancesFromScaleIn();
		  ec2AutoScalingGroup.connections.addSecurityGroup(securityGroup);		  

		  const capacityProviderDef = new AsgCapacityProvider(this, 'todo-app-capacity-provider', {
			autoScalingGroup: ec2AutoScalingGroup,
			enableManagedTerminationProtection: true,
			enableManagedScaling: true,			
		  });
	  		  
		  cluster.addAsgCapacityProvider(capacityProviderDef);
asked a year ago256 views
1 Answer
0

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:

  1. 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.

  2. 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.

  3. 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:

  1. Remove the protectNewInstancesFromScaleIn() call if you don't need this protection.

  2. Set enableManagedTerminationProtection: false in your capacity provider definition.

  3. Consider setting a lower desiredCapacity or even setting it to 0 before deleting the stack.

  4. If you're using the --force-delete option 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

answered a year ago
EXPERT
reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.