Skip to content

Instantly share code, notes, and snippets.

@sztamas
Created March 19, 2019 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sztamas/ccd2d8e1a6c7f3ad4fed8c8f7a595f1d to your computer and use it in GitHub Desktop.
Save sztamas/ccd2d8e1a6c7f3ad4fed8c8f7a595f1d to your computer and use it in GitHub Desktop.
#!/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