Last active
October 7, 2022 09:37
-
-
Save cauealvesbraz/3d81bccf5c79e9e825aba5ee03a8a996 to your computer and use it in GitHub Desktop.
AWS EC2: Find unused security groups by region
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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