Attaching EBS during startup of Amazon EC2 Spot Instance
Asked Answered
C

2

6

I would like to run an Amazon EC2 spot instance as CI server. In order to keep the historical information I would like to keep all the data on separate EBS volume due to the fact the EBS volume of the spot instance will be deleted each time it will be terminated.

How can I mount the EBS volume during startup?

Thank you, Ido.

Cosmopolite answered 7/12, 2011 at 23:2 Comment(0)
S
5

you can have a init script that attaches the already available volume, using this command:

ec2-attach-volume volume_id --instance instance_id --device device 

for this to work, you need

  1. EC2-API installed on the machine

  2. you need to have EC2_PRIVATE_KEY and EC2_CERT as environment variable or will have to pass explicitly.

  3. The volume you're trying to attach should be available. Or, optionally, you can first force detach the volume.

  4. you may use curl http://169.254.169.254/latest/meta-data/instance-id to get hold of current instance's instance_id.

Refer:

Hope this helps.

Souse answered 8/12, 2011 at 5:28 Comment(2)
Is there any way to do this with the root volume ? Presumably you cant use the init script because if its already using the root disk to run the script, then it's already too late to make the existing EBS disk the root volume ?Thrave
Also be aware that init scripts are run by root, so your credentials need to be added as root. awscli needs this in particular. After attaching the device, it may need to be explicitly mounted.Honeysucker
C
2

You can use the EC2 API and knowledge of your EBS volume IDs to do this.

The exact steps depend on your operating system. Here's a link that shows how to do it on Linux

http://ec2dream.blogspot.com/search/label/EBS

That particular script is in Ruby, which you may need to install depending on your Linux distribution. The principals can be easily ported to e.g. bash.

If your instance is running Windows, see this instead:

Automount EBS volume in Amazon EC2 Windows Instance

Cadmann answered 7/12, 2011 at 23:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.