Skip to content

Instantly share code, notes, and snippets.

@amcginlay
Last active July 27, 2020 08:36
Show Gist options
  • Save amcginlay/99299b1255896ae7c818e698d8a2eba3 to your computer and use it in GitHub Desktop.
Save amcginlay/99299b1255896ae7c818e698d8a2eba3 to your computer and use it in GitHub Desktop.
A script to list any security groups which can be safely deleted (unless it's named "default")
#!/bin/bash
for region in $(aws ec2 describe-regions --query "Regions[*].[RegionName]" --output text); do
echo "--- ${region} ---"
aws ec2 describe-vpcs --region ${region} --query "Vpcs[].[VpcId,Tags[?Key=='Name'].Value | [0]]" --output text | while read vpcdata; do
set $vpcdata; vpcid=${1}; vpcname=${2}
aws ec2 describe-security-groups --region ${region} --filters "Name=vpc-id,Values=${vpcid}" --query "SecurityGroups[].[GroupId,GroupName]" --output text | while read sgdata; do
set $sgdata; sgid=${1}; sgname=${2}
if [ "${sgname}" != "default" ]; then
usagecount=$(aws ec2 describe-network-interfaces --region ${region} --filters "Name=group-id,Values=${sgid}" --query "NetworkInterfaces[] | length(@)" --output text)
if [ "${usagecount}" == "0" ]; then
echo "${region} ${vpcname} (${vpcid}) ${sgname} (${sgid})"
fi
fi
done
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment