Kumaran
Kumaran

Reputation: 21

Unable to create ec2 instance using boto3

#!/usr/bin/env python
import boto3
ec2 = boto3.resource('ec2', region_name='us-east-1')
vpc = ec2.Vpc('vpc-xxxxx')
subnet = ec2.Subnet('subnet-xxxxx')
security_group = ec2.SecurityGroup('sg-xxxxx')
key_pair = ec2.KeyPair('xxxx')
instance = ec2.create_instances(
ImageId='ami-43a15f3e',
MinCount=1,
MaxCount=1,
InstanceType='t2.micro')
print (instance[0].id)

Just tried to create instance by running the above boto3 script. Its failing with below error. Any help on this.

Traceback (most recent call last):
  File "boto2.py", line 12, in <module>
    InstanceType='t2.micro')
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\boto3\resources\factory.py", line 520, in do_action
    response = action(self, *args, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\boto3\resources\action.py", line 83, in __call__
    response = getattr(parent.meta.client, operation_name)(**params)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\botocore\client.py", line 314, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\botocore\client.py", line 612, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (VPCIdNotSpecified) when calling the RunInstances operation: No default VPC for this user

Upvotes: 0

Views: 2394

Answers (1)

rom
rom

Reputation: 1039

According to boto3 documentation:

[EC2-VPC] If you don't specify a subnet ID, we choose a default subnet from your default VPC for you. If you don't have a default VPC, you must specify a subnet ID in the request.

You have to provide the subnet ID to the create_instances function if you don't have a default VPC, and according to your error, it looks like you don't have a default VPC.

Try to run:

instance = ec2.create_instances(
ImageId='ami-43a15f3e',
MinCount=1,
MaxCount=1,
InstanceType='t2.micro',
SubnetId='subnet-xxxxx')

Upvotes: 2

Related Questions