skip to Main Content

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


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

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

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

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

    import * as cdk from '@aws-cdk/core';
    import * as ec2 from '@aws-cdk/aws-ec2';
    
    class DeleteOnTerminationStack extends cdk.Stack {
      constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
        super(scope, id, props);
    
        // Define an Amazon Machine Image (AMI)
        const ami = ec2.MachineImage.latestAmazonLinux();
    
        // Create an EC2 instance
        const instance = new ec2.Instance(this, 'DeleteOnTerminationInstance', {
          instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.MICRO),
          machineImage: ami,
          vpc: new ec2.Vpc(this, 'DeleteOnTerminationVPC', { maxAzs: 2 }), // Create a new VPC or use an existing one
          blockDevices: [{
            deviceName: '/dev/sdh',
            volume: ec2.BlockDeviceVolume.ebs(50, {
              deleteOnTermination: true, // Enable the "Delete on Termination" flag
              encrypted: true, // Optional: to enable encryption
              // You can specify other properties of the EBS volume here
            }),
          }],
        });
      }
    }
    
    const app = new cdk.App();
    new DeleteOnTerminationStack(app, 'DeleteOnTerminationStack');
    
    
    Login or Signup to reply.
  2. 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:

      BlockDeviceMappings:
        - DeviceName: /dev/xvda
          Ebs:
            DeleteOnTermination: true
            VolumeSize: 2000
    

    Those settings can be assigned in CDK via the ASG, Instance, or LaunchTemplate resources.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search