Python Boto:如何指定子网ID和安全组?
我正在尝试使用boto来启动一个实例。这个实例需要在我VPC中的一个特定子网里启动,同时还要在我VPC中的一个特定安全组里。
下面的代码成功地在我VPC的正确子网上启动了一个实例:
conn.run_instances(
image_id=base_ami,
key_name=bakery_key,
subnet_id=bakery_subnet)
但是,下面的代码给我带来了一个错误:
reservation = conn.run_instances(
image_id=base_ami,
key_name=bakery_key,
security_groups=['TheNameOfMySecurityGroup'],
subnet_id=bakery_subnet)
这是我收到的错误信息。当我使用子网的ID而不是实际的子网名称时,我也会遇到同样的错误:
Traceback (most recent call last):
File "./botobakery.py", line 24, in <module>
subnet_id=bakery_subnet)
File "/usr/lib/python2.6/site-packages/boto/ec2/connection.py", line 935, in run_instances
verb='POST')
File "/usr/lib/python2.6/site-packages/boto/connection.py", line 1177, in get_object
raise self.ResponseError(response.status, response.reason, body)
boto.exception.EC2ResponseError: EC2ResponseError: 400 Bad Request
<?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>InvalidParameterCombination</Code><Message>The parameter groupName cannot be used with the parameter subnet</Message></Error></Errors> <RequestID>c8a6b824-4ab3-41d2-9633-9830c167d2d6</RequestID></Response>
如果有人知道怎么把我的实例同时启动到特定的子网和特定的安全组里,我将非常感激!
1 个回答
10
因为你是在一个虚拟私有云(VPC)中启动,所以你必须用安全组的ID来指定它,而不是用名字。在“经典”EC2中,名字是可以用的。所以,如果你要用的安全组的ID是 sg-12345678
,你可以使用这样的命令:
reservation = conn.run_instances(
image_id=base_ami,
key_name=bakery_key,
security_group_ids=['sg-12345678'],
subnet_id=bakery_subnet)