Can I re-use an EBS volume with AWS ASG?

Solution 1:

IMO, you can try to do it like this (I do something similar but with Elastic Network Interfaces). This assumes either a knowledge of AWS CloudFormation service or AWS CLI:

  1. Create an EBS volume
  2. Create a bare AutoScaling group of min/max size set to 1. This will ensure that the only instance when it gets unhealthy will be replaced by a new and healthy one.
  3. Attach the volume to the instance from UserData section of AutoScaling LaunchConfiguration resource.
  4. Now, what happens with the volume when the instance will be terminated? A new one is launched by AutoScaling group. The volume will stay attached/in-use until the old one is terminated. Then, the volume will be available again and it can be attached to a new instance.
  5. Attaching/reattaching a volume is more about timing such action. You can blindly assume UserData will be executed after the volume is available again (will the instance be terminated "quickly"?). Or you can do some status checks like describe the volume metadata, check its state and when it is not in use reattach it.

A quick example in Linux bash shell:

INST_ID='i-xyzxyzxyz' 
VOL_ID='vol-xyzxyzxyz'
VOL_STATUS=''
until [ "x$VOLUME_STATUS" == "xattached" ]; do
    VOL_STATUS=$(aws ec2 describe-volumes --volume-ids $VOL_ID --query 'Volumes[0].State')
    sleep 5
done

aws ec2 attach-volume --volume-id $VOL_ID --instance-id $INST_ID --device /dev/sdh
  1. If you want it more resilient, you can try to use AutoScaling lifecycle hooks. Before the instance is put into InService state, you can attach the volume. It will stay in this state until you tell the group to continue (otherwise it has 1h timeout by default). This is for scale-out events or when a new instance is launched. When it is terminated or scaled-in it can also wait for a signal. You would detach the volume and signal it to continue terminating it.
  2. All AWS CLI commands assumes that your instance has attached a proper IAM instance profile which allows attaching/dettaching EBS volumes or describing them.

Solution 2:

No.

Autoscaling groups start multiple EC2 instances. Each EBS volume can only be attached to a single EC2 instance. Therefore you shouldn't do this, and it's probably not possible. If you have multiple volumes and multiple instances you can probably implement some complex logic to make it work, but you shouldn't - simple architectures tend to be best.

What you can do is run an EC2 instance sharing data over NFS that the ASG accesses, or you can use AWS Elastic File System, but beware latency is relatively high.

If you expand on your requirements and use case you may get more useful answers.

Update

Based on the additional requirements, it's probably possible. It's not a standard requirement so I don't think there's any explicit support for this.

I would probably look at writing some kind of startup script which associated the volume with the EC2 instance then mounted it. There's an answer here which could be of some use. It's probably possible with some kind of setup with SNS and Lambda, but the EC2 instance would likely still need something to run to map the volume.

Someone else may be able to give you more information, I've never had to do anything like this.