How to specify root volume size of core-os ec2 instance using boto3?

三世轮回 提交于 2019-12-03 16:10:40

Ran into this issue myself today, probably to late for the original poster, but in case anyone else stumbles across this question later I did the following:

import boto3
ec2 = boto3.resource('ec2',
                     region_name='eu-west-1',
                     aws_access_key_id='my-key',
                     aws_secret_access_key='my-secret')
instance = ec2.create_instances(ImageId='my-image-id',
                                BlockDeviceMappings=[{"DeviceName": "/dev/xvda","Ebs" : { "VolumeSize" : 50 }}])

The above has been truncated (you'll need to pass more arguments to create_instances for other values, InstanceType etc.) but essentially pass the root device (/dev/xvda in this case) in as part of the BlockDeviceMappings value with the desired volume size (50GB in the above example).

Same as Steve Jeffereies has mentioned, naming the DeviceName is the key. I was able to use /dev/sda1 which you would usually see on AWS console. The following is a working example using magnetic,

BlockDeviceMappings=[
    {
        'DeviceName': '/dev/sda1',
        'Ebs': {
            'VolumeSize': 30,
            'VolumeType': 'standard'
        }
    }
]

Follow is minimal required fields that have to be set the size of the root device:

import boto3
ec2_resource = boto3.resource('ec2')
    reservations = ec2_resource.create_instances(

        ImageId= "ami-xyz",
        MinCount=1,
        MaxCount=1,
        InstanceType='xyz',
        KeyName='key-pair',
        TagSpecifications=[
            {
                'ResourceType': 'instance',
                'Tags': [{
                     'Key': 'Name',
                    'Value': 'xyz-machine'
                }]
            }
        ],
        IamInstanceProfile={
        'Name':'xyz-role'
        },
        BlockDeviceMappings=[
            {
                'DeviceName': '/dev/sda1',
                'Ebs': {
                    'VolumeSize': 30,
                    'VolumeType': 'standard'
                }
            }
        ]
    )

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

Also, here's a way to do it from the AWS Command-Line Interface (CLI):

aws ec2 run-instances --image-id ami-xxxxxxxx --instance-type m1.xlarge --block-device-mappings '{"DeviceName": "/dev/sda1","Ebs" : { "VolumeSize" : 50 }}'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!