【问题标题】:Unable to delete security group: An error occurred (DependencyViolation) when calling the DeleteSecurityGroup operation无法删除安全组:调用 DeleteSecurityGroup 操作时发生错误 (DependencyViolation)
【发布时间】:2020-04-18 02:41:02
【问题描述】:

我正在尝试删除具有 0 个接口且未在任何其他使用 boto3 的安全组的入口规则中引用的安全组。 但我收到错误: 调用 DeleteSecurityGroup 操作时发生错误(DependencyViolation):资源 sg-XXYYZZ 有一个依赖对象

我想要一个代码列出引用安全组 sg-XXYYZZ 的入口规则,并在删除安全组之前使用 boto3 删除这些入口规则response = ec2.delete_security_group( GroupId=sg, DryRun=False )

我正在使用以下方法列出入口规则:

    for sg in final_del_list:
        response = ec2.describe_security_groups( GroupIds=[sg] )
        print( "\n\n Security Group:", sg )
        for res in response['SecurityGroups']:
            msg = "The Ingress rules are as follows: " if len(res['IpPermissions']) > 0 else "No ingress rules"
            print( msg )
            for ip in res['IpPermissions']:
                print( "IP Protocol: ", ip['IpProtocol'] )
                try:
                    print( "PORT: ", str( ip['FromPort'] ) )
                    for range in ip['IpRanges']:
                        print( "IP Ranges: ", range['CidrIp'] )
                except Exception:
                    print( "No value for ports and ip ranges available for this security group" )

有人可以指导我如何在其入口规则中列出引用 sg-XXYYZZ 的安全性或帮助我解决错误

【问题讨论】:

  • 你有什么问题?
  • @jarmod 我想编写一个代码来列出引用安全组 sg-XXYYZZ 的入口规则并使用 boto3 删除这些入口规则
  • 很遗憾,Stack Overflow 不是代码编写服务,但我们可以帮助您解决代码中的特定问题。您可以从 describe_security_groups (boto3.amazonaws.com/v1/documentation/api/latest/reference/…) 开始,查看与安全组 (IpPermissions) 关联的入站规则,然后查看 IpRanges.CidrIp 以查找以“sg-”开头的源安全组。
  • @jarmod 感谢您的评论。我不是在寻找答案中的代码,正如我所提到的,我正在尝试编写我实际上确实编写过代码的代码,只是我无法列出安全组。我相信我没有提出正确的问题。我会更新我的问题。谢谢

标签: python-3.x amazon-web-services boto3 aws-security-group


【解决方案1】:

看到你的错误:An error occurred (DependencyViolation) when calling the DeleteSecurityGroup operation: resource sg-XXYYZZ has a dependent object

我可以在这里提出一些建议,而不是现在编写代码。 1.您不能安全组,如果它与任何其他实例相关联,即使实例处于停止状态。 2. 这可能看起来是一个孤立的安全组,但它可能与另一个附加到实例的安全组相关联。因此,您需要先编辑该安全组,然后才能删除您指定的安全组。

我写了一个这样的script in github 可能对你有帮助

【讨论】:

    【解决方案2】:

    可以在UserIdGroupPairs下找到安全组

       response = ec2.describe_security_groups( GroupIds=[sg] )
        for res in response['SecurityGroups']:
            if len( res['IpPermissions'] ) > 0:
                for item in res['IpPermissions']:
                    for sg in item['UserIdGroupPairs']:
                        sg_list.append( sg['GroupId'] )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-09
      • 2020-03-11
      • 2019-12-14
      • 2020-01-16
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多