amazon-web-services - 使用 boto3 时如何创建具有自定义根卷大小的 aws 实例

标签 amazon-web-services boto3

AWS 中创建实例时卷根大小默认为 8GB,我正在尝试使用 boto3 创建一个实例但是使用不同的默认大小,例如 300GB,我目前正在尝试这样的事情但没有成功:

block_device_mappings = []


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

知道如何实现这一点吗?

最佳答案

很可能发生的情况是您使用的 AMI 使用 /dev/xvda 而不是 /dev/sda1 作为其根卷。

如今的 AMI 支持两种类型的虚拟化之一,半虚拟化 (PV) 或硬件虚拟化 (HVM),并且 PV 镜像支持 /dev/sda1 作为根设备名称,而 HVM 镜像可以指定 dev/xvda/dev/sda1(更多来自 AWS Documentation)。

您可以添加图像检查以确定您正在使用的 AMI 将其根卷设置为什么,然后使用该信息调用 create_images

下面是调用 describe_images 的代码片段,检索有关其 RootDeviceName 的信息,然后使用它来配置 block 设备映射。

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

作为引用,对 describe_images 的调用返回一个如下所示的 dict:

{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}}

关于amazon-web-services - 使用 boto3 时如何创建具有自定义根卷大小的 aws 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41223308/

相关文章:

ios - 更改 Cognito-User-Pool/AWS-Amplify;导致注册问题

python - 如何使用 boto3 获取用户当前的密码期限

python - AWS boto3 create_policy() - 指定policyDocument

wordpress - AWS WordPress ERR_TOO_MANY_REDIRECTS

amazon-web-services - 在 Amazon RDS 控制台中找不到数据库安全组

python - 在 AWS Lambda 中使用存储在 S3 上的 pickled 文件

python - 为什么 AWS SageMaker 创建 S3 存储桶

boto3 - 在 boto3 中获取当前用户帐户 ID

python - 为什么安装 boto3 会破坏我的 awscli 安装?

python - 将输入(参数)从阶跃函数传递并使用到 lambda 任务