Search code examples
amazon-web-servicesaws-lambdaautoscalingamazon-ebs

How to detach an EBS volume from an EC2 (which is under ASG) before it is being scaled down


I have multiple Non-Production servers where each have 900GB EBS Volume attached. And currently there is a practice of taking snapshot of these instances every day. I want to detach this volume when ASG scales down and reattach it to them when it scales up thus reducing the size of snapshot. The 900GB volume is the Non-Prod DB Volume so I plan to schedule a weekly snapshot of it separately.

NOTE: Each Non-Production ASG will have only one EC2 spot instance max and the scaling up & down is done via scaling policies.

I am trying to setup Event Bridge notification, triggering a Lambda function whenever ASG performs an action. I want to know if there are any other AWS provided service for this or any better way to do this.


Solution

  • by default an EBS volume gets detached than the ec2 instance is removed. And for mounting it you can run a script in the user data.

    /var/awslogs/bin/aws ec2 attach-volume --volume-id `your-ebs-volume-id` --instance-id `your-instance-id` --device /dev/xvdk