Skip to content

Instantly share code, notes, and snippets.

@phucnh
Forked from thibautsacreste/aws.sg.unused
Created June 6, 2019 15:09
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 phucnh/92a29a04cbeb3d555750530e45bdf7ad to your computer and use it in GitHub Desktop.
Save phucnh/92a29a04cbeb3d555750530e45bdf7ad to your computer and use it in GitHub Desktop.
Bash: list unused AWS security groups
#!/usr/bin/env bash
# lists all unused AWS security groups.
# a group is considered unused if it's not attached to any network interface.
# requires aws-cli and jq.
# all groups
aws ec2 describe-security-groups \
| jq --raw-output '.SecurityGroups[] | [.GroupName, .GroupId] | @tsv' \
| sort > /tmp/sg.all
# groups in use
aws ec2 describe-network-interfaces \
| jq --raw-output '.NetworkInterfaces[].Groups[] | [.GroupName, .GroupId] | @tsv' \
| sort \
| uniq > /tmp/sg.in.use
diff /tmp/sg.all /tmp/sg.in.use |grep "<" |cut -d ' ' -f2-3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment