【问题标题】:Describe ELB AWS security group rules by SG name按 SG 名称描述 ELB AWS 安全组规则
【发布时间】:2018-03-23 20:59:57
【问题描述】:

我正在尝试使用 Python boto3 来描述 ELB SecurityGroup 并列出它的所有规则。

但是,有一个错误提示我没有使用默认 VPC。

我尝试过滤并指定非默认 VPC vpc-67890,但没有帮助:

client = boto3.client('ec2')
response = client.describe_security_groups(
     ...: Filters = [
     ...: {
     ...: 'Name': 'vpc-id',
     ...: 'Values': [
     ...: 'vpc-67890']},
     ...: ],
     ...: GroupNames=['SG_NAME'])

ClientError: An error occurred (InvalidGroup.NotFound) when calling the DescribeSecurityGroups operation: The security group 'SG_NAME' does not exist in default VPC 'vpc-12345'

我尝试使用 boto3 资源,但它是同样的问题,它不返回所有 SecurityGroups,只是空响应:

ec2 = boto3.resource('ec2')
vpc = ec2.Vpc('vpc-67890')
all_security_groups = vpc.security_groups.all()
specific_security_group = vpc.security_groups.filter(GroupNames=['SG_NAME'])

for i in all_security_groups:
    print i

(没有回应)

当我查询我过滤的特定组时,它会引发错误:

for i in specific_security_group:
    print i

ClientError: An error occurred (InvalidGroup.NotFound) when calling the DescribeSecurityGroups operation: The security group 'SG_NAME' does not exist in default VPC 'vpc-12345'

我知道如果使用非默认 VPC,它需要 GroupID 而不是 GroupName,但问题是 describe_elb API 仅返回 GroupName。

尝试通过 AWS Cli 描述 SecurityGroup 时也会发生同样的情况:

$ aws ec2 describe-security-groups --group-names SG_NAME

An error occurred (InvalidGroup.NotFound) when calling the DescribeSecurityGroups operation: The security group 'SG_NAME' does not exist in default VPC 'vpc-12345'

有人遇到同样的问题吗?

提前致谢。

【问题讨论】:

    标签: python amazon-web-services boto3


    【解决方案1】:

    是的,您不能在默认 VPC 之外使用 GroupName 参数。它有点隐藏在 API 文档中:describing the GroupName parameter 它说:

    [EC2-Classic and default VPC only]

    您需要将“按组查询”部分留空,而是使用过滤器中的组,如下所示:

    filters = [dict(Name='group-name', Values=['SG_NAME']), 
               dict(Name='vpc-id', Values=['vpc-67890'])]
    client.describe_security_groups(Filters=filters)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-15
      • 2019-07-09
      • 2020-02-06
      • 1970-01-01
      • 2019-12-14
      • 2014-04-06
      • 1970-01-01
      • 2018-08-25
      相关资源
      最近更新 更多