Skip to content

Instantly share code, notes, and snippets.

@wilhelm-murdoch
Last active October 22, 2021 13:07
Show Gist options
  • Save wilhelm-murdoch/b4fbc4cf40aafb912b8ef56828aba8e7 to your computer and use it in GitHub Desktop.
Save wilhelm-murdoch/b4fbc4cf40aafb912b8ef56828aba8e7 to your computer and use it in GitHub Desktop.
List all unused security groups in all AWS regions ...
#!/usr/bin/env bash
set -eo pipefail
[[ -n "${VERBOSE}" ]] && set -x
for region in $(aws ec2 describe-regions | jq -r '.Regions[].RegionName'); do
for sg in $(aws ec2 describe-security-groups --region="${region}" | jq -r '.SecurityGroups[].GroupId'); do
if [[ $(aws ec2 describe-network-interfaces --region="${region}" --filters Name=group-id,Values="${sg}" | jq -r '.NetworkInterfaces | length') -eq 0 ]]; then
echo "${sg} in ${region}"
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment