Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attaching EBS during startup of Amazon EC2 Spot Instance

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.

like image 386
Ido Ran Avatar asked Dec 07 '11 23:12

Ido Ran


People also ask

Can we attach EBS volume to running EC2 instance?

To attach an EBS volume to an instance using the console Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the navigation pane, choose Volumes. Select the volume to attach and choose Actions, Attach volume. You can attach only volumes that are in the Available state.

Can you attach volumes to AWS EC2 instance once it has been created?

You can't attach instance store volumes to an instance after you've launched it. If you change the instance type, an instance store will not be attached to the new instance type.

Where is EBS attached to EC2?

Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the navigation pane, choose Instances. Select the instance. In the Description tab, for Block devices, select the block device mapping and then choose the EBS ID to view additional details for the volume.

How can an EBS volume that is currently attached?

EBS Volumes can be attached and striped across multiple similarly-provisioned EBS volumes using RAID 0 or logical volume manager software, thus aggregating available IOPs, total volume throughput, and total volume size.


2 Answers

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:

  • API Reference for attaching a EBS volume
  • Amazon-doc on instance data

Hope this helps.

like image 144
Nishant Avatar answered Oct 11 '22 20:10

Nishant


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

like image 2
Eric J. Avatar answered Oct 11 '22 20:10

Eric J.