如何使用boto在VPC中启动实例?

时间:2015-09-29 07:52:01

标签: python amazon-web-services boto

我需要从AMI启动一个实例..而且..我需要启动具有与创建AMI的原始实例相同属性的新实例。即,相同的VPC ID,相同的key_name,相同_region等等

我的代码接受的是:

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

这不起作用,因为它说::

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>

帮我!!!!!!!

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题..你需要做的就是这个

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)

这将解决您的问题。问候\ m /

答案 1 :(得分:1)

所以基本上,Nishant的回答会起作用,但是如果有多个SG的话,你就不会得到精确的副本:

为此请参考以下代码:

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

参考:我在这个帖子上回答了同样的问题 How to Launch a exact same replica of a EC2 instance in VPC from the AMI of a previous EC2 instance