はじめに
サーバー(EC2 インスタンス)へのアクセス制御は Security Group の IP アドレスで制御しています。
Security Group に登録している IP アドレスを確認したときに試した aws cli のコマンドを備忘録として残しておきます。
security group に登録している IP アドレスを列挙する
ec2 describe-security-groups
を使用して、table 形式で出力します。
SG_ID="sg-XXXXXXXXXXXXXXXXX"
aws ec2 describe-security-groups \
--group-ids $SG_ID \
--query 'SecurityGroups[*].IpPermissions[*].{
FromPort:FromPort,
Ip:IpRanges[*]
}' \
--output table
security group に登録している IP アドレスを確認する
#!/bin/bash
set -euo pipefail
IP="7.7.7.7/32"
SG_ID1="sg-XXXXXXXXXXXXXXXXX"
SG_ID2="sg-YYYYYYYYYYYYYYYYY"
IPs=$(aws ec2 describe-security-groups \
--group-ids $SG_ID1 \
--query "SecurityGroups[*].IpPermissions[*]" --output text)
read -r -p "一覧を表示しますか? (y/N): " yn
case "$yn" in
[yY]*) echo $IPs | xargs -n1;;
*) ;;
esac
read -r -p $IP"の検索を開始しますか? (y/N): " yn
case "$yn" in
[yY]*) echo "処理を開始します.";;
*) echo "処理を終了します." ; exit ;;
esac
if [[ `echo $IPs | grep $IP` ]] ; then
echo 'ありました'
exit
fi