Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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