Skip to content

Instantly share code, notes, and snippets.

@kjoconnor
Created February 7, 2014 00:53
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/8855568 to your computer and use it in GitHub Desktop.
Save kjoconnor/8855568 to your computer and use it in GitHub Desktop.
Find security group grants in ElastiCache and EC2
from boto.ec2 import connect_to_region
from boto.elasticache import connect_to_region as elasticache_region
target = 'sg-name'
ec2 = connect_to_region('us-east-1')
ec = elasticache_region('us-east-1')
sgs = ec2.get_all_security_groups()
for sg in sgs:
for rule in sg.rules:
for grant in rule.grants:
if target == getattr(grant, 'cidr_ip', None) \
or target == getattr(grant, 'groupName', None):
print 'target found in sg {sg} as {rule}'\
.format(sg=sg.name, rule=rule)
ec_sgs = ec.describe_cache_security_groups()
for ec_sg in ec_sgs['DescribeCacheSecurityGroupsResponse']['DescribeCacheSecurityGroupsResult']['CacheSecurityGroups']:
if len(ec_sg['EC2SecurityGroups']) > 0:
for group in ec_sg['EC2SecurityGroups']:
if group['EC2SecurityGroupName'] == target:
print 'target found in ec sg {sg}'\
.format(sg=ec_sg['CacheSecurityGroupName'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment