Skip to content

awscliでSG作成とルール追加

awscliでセキュリティグループを新規追加して、ルールを投入します。
事前に対象のVPC_IDを控えておきましょう。

セキィリティグループ作成

以下のコマンドで新規SGを作成できます。実行するとセキュリティグループIDが出てくるので、その後のコマンド実行時にそれを指定します。

$ aws ec2 create-security-group \
      --group-name http-sg \
      --description "http-sg" --vpc-id vpc-*************

作成したセキュリティグループにタグをつける

作成したSGにタグをつけておきましょう。

$ aws ec2 create-tags \
      --resources sg-************* \
      --tags Key=Name,Value=http-sg

作成したセキュリティグループにルール追加

ルールを追加します。 Description で任意のコメントを付与できます。

$ aws ec2 authorize-security-group-ingress \
    --group-id sg-***************** \
    --ip-permissions \
    IpProtocol=tcp,FromPort=80,ToPort=80,IpRanges='[{CidrIp=172.16.0.10/32,Description="ここにコメントを"}]' \
    IpProtocol=tcp,FromPort=80,ToPort=80,IpRanges='[{CidrIp=172.16.0.15/32,Description="ここにコメントを"}]' \
    IpProtocol=tcp,FromPort=80,ToPort=80,IpRanges='[{CidrIp=172.16.0.19/32,Description="ここにコメントを"}]' \
    IpProtocol=tcp,FromPort=80,ToPort=80,IpRanges='[{CidrIp=172.16.0.220/32,Description="ここにコメントを"}]' \
    IpProtocol=tcp,FromPort=80,ToPort=80,IpRanges='[{CidrIp=172.16.0.225/32,Description="ここにコメントを"}]' \
    IpProtocol=tcp,FromPort=80,ToPort=80,IpRanges='[{CidrIp=172.16.0.230/32,Description="ここにコメントを"}]' \ 
    IpProtocol=tcp,FromPort=80,ToPort=80,IpRanges='[{CidrIp=192.168.0.0/24,Description="ここにコメントを"}]'

確認

$ aws ec2 describe-security-groups --group-id sg-*****************