Skip to content

Instantly share code, notes, and snippets.

@kjoconnor
Created May 16, 2014 21:43
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 kjoconnor/14cc1e0b5ca181106da6 to your computer and use it in GitHub Desktop.
Save kjoconnor/14cc1e0b5ca181106da6 to your computer and use it in GitHub Desktop.
Find if a security group is in use anywhere
import sys
from boto.ec2 import connect_to_region
target_sg = sys.argv[1]
ec2 = connect_to_region('us-west-1')
target_sg_object = ec2.get_all_security_groups([target_sg])[0]
print 'EC2 instances with group'
instances = target_sg_object.instances()
for instance in instances:
print '{target_sg} is used by instance {name} ({id}) <{status}>'.format(
target_sg=target_sg,
name=instance.tags.get('Name', 'No Name'),
id=instance.id,
status=instance.state
)
print 'EC2 security groups with group'
security_groups = ec2.get_all_security_groups()
for security_group in security_groups:
for rule in security_group.rules:
for grant in rule.grants:
if getattr(grant, 'groupName', False) == target_sg:
print '{sg} includes {target_sg} in rule {rule}'.format(
sg=security_group,
target_sg=target_sg,
rule=rule
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment