6

I want to create an ASG such that I can reuse existing EBS volumes (because I have persistent data on those volumes) to new EC2 instances that are instantiated via ASG. Is this possible?

Update copied from comment

To clarify my original question, I am not trying to attach an EBS volume to two EC2 instances at the same time. Rather, when one EC2 instance is terminated, I want launch a new EC2 instance with the previous EBS volume instead of creating a brand new EBS volume

4

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.

  • EFS latency is marginally higher than EBS, but still low and consistent. Anecdotal reports of high latency seem exaggerated. For large filesystems, EFS also offers more aggregate throughput than EBS. Still, this answer is essentially correct. – Michael - sqlbot Feb 11 '17 at 17:29
  • To clarify my original question, I am not trying to attach an EBS volume to two EC2 instances at the same time. Rather, when one EC2 instance is terminated, I want launch a new EC2 instance with the previous EBS volume instead of creating a brand new EBS volume – steve landiss Feb 11 '17 at 19:14
9

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.
  • the until [ "$VOLUME_STATUS" == 'attached' ] block continually loops on AWS' Linux 2 AMIs in Bash. I've tried everything I can think of. Any idea why? – CDub Feb 13 at 3:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.