Skip to content

Instantly share code, notes, and snippets.

@hltbra
Last active November 15, 2018 12:04
Show Gist options
  • Save hltbra/11147385 to your computer and use it in GitHub Desktop.
Save hltbra/11147385 to your computer and use it in GitHub Desktop.
Get AWS ingress and egress rules for a given security group
import boto
ec2 = boto.connect_ec2()
groups = ec2.get_all_security_groups()
def get_groups_that_have_ingress_rules_to(groups, group_id):
result = []
for group in groups:
for rule in group.rules:
for grant in rule.grants:
if grant.group_id == group_id:
result.append(group)
return result
def get_groups_that_have_egress_rules_to(groups, group_id):
result = []
for group in groups:
for rule in group.rules_egress:
for grant in rule.grants:
if grant.group_id == group_id:
result.append(group)
return result
SG_TO_FIND_INGRESS = 'INGRESS HERE'
print('-'*10)
print("Groups that have inbound from {}".format(SG_TO_FIND_INGRESS))
for group in get_groups_that_have_ingress_rules_to(groups, SG_TO_FIND_INGRESS):
print group.name, group.id
print('-'*10)
SG_TO_FIND_EGRESS = 'EGRESS GROUP HERE'
print('-'*10)
print("Groups that have outbound to {}".format(SG_TO_FIND_INGRESS))
for group in get_groups_that_have_egress_rules_to(groups, SG_TO_FIND_EGRESS):
print group.name, group.id
print('-'*10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment