Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch EC2 instance with Boto, specifying size of EBS?

I'm using boto/python to launch a new EC2 instance that boots from an EBS volume. At the time I launch the instance, I'd like to override the default size of the booting EBS volume.

I found no boto methods or parameters that might fit into my launch code:

ec2 = boto.connect_ec2( ACCESS_KEY, SECRET_KEY, region=region )  reservation = ec2.run_instances( image_id=AMI_ID,                                   key_name=EC2_KEY_HANDLE,                                   instance_type=INSTANCE_TYPE,                                  security_groups = [ SECGROUP_HANDLE, ] ) 

This web page shows how to increase the size of a running EC2-instance's EBS volume using command-line tools, but I'd like to use boto at the time the EC2 instance is specified:

like image 550
Iron Pillow Avatar asked Nov 27 '12 13:11

Iron Pillow


People also ask

How do I change my EBS volume size?

To modify an EBS volume using the consoleIn the navigation pane, choose Volumes. Select the volume to modify and choose Actions, Modify volume. The Modify volume screen displays the volume ID and the volume's current configuration, including type, size, IOPS, and throughput.

How do you attach an EBS volume to an EC2 instance?

To attach an EBS volume to an instance using the consoleOpen 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.


1 Answers

You have to create a block device mapping first:

dev_sda1 = boto.ec2.blockdevicemapping.EBSBlockDeviceType() dev_sda1.size = 50 # size in Gigabytes bdm = boto.ec2.blockdevicemapping.BlockDeviceMapping() bdm['/dev/sda1'] = dev_sda1  

After this you can give the block device map in your run_instances call:

reservation = ec2.run_instances( image_id=AMI_ID,                                   key_name=EC2_KEY_HANDLE,                                   instance_type=INSTANCE_TYPE,                                  security_groups = [ SECGROUP_HANDLE, ],                                  block_device_mappings = [bdm]) 

Unfortunately this is not really well documented, but the example can be found in the source code.

like image 164
j0nes Avatar answered Oct 09 '22 00:10

j0nes