I have an auto scaling group that uses an AMI that has EBS volumes associated with it. Every time that the group scales those EBS volumes get recreated.
Also due to reasons, i have a lifetime limit on the scaling group servers of 24 hours.
I’ve found that while the EC2 instance cleans itself up well, it leaves the EBS volumes in a detached state. Over the course of the month this has caused my EBS storage costs to explode because each volume created is 1tb in size.
I’m using CDK to create my scaling group and i can’t seem to find any way to set the retention policy on the resources created.
Anyone have a way to have the EBS volumes destroyed upon instance termination?
const scalingGroup = new AutoScalingGroup(this, 'ServerScalingGroup', {
vpc,
role,
securityGroup,
minCapacity: 2,
maxCapacity: 20,
requireImdsv2: true,
instanceType: InstanceType.of(InstanceClass.C7I, InstanceSize.XLARGE),
machineImage: MachineImage.lookup({
windows: true,
name: options.ami
}),
healthCheck: HealthCheck.elb({
grace: cdk.Duration.minutes(5)
}),
maxInstanceLifetime: cdk.Duration.days(1),
cooldown: cdk.Duration.minutes(2)
});
I see two potential solutions here using notifications
or using addLifecycleHook()
but both of those seem like hacks.
2
Answers
When Application Auto-Scaling terminates an Amazon EC2 instance, it does not automatically delete the Amazon Elastic Block Store (EBS) volumes attached to that instance by default. This behavior is by design for several reasons:
Data Persistence: EBS volumes are designed to offer persistent storage that outlives the lifespan of an EC2 instance. When an instance is terminated, users may still want to retain the data on the EBS volumes for future use, analysis, backup, or for attaching to another instance.
Safety and Data Protection: Automatically deleting EBS volumes upon instance termination could lead to accidental data loss. By not deleting the EBS volumes automatically, AWS adds a layer of protection against unintended data deletion.
Flexibility: Users have different needs and use cases. Some may want to keep the data on the EBS volumes for archival purposes, while others might use the volumes as a way to quickly bootstrap data onto new instances. AWS provides the flexibility for users to decide what to do with the EBS volumes after an instance is terminated.
However, AWS allows users to configure this behavior based on their preferences and requirements. When launching an EC2 instance, you can specify whether you want the attached EBS volumes to be deleted when the instance is terminated. This is done by setting the "Delete on Termination" flag for each EBS volume attached to the instance. If this flag is set to true, the EBS volume will be automatically deleted when the instance is terminated. This setting can be configured via the AWS Management Console, AWS CLI, or AWS SDKs.
With CDK, it’s possible to control this setting, as well. Here’s the example code:
If the block devices setting is explicitly set in the CDK (or CF), the setting from the AMI is ignored. Here’s the relevant yaml from a a CF stack for a vendor-provided AMI:
Those settings can be assigned in CDK via the ASG, Instance, or LaunchTemplate resources.