简体   繁体   English

使用 boto 或 bash(使用 AWS-CLI)在 ec2、rds、LB 中查找未使用的安全组

[英]Finding unused Security Groups in ec2,rds,LB using boto or bash(using AWS-CLI)

I'm trying to find a way to determine orphan security groups so I can clean up and get rid of them.我试图找到一种方法来确定孤立的安全组,以便我可以清理并摆脱它们。 Does anyone know a way to discover unused security groups.有谁知道一种发现未使用的安全组的方法。 It should check EC2,RDS,LB,VPC etc它应该检查 EC2、RDS、LB、VPC 等

I am able to find the unused SG only for EC2 Security groups.我只能为 EC2 安全组找到未使用的 SG。 I tried to find rds Security groups.我试图找到 rds 安全组。

#!/usr/bin/python3
import boto3

session = boto3.session.Session(profile_name='xxx-xxx-xxx')


sgs = session.client('rds', region_name='us-east-1').describe_db_security_groups()
for sg in sgs:
    print (sg)

Security Groups are associated with Elastic Network Interfaces (ENIs).安全组与弹性网络接口(ENI) 相关联。

Therefore, you could iterate through all ENIs and retrieve the attached Security Groups.因此,您可以遍历所有 ENI 并检索附加的安全组。 Then, compare the list against security groups to identify which ones are not in use.然后,将列表与安全组进行比较以确定哪些未在使用。

Alternatively, I wonder if you can just attempt to delete them?或者,我想知道您是否可以尝试删除它们? If they are "in use", the deletion should probably fail.如果它们“正在使用”,则删除可能会失败。 (But test it first!) (但先测试一下!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM