Created
January 11, 2019 12:36
-
-
Save kasunbg/776596dbcb76c9c0d70f594ac62f55ab to your computer and use it in GitHub Desktop.
Delete unused AWS security groups
This file contains hidden or 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
comm -23 <(aws ec2 describe-security-groups --query 'SecurityGroups[*].GroupId' --output text | tr '\t' '\n'| sort) \ | |
<(aws ec2 describe-instances --query 'Reservations[*].Instances[*].SecurityGroups[*].GroupId' --output text | tr '\t' '\n' | sort | uniq) \ | |
| tee -a unused-security-groups-in-ec2.txt | |
for x in `cat unused-security-groups-in-ec2.txt`; do echo 'deleting sg: $x' ; aws ec2 delete-security-group --group-id $x; done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WARNING
If you have AWS ECS services with AWSPVC or Fargate launch type and they use the security group, there is no protection, the security group will be removed, leaving the services broken. Unless the services is up and running.