presents by IT Consulting 109

AWS – セキュリティグループ情報の取得

概要

セキュリティグループの情報を取得する方法について記載しています。

EC2セキュリティグループ
セキュリティグループは、1 つ以上のインスタンスのトラフィックを制御する仮想ファイアウォールとして機能します。インスタンスを起動するときに、1 つ以上のセキュリティグループとインスタンスを関連付けます。各セキュリティグループに対してルールを追加し、関連付けられたインスタンスに対するトラフィックを許可します。セキュリティグループルールはいつでも変更できます。新しいルールは、セキュリティグループに関連付けられているインスタンスすべてに短時間で自動的に適用されます。インスタンスに到達できるトラフィックを許可するかどうかの判断では、インスタンスに関連付けられているすべてのセキュリティグループのすべてのルールが評価されます。

 

手順

セキュリティグループの情報を取得するには次のコマンドを実行します。

aws ec2 describe-security-groups

TEXT

SECURITYGROUPS  https - connection security gtoup       sg-22e4d55*     sg_https        987654321000 vpc-8db025e*
IPPERMISSIONS   443     tcp     443
IPRANGES        0.0.0.0/0
IPPERMISSIONSEGRESS     -1
IPRANGES        0.0.0.0/0
TAGS    Name    sg-https
SECURITYGROUPS  ssh - connection security gtoup sg-aae7d6d*     sg_ssh  987654321000    vpc-8db025e*
IPPERMISSIONS   22      tcp     22
IPRANGES        0.0.0.0/0
IPPERMISSIONSEGRESS     -1
IPRANGES        0.0.0.0/0
TAGS    Name    sg-ssh
SECURITYGROUPS  default VPC security group      sg-c5f7c5a*     default 987654321000    vpc-8db025e*
IPPERMISSIONSEGRESS     -1
IPRANGES        0.0.0.0/0
TAGS    Name    sg-default
SECURITYGROUPS  default VPC security group      sg-da033eb*     default 987654321000    vpc-9e7f74fa
IPPERMISSIONS   -1
USERIDGROUPPAIRS        sg-da033eb*     987654321000
IPPERMISSIONSEGRESS     -1
IPRANGES        0.0.0.0/0
TAGS    Name
SECURITYGROUPS  http - connection security gtoup        sg-ebe6d79*     sg_http 987654321000    vpc-8db025e*
IPPERMISSIONS   80      tcp     80
IPRANGES        0.0.0.0/0
IPPERMISSIONSEGRESS     -1
IPRANGES        0.0.0.0/0
TAGS    Name    sg-http

TABLE

