I am using boto, the code like this:
dev_xvdb = boto.ec2.blockdevicemapping.EBSBlockDeviceType(volume_id='vol-xxxxxx')
bdm = boto.ec2.blockdevicemapping.BlockDeviceMapping()
bdm['/dev/xvdb'] = dev_xvdb
rs = ec2.request_spot_instances(price=MY_MAX_PRICE,
image_id='ami-xxxxx',
count=1,
type='one-time',
key_name='MY_KEY_NAME',
security_groups=['default'],
instance_type='t1.micro',
block_device_map=bdm)
This code can be run properly, but can’t attach the EBS volume(id=vol-xxxxxx). Why?
I think the problem here is that you cannot attach an existing volume to an instance with BlockDeviceMapping. The BlockDeviceMapping allows you to specify either a volume size or a snapshot-id. If you specify a size, it will create a new, blank volume of that size and attach it to the device you specify. If you specify a snapshot-id, it will create a new volume from that snapshot and attach it.
If you want to attach an existing volume to an instance you have to use the
attach_volumemethod which can only be run after you have an instance ID.Why then, you might ask, does boto’s EBSBlockDeviceType have a
volume_idattribute? That’s because when we do aget_all_instancescall, the data returned by AWS for BlockDeviceMapping includes thevolume_idfor currently connected EBS volumes.