使用 boto3 启动 aws ec2 实例时指定 VirtualizationType

specifying VirtualizationType when launching aws ec2 instances with boto3

有人知道在使用 boto3 启动 aws ec2 实例时如何设置 VirtualizationType 吗?我试过

session = boto3.session.Session(
            aws_access_key_id=self.access_key_id,
            aws_secret_access_key=self.secret_access_key,
            region_name=self.region)
ec2resource = session.resource('ec2')
ec2resource.create_instances(
            ImageId=ami_id, MinCount=1, MaxCount=count, KeyName=key_name,
            InstanceType=type, SecurityGroups=security_groups, 
            VirtualizationType='paravirtual')

但得到了

[2016-01-15 10:54:18 CST] INFO Calling ec2:run_instances with {'VirtualizationType': 'paravirtual', 'KeyName': 'common', 'SecurityGroups': ['default'], 'MaxCount': 2, 'MinCount': 1, 'InstanceType': 'm1.small', 'ImageId': 'ami-d05e75b8'}
Traceback (most recent call last):
  File "suites/ec2.py", line 21, in <module>
    print ec2.launch(count=2, VirtualizationType='paravirtual')
  File "/Users/bchung/Dropbox/PycharmProjects/perf/lib/aws/ec2.py", line 183, in launch
    InstanceType=type, SecurityGroups=security_groups, **kwargs)
  File "/Library/Python/2.7/site-packages/boto3/resources/factory.py", line 455, in do_action
    response = action(self, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/boto3/resources/action.py", line 79, in __call__
    response = getattr(parent.meta.client, operation_name)(**params)
  File "/Library/Python/2.7/site-packages/botocore/client.py", line 310, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/Library/Python/2.7/site-packages/botocore/client.py", line 383, in _make_api_call
    api_params, operation_model, context=request_context)
  File "/Library/Python/2.7/site-packages/botocore/client.py", line 425, in _convert_to_request_dict
    api_params, operation_model)
  File "/Library/Python/2.7/site-packages/botocore/validate.py", line 273, in serialize_to_request
    raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: 
    Parameter validation failed:
    Unknown parameter in input: "VirtualizationType", must be one of: 
    DryRun, ImageId, MinCount, MaxCount, KeyName, SecurityGroups, 
    SecurityGroupIds, UserData, InstanceType, Placement, KernelId,
    RamdiskId, BlockDeviceMappings, Monitoring, SubnetId, 
    DisableApiTermination, InstanceInitiatedShutdownBehavior, 
    PrivateIpAddress, ClientToken, AdditionalInfo, NetworkInterfaces, 
    IamInstanceProfile, EbsOptimized

我需要设置 VirtualizationType 因为我需要一些只允许 paravirtual 的旧实例类型 (m1.small)而不是 hvm,boto3 似乎默认使用 hvm :

Traceback (most recent call last):
  File "suites/ec2.py", line 21, in <module>
    print ec2.launch(count=2)
  File "/Users/bchung/Dropbox/PycharmProjects/perf/lib/aws/ec2.py", line 183, in launch
    InstanceType=type, SecurityGroups=security_groups, **kwargs)
  File "/Library/Python/2.7/site-packages/boto3/resources/factory.py", line 455, in do_action
    response = action(self, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/boto3/resources/action.py", line 79, in __call__
    response = getattr(parent.meta.client, operation_name)(**params)
  File "/Library/Python/2.7/site-packages/botocore/client.py", line 310, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/Library/Python/2.7/site-packages/botocore/client.py", line 396, in _make_api_call
    raise ClientError(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred 
(InvalidParameterCombination) when calling the RunInstances operation:
 Non-Windows instances with a virtualization type of 'hvm' are currently not supported for this instance type.

我检查过: http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.ServiceResource.create_instances and http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Client.run_instances

而且参数中没有 VirtualizationType,所以我想知道是否有办法设置或者因为它会被弃用所以 boto3 只是不允许指定这个?

欢迎任何提示或建议,提前致谢!

无法从 HVM AMI 映像启动准虚拟 EC2 实例。这不仅仅是 boto3 的事情,这是 AWS 的限制。准虚拟 EC2 实例只能从准虚拟 AMI 启动。

这是因为 PV EC2 实例与 HVM EC2 实例的启动方式不同。

为了启动 Linux m1.small 实例,您需要找到 and/or 创建准虚拟 AMI 映像。