使われていないSecurity Groupsを抽出するだけの簡単なスクリプトを書きました。使っていないものを削除して整理しましょう!
スクリプトは各サービスのインスタンスなどにアタッチされていないSecurity GroupのIDを抽出します。
環境変数やif文は各自追加したり変更すればよいです。
jsonでやる場合は抽出方法は変わります。
sg_check.sh
#!/bin/bash
export AWS_DEFAULT_REGION=ap-northeast-1
export AWS_DEFAULT_OUTPUT=text
aws ec2 describe-security-groups | grep SECURITYGROUPS | awk -F "\t" '{print $3}' | sort -u | while read line
do
aws ec2 describe-instances | grep $line > /dev/null
if [ $? -eq 0 ]
then
continue
fi
aws elb describe-load-balancers | grep $line > /dev/null
if [ $? -eq 0 ]
then
continue
fi
aws ec2 describe-network-interfaces | grep $line > /dev/null
if [ $? -eq 0 ]
then
continue
fi
aws rds describe-db-instances | grep $line > /dev/null
if [ $? -eq 0 ]
then
continue
fi
aws autoscaling describe-launch-configurations | grep $line > /dev/null
if [ $? -eq 0 ]
then
continue
fi
echo $line
done
descriptionをちゃんと書いてSecurityGroupは整理したいですね。。。
参考
https://qiita.com/rmanzoku/items/f18bee522d90f9bd2b78
https://qiita.com/wildeagle/items/f01f316e791115c6d887