Skip to content

Instantly share code, notes, and snippets.

@tomislacker
Last active July 25, 2018 16:33
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 tomislacker/d650bbdefd25d5c12933eea23f073393 to your computer and use it in GitHub Desktop.
Save tomislacker/d650bbdefd25d5c12933eea23f073393 to your computer and use it in GitHub Desktop.
Find Unused Security Groups.sh
#!/bin/bash
find_all_groups ()
{
aws ec2 describe-security-groups \
--query 'SecurityGroups[*].GroupId' \
--output text \
| tr '\t' '\n' \
| sort -u
}
find_inuse_groups ()
{
aws ec2 describe-network-interfaces \
--query 'NetworkInterfaces[*].Groups[*].GroupId' \
--output text \
| tr '\t' '\n' \
| sort -u
}
find_unused_groups ()
{
comm -23 <(find_all_groups) <(find_inuse_groups)
}
aws ec2 describe-security-groups \
--group-ids $(find_unused_groups) \
--query 'SecurityGroups[*].{ID:GroupId,Name:GroupName,VPC:VpcId,Desc:Description}' \
--output table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment