使用python boto检索安全组但设置vpc id

时间:2023-01-21 15:48:56

i am trying to play around with python boto and i run the below example(below is the official doc for this). I know for a fact that an SG called "Pub_HDP_SG" exist but when i try to run my command by passing in a parameter called groupnames i get below error. I get the fact that this SG does not exist in my default VPC, so how do i set my VPC to a specific VPC ??

我正在尝试使用python boto和我运行以下示例(下面是这个的官方文档)。我知道一个名为“Pub_HDP_SG”的SG存在,但当我尝试通过传入一个名为groupnames的参数来运行我的命令时,我得到以下错误。我知道这个SG在我的默认VPC中不存在,所以我如何将我的VPC设置为特定的VPC?

http://boto.readthedocs.org/en/latest/ref/ec2.html?highlight=get_all_security_groups#boto.ec2.connection.EC2Connection.get_all_security_groups

http://boto.readthedocs.org/en/latest/ref/ec2.html?highlight=get_all_security_groups#boto.ec2.connection.EC2Connection.get_all_security_groups

>>> import boto
>>> ec2 = boto.connect_ec2()
>>> sg = ec2.get_all_security_groups()
>>> print sg
[SecurityGroup:default, SecurityGroup:Pub_HDP_SG, SecurityGroup:RDP Rule - open everyone , SecurityGroup:us-east-open-all, SecurityGroup:wordpress-app-SG, SecurityGroup:default, SecurityGroup:AWS-AMI-SG, SecurityGroup:launch-wizard-2]
>>>
>>> sgn = "Pub_HDP_SG"
>>>
>>> sg = ec2.get_all_security_groups(groupnames=[sgn])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/site-packages/boto/ec2/connection.py", line 2968, in get_all_security_groups
    [('item', SecurityGroup)], verb='POST')
  File "/usr/lib/python2.6/site-packages/boto/connection.py", line 1186, in get_list
    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>InvalidGroup.NotFound</Code><Message>The security group 'Pub_HDP_SG' does not exist in default VPC 'vpc-b3bf61d6'</Message></Error></Errors><RequestID>c708a4cd-0bc9-4761-a5a6-556b1c68ecdb</RequestID></Response>
>>>

1 个解决方案

#1


6  

This is a quirky aspect of the EC2 API. The group names parameter will only work with security groups in the default VPC (or EC2 Classic). If you want to find groups in any VPC by name, use the filters parameter instead.

这是EC2 API的一个古怪的方面。 group names参数仅适用于默认VPC(或EC2 Classic)中的安全组。如果要按名称在任何VPC中查找组,请改用filters参数。

groups = ec2.get_all_security_groups(filters={'group-name': [sgn]})

This should return the list but note that it still returns a list of groups, not just the scalar group object.

这应该返回列表,但请注意它仍然返回一个组列表,而不仅仅是标量组对象。

#1


6  

This is a quirky aspect of the EC2 API. The group names parameter will only work with security groups in the default VPC (or EC2 Classic). If you want to find groups in any VPC by name, use the filters parameter instead.

这是EC2 API的一个古怪的方面。 group names参数仅适用于默认VPC(或EC2 Classic)中的安全组。如果要按名称在任何VPC中查找组,请改用filters参数。

groups = ec2.get_all_security_groups(filters={'group-name': [sgn]})

This should return the list but note that it still returns a list of groups, not just the scalar group object.

这应该返回列表,但请注意它仍然返回一个组列表,而不仅仅是标量组对象。