--------------------------------------------------------
|                DescribeSecurityGroups                |
+------------------------------------------------------+
||                   SecurityGroups                   ||
|+--------------+-------------------------------------+|
||  Description |  https - connection security gtoup  ||
||  GroupId     |  sg-22e4d55*                        ||
||  GroupName   |  sg_https                           ||
||  OwnerId     |  987654321000                       ||
||  VpcId       |  vpc-8db025e*                       ||
|+--------------+-------------------------------------+|
|||                   IpPermissions                  |||
||+---------------------------------+----------------+||
|||  FromPort                       |  443           |||
|||  IpProtocol                     |  tcp           |||
|||  ToPort                         |  443           |||
||+---------------------------------+----------------+||
||||                    IpRanges                    ||||
|||+-------------------------+----------------------+|||
||||  CidrIp                 |  0.0.0.0/0           ||||
||||  Description            |                      ||||
|||+-------------------------+----------------------+|||
|||                IpPermissionsEgress               |||
||+----------------------------------+---------------+||
|||  IpProtocol                      |  -1           |||
||+----------------------------------+---------------+||
||||                    IpRanges                    ||||
|||+--------------------+---------------------------+|||
||||  CidrIp            |  0.0.0.0/0                ||||
|||+--------------------+---------------------------+|||
|||                       Tags                       |||
||+--------------------+-----------------------------+||
|||  Key               |  Name                       |||
|||  Value             |  sg-https                   |||
||+--------------------+-----------------------------+||
||                   SecurityGroups                   ||
|+--------------+-------------------------------------+|
||  Description |  ssh - connection security gtoup    ||
||  GroupId     |  sg-aae7d6d*                        ||
||  GroupName   |  sg_ssh                             ||
||  OwnerId     |  987654321000                       ||
||  VpcId       |  vpc-8db025e*                       ||
|+--------------+-------------------------------------+|
|||                   IpPermissions                  |||
||+---------------------------------+----------------+||
|||  FromPort                       |  22            |||
|||  IpProtocol                     |  tcp           |||
|||  ToPort                         |  22            |||
||+---------------------------------+----------------+||
||||                    IpRanges                    ||||
|||+--------------------+---------------------------+|||
||||  CidrIp            |  0.0.0.0/0                ||||
|||+--------------------+---------------------------+|||
|||                IpPermissionsEgress               |||
||+----------------------------------+---------------+||
|||  IpProtocol                      |  -1           |||
||+----------------------------------+---------------+||
||||                    IpRanges                    ||||
|||+--------------------+---------------------------+|||
||||  CidrIp            |  0.0.0.0/0                ||||
|||+--------------------+---------------------------+|||
|||                       Tags                       |||
||+-----------------------+--------------------------+||
|||  Key                  |  Name                    |||
|||  Value                |  sg-ssh                  |||
||+-----------------------+--------------------------+||
||                   SecurityGroups                   ||
|+----------------+-----------------------------------+|
||  Description   |  default VPC security group       ||
||  GroupId       |  sg-c5f7c5a*                      ||
||  GroupName     |  default                          ||
||  OwnerId       |  987654321000                     ||
||  VpcId         |  vpc-8db025e*                     ||
|+----------------+-----------------------------------+|
|||                IpPermissionsEgress               |||
||+----------------------------------+---------------+||
|||  IpProtocol                      |  -1           |||
||+----------------------------------+---------------+||
||||                    IpRanges                    ||||
|||+--------------------+---------------------------+|||
||||  CidrIp            |  0.0.0.0/0                ||||
|||+--------------------+---------------------------+|||
|||                       Tags                       |||
||+------------------+-------------------------------+||
|||  Key             |  Name                         |||
|||  Value           |  sg-default                   |||
||+------------------+-------------------------------+||
||                   SecurityGroups                   ||
|+----------------+-----------------------------------+|
||  Description   |  default VPC security group       ||
||  GroupId       |  sg-da033eb*                      ||
||  GroupName     |  default                          ||
||  OwnerId       |  987654321000                     ||
||  VpcId         |  vpc-9e7f74fa                     ||
|+----------------+-----------------------------------+|
|||                   IpPermissions                  |||
||+----------------------------------+---------------+||
|||  IpProtocol                      |  -1           |||
||+----------------------------------+---------------+||
||||                UserIdGroupPairs                ||||
|||+------------------+-----------------------------+|||
||||  GroupId         |  sg-da033eb*                ||||
||||  UserId          |  987654321000               ||||
|||+------------------+-----------------------------+|||
|||                IpPermissionsEgress               |||
||+----------------------------------+---------------+||
|||  IpProtocol                      |  -1           |||
||+----------------------------------+---------------+||
||||                    IpRanges                    ||||
|||+--------------------+---------------------------+|||
||||  CidrIp            |  0.0.0.0/0                ||||
|||+--------------------+---------------------------+|||
|||                       Tags                       |||
||+--------------------------+-----------------------+||
|||  Key                     |  Name                 |||
|||  Value                   |                       |||
||+--------------------------+-----------------------+||
||                   SecurityGroups                   ||
|+--------------+-------------------------------------+|
||  Description |  http - connection security gtoup   ||
||  GroupId     |  sg-ebe6d79*                        ||
||  GroupName   |  sg_http                            ||
||  OwnerId     |  987654321000                       ||
||  VpcId       |  vpc-8db025e*                       ||
|+--------------+-------------------------------------+|
|||                   IpPermissions                  |||
||+---------------------------------+----------------+||
|||  FromPort                       |  80            |||
|||  IpProtocol                     |  tcp           |||
|||  ToPort                         |  80            |||
||+---------------------------------+----------------+||
||||                    IpRanges                    ||||
|||+--------------------+---------------------------+|||
||||  CidrIp            |  0.0.0.0/0                ||||
|||+--------------------+---------------------------+|||
|||                IpPermissionsEgress               |||
||+----------------------------------+---------------+||
|||  IpProtocol                      |  -1           |||
||+----------------------------------+---------------+||
||||                    IpRanges                    ||||
|||+--------------------+---------------------------+|||
||||  CidrIp            |  0.0.0.0/0                ||||
|||+--------------------+---------------------------+|||
|||                       Tags                       |||
||+---------------------+----------------------------+||
|||  Key                |  Name                      |||
|||  Value              |  sg-http                   |||
||+---------------------+----------------------------+||

JSON

