user5333832
user5333832

Reputation:

How to Launch a instance in VPC using boto?

I need to launch a instance from a AMI .. moreover.. i need to launch the new instance with same attributes as the original instance from which the AMI was created.. i.e ,, same VPC id , same key_name, same _region etc etc

my code accepts is:

reservations = conn.get_all_instances(instance_ids=[sys.argv[1]])
instances = [i for r in reservations for i in r.instances]
for i in instances:
    key_name = i.key_name
#    security_group = "sg-f05ee295"      #i.groups
    instance_type = i.instance_type
#    print security_group[0]
#    subnet_name = i.subnet_id
    reserve = conn.run_instances(image_id=ami_id,key_name=key_name,instance_type=instance_type,security_group_ids =['sg-f05ee295'])
    print "new replica system id is " + reserve.instances[0].id

This is not working, since it says::

boto.exception.EC2ResponseError: EC2ResponseError: 400 Bad Request
<?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>InvalidParameterCombination</Code><Message>VPC security groups may not be used for a non-VPC launch</Message></Error></Errors><RequestID>57c29a87-8f6c-462d-a16b-7a4888dd5341</RequestID></Response>

HELP ME OUT!!!!!!!

Upvotes: 2

Views: 1244

Answers (2)

padmakarojha
padmakarojha

Reputation: 335

So basically , answer by Nishant will work , but you won't get exact replica just in case there are more than one SG's :

For that refer to the code below:

for i in instances:
    key_name = i.key_name
    security_group = []
    for each in i.groups:
         security_group.append(each.id)
    instance_type = i.instance_type
    subnet_name = i.subnet_id
    reserve = conn.run_instances(image_id=ami_id,subnet_id=subnet_name ,key_name=key_name,instance_type=instance_type,security_group_ids =security_group)
print "new replica system id is " + reserve.instances[0].id

Reference : I answered the same question on this thread How to Launch a exact same replica of a EC2 instance in VPC from the AMI of a previous EC2 instance

Upvotes: 1

Nishant Singh
Nishant Singh

Reputation: 3209

I ran into same issue.. all u need to do is this

reservations = conn.get_all_instances(instance_ids=[sys.argv[1]])
instances = [i for r in reservations for i in r.instances]
for i in instances:
    key_name = i.key_name
    security_group = i.groups[0].id
    instance_type = i.instance_type
    print "Now Spinning New Instance"
    subnet_name = i.subnet_id
    reserve = conn.run_instances(image_id=ami_id,key_name=key_name,instance_type=instance_type,security_group_ids=[security_group],subnet_id=subnet_name)

This will solve your problem. Regards \m/

Upvotes: 1

Related Questions