amazon-web-services - 如何在 request_spot_instances 中附加 VPC 和子网?

标签 amazon-web-services amazon-ec2 aws-cloudformation boto3 amazon-vpc

我们有多个子网和 VPC。如何在 request_spot_instances 期间定义特定子网和 VPC?

以下是我的代码:

client = boto3.client('ec2')

response = client.request_spot_instances(
    DryRun=False,
    ClientToken=''.join(random.choices(string.ascii_lowercase + string.digits, k=10)),
    InstanceCount=1,
    Type='one-time',
    LaunchSpecification={
        'ImageId': 'ami-db710fa3',
        'KeyName': 'my_key',
        'InstanceType': 'm4.4xlarge',
        'Placement': {
            'AvailabilityZone': 'us-east-2a',
        },
        'BlockDeviceMappings': [
            {
                'Ebs': {
                    # 'SnapshotId': 'snap-f70deff0',
                    'VolumeSize': 100,
                    'DeleteOnTermination': True,
                    'VolumeType': 'gp2',
                    'Iops': 300,
                    'Encrypted': False
                },
            },
        ],

        'EbsOptimized': True,
        'Monitoring': {
            'Enabled': True
        },
        'SecurityGroupIds': ['sg-1231231' ],
        'NetworkInterfaces': [
            {
                'DeviceIndex': 123,
                'SubnetId': 'Subnet-df123123'
            },
        ],

    }
)

但是,上面的代码抛出错误,

botocore.exceptions.ClientError: An error occurred (InvalidParameterCombination) when calling the RequestSpotInstances operation: Network interfaces and an instance-level security groups may not be specified on the same request

感谢您的帮助,谢谢

最佳答案

该错误说明了一切:同一请求中可能未指定网络接口(interface)和实例级安全组

这是因为 NetworkInterfaces 有一个名为 Groups 的子参数,您可以在其中指定安全组。这是必需的,因为可以指定多个网络接口(interface),每个网络接口(interface)具有不同的安全组。

如果NetworkInterfaces指定,您可以使用SecurityGroupIds(与NetworkInterfaces级别相同)并且这些组将应用于使用实例创建的默认网络接口(interface)。

因此,如果您实际上不需要 'DeviceIndex': 123,只需删除整个 NetworkInterfaces 位就可以了。

关于amazon-web-services - 如何在 request_spot_instances 中附加 VPC 和子网?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51048837/

相关文章:

amazon-web-services - 如何使用 CloudFormation 创建此 DynamoDb 表组?

java - Elastic Beanstalk (AWS) 的 Hibernate 配置

amazon-web-services - 如何为 SageMaker 提取预构建的 docker 镜像?

amazon-web-services - 如何将目标添加到 CloudFormation 中的网络负载均衡器

python - 如何使用 Python AWS AppSync 客户端?

linux - 如何在亚马逊ec2中共享网络应用程序

amazon-web-services - AWS 不断间歇性地请求新的主机 key

amazon-web-services - Cloudformation "AWS::EC2::SecurityGroup"对象,原因为 "No default VPC for this user"

aws-lambda - 如何在 Cloudformation 的输出中显示 lambda 函数返回值

amazon-web-services - 更新 Cloudformation 堆栈名称