{
    "SecurityGroups": [
        {
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "PrefixListIds": [],
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "UserIdGroupPairs": [],
                    "Ipv6Ranges": []
                }
            ],
            "Description": "https - connection security gtoup",
            "Tags": [
                {
                    "Value": "sg-https",
                    "Key": "Name"
                }
            ],
            "IpPermissions": [
                {
                    "PrefixListIds": [],
                    "FromPort": 443,
                    "IpRanges": [
                        {
                            "Description": "",
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "ToPort": 443,
                    "IpProtocol": "tcp",
                    "UserIdGroupPairs": [],
                    "Ipv6Ranges": []
                }
            ],
            "GroupName": "sg_https",
            "VpcId": "vpc-8db025e*",
            "OwnerId": "987654321000",
            "GroupId": "sg-22e4d55*"
        },
        {
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "PrefixListIds": [],
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "UserIdGroupPairs": [],
                    "Ipv6Ranges": []
                }
            ],
            "Description": "ssh - connection security gtoup",
            "Tags": [
                {
                    "Value": "sg-ssh",
                    "Key": "Name"
                }
            ],
            "IpPermissions": [
                {
                    "PrefixListIds": [],
                    "FromPort": 22,
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "ToPort": 22,
                    "IpProtocol": "tcp",
                    "UserIdGroupPairs": [],
                    "Ipv6Ranges": []
                }
            ],
            "GroupName": "sg_ssh",
            "VpcId": "vpc-8db025e*",
            "OwnerId": "987654321000",
            "GroupId": "sg-aae7d6d*"
        },
        {
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "PrefixListIds": [],
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "UserIdGroupPairs": [],
                    "Ipv6Ranges": []
                }
            ],
            "Description": "default VPC security group",
            "Tags": [
                {
                    "Value": "sg-default",
                    "Key": "Name"
                }
            ],
            "IpPermissions": [],
            "GroupName": "default",
            "VpcId": "vpc-8db025e*",
            "OwnerId": "987654321000",
            "GroupId": "sg-c5f7c5a*"
        },
        {
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "PrefixListIds": [],
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "UserIdGroupPairs": [],
                    "Ipv6Ranges": []
                }
            ],
            "Description": "default VPC security group",
            "Tags": [
                {
                    "Value": "",
                    "Key": "Name"
                }
            ],
            "IpPermissions": [
                {
                    "IpProtocol": "-1",
                    "PrefixListIds": [],
                    "IpRanges": [],
                    "UserIdGroupPairs": [
                        {
                            "UserId": "987654321000",
                            "GroupId": "sg-da033eb*"
                        }
                    ],
                    "Ipv6Ranges": []
                }
            ],
            "GroupName": "default",
            "VpcId": "vpc-9e7f74fa",
            "OwnerId": "987654321000",
            "GroupId": "sg-da033eb*"
        },
        {
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "PrefixListIds": [],
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "UserIdGroupPairs": [],
                    "Ipv6Ranges": []
                }
            ],
            "Description": "http - connection security gtoup",
            "Tags": [
                {
                    "Value": "sg-http",
                    "Key": "Name"
                }
            ],
            "IpPermissions": [
                {
                    "PrefixListIds": [],
                    "FromPort": 80,
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "ToPort": 80,
                    "IpProtocol": "tcp",
                    "UserIdGroupPairs": [],
                    "Ipv6Ranges": []
                }
            ],
            "GroupName": "sg_http",
            "VpcId": "vpc-8db025e*",
            "OwnerId": "987654321000",
            "GroupId": "sg-ebe6d79*"
        }
    ]
}

 

出力結果から以下の項目が取得できます。

項目 形式 説明 対応箇所
Description  string セキュリティグループの説明。  説明
GroupName string セキュリティグループの名前。 グループ名

IpPermissions

IpPermissionsEgress

list

セキュリティグループに関連付けられた1つまたは複数の受信規則。

IpPermissions:インバウンド
IpPermissionsEgress:アウトバウンド

 FromPort
 ToPort

integer

TCPおよびUDPプロトコルのnge、またはICMP / ICMPv6タイプ番号。 -1の値は、すべてのICMP / ICMPv6タイプを示します。 すべてのICMP / ICMPv6タイプを指定する場合は、すべてのコードを指定する必要があります。

FromPort:開始ポート番号
ToPort:終了ポート番号

ポート範囲
 IpProtocol string IPプロトコル名(tcp、udp、icmp)または番号。 プロトコル
 IpRanges list 1つまたは複数のIPv4範囲。
  CidrIp string IPv4 CIDRの範囲。 両方ではなく、CIDR範囲または送信元セキュリティグループを指定できます。 単一のIPv4アドレスを指定するには、/ 32接頭辞の長さを使用します。 ソース
  Description  string  このIPv4アドレス範囲を参照するセキュリティグループルールの説明。 説明

 

補足


この記事を書いた人
名前:TRUE's。 千葉県育ち、神奈川県在住のIT系フリーエンジニア。 IT系のナレッジサイトを不定期で更新中。 フォトグラファー兼エンジニアとして日々勤しんでいる。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です