【发布时间】: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