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

前端 未结 2 1753
天命终不由人
天命终不由人 2020-12-12 21:16

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

相关标签:
2条回答
  • 2020-12-12 21:57

    You can also use CloudFormation, which is used to document and automate your environment. You can check the template for the ESB definition at: https://s3.amazonaws.com/cloudformation-templates-us-east-1/EC2WithEBSSample.template

     "Resources" : {
        "Ec2Instance" : {
          "Type" : "AWS::EC2::Instance",
          "Properties" : {
            "AvailabilityZone" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "TestAz" ]},
            "SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ],
            "KeyName" : { "Ref" : "KeyName" },
            "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]},
            "Volumes" : [ 
              { "VolumeId" : { "Ref" : "NewVolume" },
                "Device" : "/dev/sdk"
              }
            ]
          }
        },
    
        ...
    
        "NewVolume" : {
          "Type" : "AWS::EC2::Volume",
          "Properties" : {
            "Size" : "100",
            "AvailabilityZone" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "TestAz" ]}
          }
        }
    

    You can then use Boto CloudFormation API to deploy your environment.

    0 讨论(0)
  • 2020-12-12 21:59

    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.

    0 讨论(0)
提交回复
热议问题