【问题标题】:boto3: Spot Instance Creationboto3:Spot 实例创建
【发布时间】:2016-06-13 18:59:09
【问题描述】:

我正在尝试使用 boto3 创建一个现场实例。虽然我关注API documentation,但我收到了一个我无法弄清楚的异常。我使用的代码是:

import boto3
import datetime
client = boto3.client('ec2')
response = client.request_spot_instances(
    DryRun=False,
    SpotPrice='0.10',
    ClientToken='string',
    InstanceCount=1,
    Type='one-time',
    LaunchSpecification={
        'ImageId': 'ami-fce3c696',
        'KeyName': 'awskey.pem',
        'SecurityGroups': ['sg-709f8709'],
        'InstanceType': 'm4.large',
        'Placement': {
            'AvailabilityZone': 'us-east-1a',
        },
        'BlockDeviceMappings': [
            {
                'Ebs': {
                    'SnapshotId': 'snap-f70deff0',
                    'VolumeSize': 100,
                    'DeleteOnTermination': True,
                    'VolumeType': 'gp2',
                    'Iops': 300,
                    'Encrypted': False
                },
            },
        ],

        'EbsOptimized': True,
        'Monitoring': {
            'Enabled': True
        },
        'SecurityGroupIds': [
            'sg-709f8709',
        ]
    }
)

我收到以下异常:

botocore.exceptions.ClientError: An error occurred (InvalidParameterValue) when calling the RequestSpotInstances operation: Value () for parameter groupId is invalid. The value cannot be empty

问题是API documentation的请求中没有groupId参数。

我错过了什么吗?

【问题讨论】:

  • 为什么要同时指定SecurityGroupsSecurityGroupIds?看起来您只需要SecurityGroupIds,因为您传递的是 sg id 而不是名称。

标签: python amazon-web-services amazon-ec2 boto boto3


【解决方案1】:

尝试使用安全组 ID,它工作正常。

#!/usr/bin/python3.6
import boto3

session=boto3.session.Session(profile_name="*****")
ec2instances=session.resource('ec2',region_name="********")
new_instance = ec2instances.create_instances(
        ImageId = 'ami-04125d804acca5692',
        MinCount = 1,
        MaxCount = 1,
        InstanceType = 't2.micro',
        KeyName ='xxxxxxxxxxxxxxxxx',
        SecurityGroupIds=['sg-05dec40ce8b91a8c8'],
        SubnetId = 'subnet-01ca807d148d9e328'
)
print(new_instance)

【讨论】:

  • 最初的问题是关于现场请求,而不是按需请求。
【解决方案2】:

虽然 API 文档中没有指定,但显然 'SecurityGroups' 参数需要安全组的名称,而不是 ID。

更改为组名解决了问题。

感谢大家首先阅读这个问题。

【讨论】:

  • 这只是节省了我几个小时。
  • 你需要SecurityGroupsSecurityGroupIds吗?
猜你喜欢
  • 2019-05-22
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 2020-10-09
  • 2018-07-12
  • 2018-07-02
  • 2017-05-17
  • 2018-07-07
相关资源
最近更新 更多