Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cauealvesbraz/3d81bccf5c79e9e825aba5ee03a8a996 to your computer and use it in GitHub Desktop.
Save cauealvesbraz/3d81bccf5c79e9e825aba5ee03a8a996 to your computer and use it in GitHub Desktop.
AWS EC2: Find unused security groups by region
#!/bin/bash
# exit when the command fails
set -o errexit;
# exit when try to use undeclared var
set -o nounset;
region=us-east-1
for securityGroup in $(aws ec2 describe-security-groups --query 'SecurityGroups[*].GroupId' --region $region --output text); do
if [[ `aws ec2 describe-network-interfaces --filters Name=group-id,Values=$securityGroup --region $region --output json | grep -c NetworkInterfaceId` -eq 0 ]]; then
echo $securityGroup;
fi;
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment