jatha
jatha

Reputation: 934

boto3: Spot Instance Creation

I'm trying to create a spot instance using boto3. Although I follow the API documentation, I receive an exception I couldn't be able to figure out. The code I'm using is:

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

And I receive the following exception:

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

The thing is there is no groupId parameter in the request in the API documentation.

Am I missing something?

Upvotes: 22

Views: 10140

Answers (2)

ThadiKaran
ThadiKaran

Reputation: 9

Try with security group ids it is working fine.

#!/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)

Upvotes: 0

jatha
jatha

Reputation: 934

Although it's not specified in the API documentation, apparently 'SecurityGroups' parameter requires the names of the security groups, not the IDs.

Changing to the group name solved the issue.

Thanks for anyone bothered to read the question in the first place.

Upvotes: 35

Related Questions