Created
March 19, 2019 08:31
-
-
Save sztamas/ccd2d8e1a6c7f3ad4fed8c8f7a595f1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import boto3 | |
ec2 = boto3.resource('ec2') | |
FMT = '{sg.group_id},{sg.group_name},"{sg.description}"' | |
def unused_security_groups(): | |
sgs_in_use = set( | |
sg['GroupId'] | |
for instance in ec2.instances.all() | |
for sg in instance.security_groups) | |
unused_sgs = (sg for sg in ec2.security_groups.all() if sg.id not in sgs_in_use) | |
return unused_sgs | |
def main(): | |
for sg in unused_security_groups(): | |
print(FMT.format(sg=sg)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment