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

4

14

I cannot figure out from documentation and source code how to define size of the root device.

You can specify N additional block devices using BlockDeviceMappings section where you can declare their sizes. But there is no way to set size of the root volume. So it always create an instance with root volume of size 8GB which is the default value.

Nika answered 26/8, 2015 at 0:0 Comment(1)
AWS documentation: docs.aws.amazon.com/AWSEC2/latest/UserGuide/…Imminent
R
18

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).

Rounds answered 15/2, 2016 at 13:53 Comment(5)
For my instances, the root device is called /dev/sda1, which I had to use instead of /dev/xvda, but this is otherwise correct for me in EC2. OpsWorks users should note that OpsWorks' create_instance() API, while similar to EC2's create_instances described in this answer, has a few differences; you need to specify "ROOT_DEVICE" as the DeviceName instead of the actual name, and a VolumeType is required for every volume (set it to "gp2" to get the default general purpose SSD volume type). Yes, these inconsistencies are silly on Amazon's part.Ethnogeny
To get the device name consistently with boto3: imgs = list(ec2.images.filter(ImageIds=['my-image-id'])); device_name = imgs[0].root_device_name Selfstyled
When I do the above it works but lsblk shows /dev/svda still at 8Gb so it does not take the supplied value.Gotthelf
Further research shows me that this creates an additional 40gb volume which lacks a partition. The root volume remains present at 8Gb.Gotthelf
It seems strange I cannot set this with create_instances. I've found that the answer here just adds another EBS rather than changing the root. For me, the workaround is to generate an AMI with the required initial root size (this sticks as the default so is good when creating the new instances). Then the instances can be resized subsequently using boto3 volume size modification plus subsequent partitiion expansion. Something of a pain but doable.Gotthelf
E
8

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'
        }
    }
]
Ermeena answered 17/6, 2016 at 18:22 Comment(1)
Are you running this in a specific order within the create_instance? Because I end up having two EBSs one with the default size and the second with the size specified. Also, because they have both /dev/sda1 the instance stops itself straight away... Any idea? Edit - formattingRegeneration
D
0

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'
                }
            }
        ]
    )
Dodecahedron answered 21/9, 2019 at 1:51 Comment(0)
D
-1

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 }}'
Desideratum answered 26/8, 2015 at 3:48 Comment(1)
-1; neither your link (which is about Boto) nor your command-line incantation (which is for the command line client) answers the question, which was about Boto 3.Ethnogeny

© 2022 - 2024 — McMap. All rights reserved.