when using boto3 how to create an aws instance with a custom root volume size

1.3k views Asked by At

When creating an instance in AWS the volume root size defaults to 8GB, I am trying to create an instance using boto3 but with a different default size, for example 300GB, I am currently trying something like this without success:

block_device_mappings = []


block_device_mappings.append({                                                                                                                                                                                                                                                                                                                                                                                                                                                          
    'DeviceName': '/dev/sda1',                                                                                                                                                                                                                               
    'Ebs': {                                                                                                                                                                                                                                                 
        'VolumeSize': 300,                                                                                                                                                                                                                 
        'DeleteOnTermination': True,                                                                                                                                                                                                                         
        'VolumeType': 'gp2'                                                                                                                                                                                                                                  
} 

Any idea of how to achieve this?

1

There are 1 answers

0
wkl On BEST ANSWER

Most likely what is happening is that you're using an AMI that uses /dev/xvda instead of /dev/sda1 for its root volume.

AMIs these days support one of two types of virtualization, paravirtual (PV) or hardware virtualized (HVM), and PV images support /dev/sda1 for the root device name, whereas HVM images can specify either dev/xvda or /dev/sda1 (more from AWS Documentation).

You can add in an image check to determine what the AMI you're using sets its root volume to, and then use that information for your calls to create_images.

Here's a code snippet that makes a call out to describe_images, retrieves information about its RootDeviceName, and then uses that to configure the block device mapping.

import boto3

if __name__ == '__main__':
    client = boto3.client('ec2')

    # This grabs the Debian Jessie 8.6 image (us-east-1 region)
    image_id = 'ami-49e5cb5e'

    response = client.describe_images(ImageIds=[image_id])

    device_name = response['Images'][0]['RootDeviceName']
    print(device_name)

    block_device_mappings = []

    block_device_mappings.append({
        'DeviceName': device_name,
        'Ebs': {
        'VolumeSize': 300,
        'DeleteOnTermination': True,
        'VolumeType': 'gp2'
        }
    })

    # Whatever you need to create the instances

For reference, the call to describe_images returns a dict that looks like this:

{u'Images': [{u'Architecture': 'x86_64',
   u'BlockDeviceMappings': [{u'DeviceName': '/dev/xvda',
     u'Ebs': {u'DeleteOnTermination': True,
      u'Encrypted': False,
      u'SnapshotId': 'snap-0ddda62ff076afbc8',
      u'VolumeSize': 8,
      u'VolumeType': 'gp2'}}],
   u'CreationDate': '2016-11-13T14:03:45.000Z',
   u'Description': 'Debian jessie amd64',
   u'EnaSupport': True,
   u'Hypervisor': 'xen',
   u'ImageId': 'ami-49e5cb5e',
   u'ImageLocation': '379101102735/debian-jessie-amd64-hvm-2016-11-13-1356-ebs',
   u'ImageType': 'machine',
   u'Name': 'debian-jessie-amd64-hvm-2016-11-13-1356-ebs',
   u'OwnerId': '379101102735',
   u'Public': True,
   u'RootDeviceName': '/dev/xvda',
   u'RootDeviceType': 'ebs',
   u'SriovNetSupport': 'simple',
   u'State': 'available',
   u'VirtualizationType': 'hvm'}],
 'ResponseMetadata': {'HTTPHeaders': {'content-type': 'text/xml;charset=UTF-8',
   'date': 'Mon, 19 Dec 2016 14:03:36 GMT',
   'server': 'AmazonEC2',
   'transfer-encoding': 'chunked',
   'vary': 'Accept-Encoding'},
  'HTTPStatusCode': 200,
  'RequestId': '85a22932-7014-4202-92de-4b5ee6b7f73b',
  'RetryAttempts': 0}}