特定のCIDRを許可しているセキュリティグループを検索したい

特定のIPアドレス帯(オフィスのグローバルIPなど)を許可しているセキュリティグループが、アカウント内にいくつあるかを調べたい場合がある。セキュリティグループの数が多いと、マネジメントコンソールで1つずつルールを確認するのは手間がかかる。aws ec2 describe-security-group-rulesは、フィルタを指定しなければアカウント内(対象リージョン)の全セキュリティグループルールを返すため、--queryを組み合わせれば特定のCIDRを許可しているルールを横断的に検索できる。

$ aws ec2 describe-security-group-rules \
  --query 'SecurityGroupRules[?CidrIpv4==`203.0.113.0/24`]'
[
    {
        "SecurityGroupRuleId": "sgr-0cd250011675fcae2",
        "GroupId": "sg-07a1267f4619bc6a6",
        "GroupOwnerId": "123456789012",
        "IsEgress": true,
        "IpProtocol": "tcp",
        "FromPort": 443,
        "ToPort": 443,
        "CidrIpv4": "203.0.113.0/24",
        "Description": "office egress",
        "Tags": [],
        "SecurityGroupRuleArn": "arn:aws:ec2:ap-northeast-1:123456789012:security-group-rule/sgr-0cd250011675fcae2"
    },
    {
        "SecurityGroupRuleId": "sgr-00a4d7c0a645b2b45",
        "GroupId": "sg-0e7e9bed480c0e1f4",
        "GroupOwnerId": "123456789012",
        "IsEgress": false,
        "IpProtocol": "tcp",
        "FromPort": 3306,
        "ToPort": 3306,
        "CidrIpv4": "203.0.113.0/24",
        "Description": "office db access",
        "Tags": [],
        "SecurityGroupRuleArn": "arn:aws:ec2:ap-northeast-1:123456789012:security-group-rule/sgr-00a4d7c0a645b2b45"
    },
    {
        "SecurityGroupRuleId": "sgr-0c2a8a736d316ca39",
        "GroupId": "sg-07a1267f4619bc6a6",
        "GroupOwnerId": "123456789012",
        "IsEgress": false,
        "IpProtocol": "tcp",
        "FromPort": 443,
        "ToPort": 443,
        "CidrIpv4": "203.0.113.0/24",
        "Description": "office",
        "Tags": [],
        "SecurityGroupRuleArn": "arn:aws:ec2:ap-northeast-1:123456789012:security-group-rule/sgr-0c2a8a736d316ca39"
    }
]

203.0.113.0/24を許可しているルールが2つのセキュリティグループから見つかった。sg-07a1267f4619bc6a6はインバウンド・アウトバウンド両方に同じCIDRのルールを持つため2件ヒットしている。--filtersにはCidrIpv4で絞り込むフィルタが用意されていないため、--query側で絞り込む必要がある。

セキュリティグループIDだけを一覧する

ルールの詳細が不要な場合は、--queryGroupIdだけを抽出すると結果を確認しやすい。

$ aws ec2 describe-security-group-rules \
  --query 'SecurityGroupRules[?CidrIpv4==`203.0.113.0/24`].GroupId' \
  --output text
sg-07a1267f4619bc6a6	sg-0e7e9bed480c0e1f4	sg-07a1267f4619bc6a6

先ほどの例の通り、同じCIDRを許可するインバウンド・アウトバウンドのルールが両方設定されているセキュリティグループは、GroupIdが複数回出力される。重複を除きたい場合はsort -uと組み合わせる。

$ aws ec2 describe-security-group-rules \
  --query 'SecurityGroupRules[?CidrIpv4==`203.0.113.0/24`].GroupId' \
  --output text | tr '\t' '\n' | sort -u
sg-07a1267f4619bc6a6
sg-0e7e9bed480c0e1f4

特定のVPC内に絞り込んで検索する

describe-security-group-rules--filtersにはvpc-idが用意されていないため、VPC単位で絞り込みたい場合はdescribe-security-groupsで対象VPCのセキュリティグループID一覧を取得し、group-idフィルタに渡す。

$ aws ec2 describe-security-group-rules \
  --filters "Name=group-id,Values=$(aws ec2 describe-security-groups \
    --filters Name=vpc-id,Values=vpc-0a26533048807f1b3 \
    --query 'SecurityGroups[].GroupId' --output text | tr '\t' ',')" \
  --query 'SecurityGroupRules[?CidrIpv4==`203.0.113.0/24`].GroupId'
[
    "sg-07a1267f4619bc6a6",
    "sg-0e7e9bed480c0e1f4",
    "sg-07a1267f4619bc6a6"
]

セキュリティグループのルール1件ごとの詳細な見方は【AWS CLI】セキュリティグループのインバウンド・アウトバウンドルールをCLIで確認する を参照。