0
votes

We have multiple subnet and VPC. How do i define specific subnet and VPC during request_spot_instances?

following is my code:

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'
            },
        ],

    }
)

However, above code is throwing errors,

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

any help is appreciated Thank you

1

1 Answers

2
votes

The error says it all: Network interfaces and an instance-level security groups may not be specified on the same request

This is because NetworkInterfaces has a sub-parameter called Groups where you specify the Security Group. This is required because multiple network interfaces can be specified, with different security groups for each.

If NetworkInterfaces is not specified, you can use SecurityGroupIds (at the same level as NetworkInterfaces) and the groups will be applied to the default network interface created with the instance.

So, if you don't actually need 'DeviceIndex': 123, just delete the whole NetworkInterfaces bit and you should be fine.