AWS CLI:セキュリティグループの作成・表示・削除コマンド
検証環境や前提について
- 検証日: 2021/04/04
PC環境:
- Windows10 Home Ver2004
- AWS CLI Ver2.1.34
- profile default: 読み取り専用
- profile aws_RW: 書き込み権限あり
- PowerShell Ver7.1.3
- AWS CLI Ver2.1.34
- Windows10 Home Ver2004
本コマンドはPowerShellで検証しています。
- 作成や削除をする場合「--profile aws_RW」オプションを付与します。
- 表示の場合は「profile default」のためprofileオプションを使用しません。
- コマンド内のパラメータやIDは検証で作成したものをそのまま使用しています。自身の環境に書き換えて使用してください。
- 個人で検証しているため実行結果に責任は持てません。必ずご自身でも検証してから使用してください。
目次
関連記事
- AWS CLI:VPCの作成・表示・削除コマンド
- AWS CLI:サブネットの作成・表示・削除コマンド
- AWS CLI:ネットワークACLの作成・表示・削除コマンド
- AWS CLI:セキュリティグループの作成・表示・削除コマンド
- AWS CLI:マネージドプレフィックスリストの作成・表示・削除コマンド
1. セキュリティグループの作成
参考URL
Amazon EC2 のセキュリティグループの作成、設定、および削除
1.1. 作成するセキュリティグループ構成
セキュリティグループの作成
項目 CLIオプション 設定値 セキュリティグループ名 --group-name Test-Sgr-Web01 説明 --description "for WebServer" VPC ID --vpc-id vpc-09ff13fba3e354e62 タグ(Name) --tag-specifications Key: Name, Value: Test-Sgr-Web01 タグ(System) --tag-specifications Key: System, Value: Test
2. セキュリティグループへルールの追加
インバウンド(ingress)
タイプ プロトコル ポート範囲 送信元(ソース) 説明 HTTP(80) tcp 80 <自己PCのGlobal-IP>/32 "Allow HTTP from YourPC." HTTP(443) tcp 443 <自己PCのGlobal-IP>/32 "Allow HTTPS from YourPC." SSH(22) tcp 22 <自己PCのGlobal-IP>/32 "Allow SSH from YourPC." telnet(23) tcp 23 <自己PCのGlobal-IP>/32 "Allow TELNET from YourPC." RDP(3389) tcp 3389 <自己PCのGlobal-IP>/32 "Allow RDP from YourPC." カスタムTCP tcp 1000-1010 10.1.0.0/16 "Port-Range Test from VPC" すべてのICMP-IPv4 icmp すべて <自己PCのGlobal-IP>/32 "Allow ICMPipv4 from YourPC." すべてのトラフィック all all Test-Mpl-Vpc01 Allow All from Prefix-List すべてのトラフィック all all Test-Sgr-Web01 Allow All from SecurityGroup ※自分のPCからの通信かつ
HTTP, HTTPS, SSH, telnet, RDP, ICMP
のみを受け付けるようにします。
※<自己PCのGlobal-IP>/32
は自分のPCが使用しているグローバルIPです。
調べ方の例として、安全な確認くん へ接続し、現在接続している場所(現IP)
に表示されるIPアドレスを使用します。
アウトバウンド(egress)
タイプ プロトコル ポート範囲 送信先 説明 すべてのトラフィック すべて すべて 0.0.0.0/0 Deny ※ アウトバウンドは全許可とするため、Defaultで作成されているルールを使用し、追加は行いません。
3. パラメータの参考
タイプ
プロトコル[--protocol
] ポート範囲[--port
]
1.2. セキュリティグループの作成コマンド
AWS CLI v2 リファレンスガイド: create-security-group
aws ec2 create-security-group ` --profile aws_RW ` --group-name Test-Sgr-Web01 ` --description "for WebServer" ` --vpc-id vpc-09ff13fba3e354e62 ` --tag-specifications "ResourceType=security-group, ` Tags=[{Key=Name,Value=Test-Sgr-Web01}, ` {Key=System,Value=Test}]"
1.3. セキュリティグループにルールを追加するコマンド
ルールを追加するセキュリティグループのIDを調べる。
aws ec2 describe-security-groups ` --filters "Name=tag:Name,Values=Test-Sgr-Web01" ` --query "SecurityGroups[].GroupId" ` --output text > sg-0cf49c5291c8e1f3f
ルール追加コマンドは、入力時やExcelの一覧からコマンドを作るときに途中改行が無い方が楽だったので横長のコマンドにします。
仮で「<自己PCのGlobal-IP>」は「1.1.1.1/32」としています。
インバウンド(ingress)
AWS CLI v2 リファレンスガイド: authorize-security-group-ingress
aws ec2 authorize-security-group-ingress --profile aws_RW --group-id sg-0cf49c5291c8e1f3f --ip-permissions IpProtocol=tcp,FromPort=80,ToPort=80,IpRanges='[{CidrIp=1.1.1.1/32,Description="Allow HTTP from YourPC."}]' aws ec2 authorize-security-group-ingress --profile aws_RW --group-id sg-0cf49c5291c8e1f3f --ip-permissions IpProtocol=tcp,FromPort=443,ToPort=443,IpRanges='[{CidrIp=1.1.1.1/32,Description="Allow HTTPS from YourPC."}]' aws ec2 authorize-security-group-ingress --profile aws_RW --group-id sg-0cf49c5291c8e1f3f --ip-permissions IpProtocol=tcp,FromPort=22,ToPort=22,IpRanges='[{CidrIp=1.1.1.1/32,Description="Allow SSH from YourPC."}]' aws ec2 authorize-security-group-ingress --profile aws_RW --group-id sg-0cf49c5291c8e1f3f --ip-permissions IpProtocol=tcp,FromPort=23,ToPort=23,IpRanges='[{CidrIp=1.1.1.1/32,Description="Allow TELNET from YourPC."}]' aws ec2 authorize-security-group-ingress --profile aws_RW --group-id sg-0cf49c5291c8e1f3f --ip-permissions IpProtocol=tcp,FromPort=3389,ToPort=3389,IpRanges='[{CidrIp=1.1.1.1/32,Description="Allow RDP from YourPC."}]' aws ec2 authorize-security-group-ingress --profile aws_RW --group-id sg-0cf49c5291c8e1f3f --ip-permissions IpProtocol=tcp,FromPort=1000,ToPort=1010,IpRanges='[{CidrIp=10.1.0.0/16,Description="Port-Range Test from VPC"}]' aws ec2 authorize-security-group-ingress --profile aws_RW --group-id sg-0cf49c5291c8e1f3f --ip-permissions IpProtocol=icmp,FromPort=-1,ToPort=-1,IpRanges='[{CidrIp=1.1.1.1/32,Description="Allow ICMPipv4 from YourPC."}]' aws ec2 authorize-security-group-ingress --profile aws_RW --group-id sg-0cf49c5291c8e1f3f --ip-permissions IpProtocol=-1,PrefixListIds='[{PrefixListId=pl-00cb691257f93ff0f,Description="Allow All from Prefix-List"}]' aws ec2 authorize-security-group-ingress --profile aws_RW --group-id sg-0cf49c5291c8e1f3f --ip-permissions IpProtocol=-1,UserIdGroupPairs='[{GroupId=sg-0cf49c5291c8e1f3f,Description="Allow All from SecurityGroup"}]'
アウトバウンド(egress)
AWS CLI v2 リファレンスガイド: authorize-security-group-egress
※ アウトバウンドは全許可とするため、Defaultで作成されているルールを使用し、追加は行いません。
1.4. セキュリティグループ表示コマンド
AWS CLI v2 リファレンスガイド: describe-security-groups
# すべてのセキュリティグループを表示する aws ec2 describe-security-groups # テキストで表示するとルールが見やすい aws ec2 describe-security-groups --output text # 特定セキュリティグループ ID のセキュリティグループのみを表示する aws ec2 describe-security-groups --group-ids sg-0cf49c5291c8e1f3f # Nameタグ==Test-Sgr-Web01 のセキュリティグループのみを表示する aws ec2 describe-security-groups --filters "Name=tag:Name,Values=Test-Sgr-Web01" # 特定VPCのセキュリティグループのみを表示する aws ec2 describe-security-groups --filters "Name=vpc-id,Values=vpc-09ff13fba3e354e62"
1.5. セキュリティグループ削除コマンド
AWS CLI v2 リファレンスガイド: delete-security-group
削除するセキュリティグループのIDを調べる。
aws ec2 describe-security-groups ` --filters "Name=tag:Name,Values=Test-Sgr-Web01" ` --query "SecurityGroups[].GroupId" ` --output text > sg-0cf49c5291c8e1f3f
セキュリティグループを削除するコマンド。
※削除前にセキュリティグループを未使用状態にする必要があります(EC2インスタンス等から外す)。
aws ec2 delete-security-group ` --profile aws_RW ` --group-id sg-0cf49c5291c8e1f3f
1.6. セキュリティグループのルールを削除するコマンド
- ルールを追加した時のコマンドを流用し、「authorize-security-group-ingress」を「revoke-security-group-ingress」に変更するだけでよい。
- 「Description部分」は書いても書かなくても良いが、「Protocol」「Port」「Cidr」は必要。
# 記載例 aws ec2 revoke-security-group-ingress --profile aws_RW --group-id sg-0cf49c5291c8e1f3f --ip-permissions IpProtocol=tcp,FromPort=80,ToPort=80,IpRanges='[{CidrIp=1.1.1.1/32,Description="Allow HTTP from YourPC."}]' aws ec2 revoke-security-group-ingress --profile aws_RW --group-id sg-0cf49c5291c8e1f3f --ip-permissions IpProtocol=tcp,FromPort=443,ToPort=443,IpRanges='[{CidrIp=1.1.1.1/32}]'