【AWS CLI】セキュリティグループのルールの一覧が欲しい!

記事タイトルとURLをコピーする

こんにちは。AWS CLIが好きな福島です。

今回はタイトル通り、セキュリティグループのルールの一覧が欲しい際に実行するコマンドをご紹介いたします。

利用するコマンド,サブコマンド

まず、AWS CLIの構造は以下の通りです。

aws <command> <subcommand> [options and parameters]

上記を前提に今回使う <command>,<subcommand>は、以下の通りです。

<command>

ec2  

<subcommand>

describe-security-group-rules

結論

  • 実行コマンド
aws ec2 describe-security-group-rules \
--query "SecurityGroupRules[].\
[SecurityGroupRuleId,\
GroupId,\
IsEgress,\
IpProtocol,\
FromPort,\
ToPort,\
CidrIpv4,\
ReferencedGroupInfo.GroupId,\
ReferencedGroupInfo.PeeringStatus,\
ReferencedGroupInfo.VpcId,\
ReferencedGroupInfo.VpcPeeringConnectionId]" \
--output text
  • 実行結果例
sgr-0765bfe2b1de9fda5   sg-04f5f74f10c1cfdbe    True    -1      -1      -1      0.0.0.0/0       None    None    None    None
sgr-064657fd126f27270   sg-0cb14c5fd84188e79    True    tcp     443     443     0.0.0.0/0       None    None    None    None
sgr-03027c5f6a69e7214   sg-0b6af0bb7eca95d40    False   tcp     0       0       None    sg-00c78fbf089ace674    active  vpc-0fee138d3e0deef81   pcx-04b2a77d2fcd82dbb
sgr-05ae5f529d2c41458   sg-0c89079bada49446b    False   tcp     443     443     xx.xx.xx.xx/32        None    None    None    None

ヘッダーが欲しい

上記では、ヘッダーがなくよくわからないと思うため、以下のコマンドでヘッダーを付与できます。

  • 実行コマンド
echo "SecurityGroupRuleId GroupId IsEgress IpProtocol FromPort ToPort CidrIpv4 SourceGroupId PeeringStatus VpcId VpcPeeringConnectionId" > /tmp/awscli.tmp ;\
aws ec2 describe-security-group-rules \
--query "SecurityGroupRules[].\
[SecurityGroupRuleId,\
GroupId,\
IsEgress,\
IpProtocol,\
FromPort,\
ToPort,\
CidrIpv4,\
ReferencedGroupInfo.GroupId,\
ReferencedGroupInfo.PeeringStatus,\
ReferencedGroupInfo.VpcId,\
ReferencedGroupInfo.VpcPeeringConnectionId]" \
--output text >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp ;\
rm /tmp/awscli.tmp
  • 実行結果
SecurityGroupRuleId    GroupId               IsEgress  IpProtocol  FromPort  ToPort  CidrIpv4            SourceGroupId         PeeringStatus  VpcId                  VpcPeeringConnectionId
sgr-0765bfe2b1de9fda5  sg-04f5f74f10c1cfdbe  True      -1          -1        -1      0.0.0.0/0           None                  None           None                   None
sgr-064657fd126f27270  sg-0cb14c5fd84188e79  True      tcp         443       443     0.0.0.0/0           None                  None           None                   None
sgr-03027c5f6a69e7214  sg-0b6af0bb7eca95d40  False     tcp         0         0       None                sg-00c78fbf089ace674  active         vpc-0fee138d3e0deef81  pcx-04b2a77d2fcd82dbb
sgr-05ae5f529d2c41458  sg-0c89079bada49446b  False     tcp         443       443     xx.xxx.xxx.xx/32    None                  None           None                   None

各項目について

  • SecurityGroupRuleId
    セキュリティグループのルールID
  • GroupId
    セキュリティグループのID
  • IsEgress
    True:アウトバウンドの通信設定
    Flase:インバウンドの通信設定
  • IpProtocol
    プロトコル
  • FromPort
    Fromのポート番号
  • ToPort
    Toのポート番号
  • CidrIpv4
    通信許可しているFromのCIDR
  • SourceGroupId
    通信許可しているセキュリティグループのID
  • PeeringStatus
    VPCピアリングのステータス
  • VpcId
    VPCID
  • VpcPeeringConnectionId
    VPCピアリングのID

補足

VPCピアリングの情報が記載さている場合は、SourceGroupIdに表示されたセキュリティグループが GroupIdに表示されたセキュリティグループと異なるVPCに存在するということを指します。

終わりに

どなたかのお役に立てれば幸いです。

福島 和弥 (記事一覧)

2019/10 入社

AWS CLIが好きです。

AWS資格12冠。2023 Japan AWS Partner Ambassador/APN ALL AWS Certifications Engineer。