如何使用Boto启动EC2实例,指定EBS的大小?

时间:2023-01-26 12:14:03

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 EBS volume.

我正在使用boto / python来启动一个从EBS卷启动的新EC2实例。在我启动实例的时候,我想覆盖引导EBS卷的默认大小。

I found no boto methods or parameters that might fit into my launch code:

我发现没有可能适合我的启动代码的boto方法或参数:

ec2 = boto.connect_ec2( ACCESS_KEY, SECRET_KEY, region=region )

reservation = ec2.run_instances( image_id=AMI_ID, 
                                 key_name=EC2_KEY_HANDLE, 
                                 instance_type=INSTANCE_TYPE,
                                 security_groups = [ SECGROUP_HANDLE, ] )

This web page shows how to increase the size of a running EC2-instance's EBS volume using command-line tools, but I'd like to use boto at the time the EC2 instance is specified:

此网页显示如何使用命令行工具增加正在运行的EC2实例的EBS卷的大小,但我想在指定EC2实例时使用boto:

2 个解决方案

#1


37  

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:

在此之后,您可以在run_instances调用中提供块设备映射:

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.

不幸的是,这并没有很好地记录,但可以在源代码中找到该示例。

#2


0  

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

您还可以使用CloudFormation,它用于记录和自动化您的环境。您可以在以下位置检查ESB定义的模板: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.

然后,您可以使用Boto CloudFormation API来部署您的环境。

#1


37  

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:

在此之后,您可以在run_instances调用中提供块设备映射:

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.

不幸的是,这并没有很好地记录,但可以在源代码中找到该示例。

#2


0  

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

您还可以使用CloudFormation,它用于记录和自动化您的环境。您可以在以下位置检查ESB定义的模板: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.

然后,您可以使用Boto CloudFormation API来部署您的环境。