LoginSignup
4
5

More than 5 years have passed since last update.

東京 リージョン にある AWS EC2 インスタンスの IP アドレス レンジ を取得する方法

Last updated at Posted at 2016-05-31

あまり需要が無さそうだけど、たまたま、掲題の件を知る必要があったので、メモ。

curljq を使用して、下記のように実行する。

curl -s -X GET https://ip-ranges.amazonaws.com/ip-ranges.json \
  | jq -r '.prefixes[]
    | select (.region == "ap-northeast-1")
    | select (.service == "EC2")
    | .ip_prefix' \
;

この記事を投稿した時点での結果は下記の通り。

$ curl -s -X GET https://ip-ranges.amazonaws.com/ip-ranges.json | jq -r '.prefixes[] | select (.region == "ap-northeast-1") | select (.service == "EC2") | .ip_prefix'

46.51.224.0/19
52.68.0.0/15
52.95.243.0/24
52.95.255.48/28
52.192.0.0/15
52.196.0.0/14
54.64.0.0/15
54.92.0.0/17
54.95.0.0/16
54.150.0.0/16
54.168.0.0/16
54.178.0.0/16
54.199.0.0/16
54.238.0.0/16
54.248.0.0/15
54.250.0.0/16
103.4.8.0/21
175.41.192.0/18
176.32.64.0/19
176.34.0.0/19
176.34.32.0/19

以上。

以下、補足。


補足説明

AWS の IP アドレス レンジ 情報 取得

$ curl -s -X GET https://ip-ranges.amazonaws.com/ip-ranges.json

{
  "syncToken": "1464030365",
  "createDate": "2016-05-23-19-22-21",
  "prefixes": [
    {
      "ip_prefix": "23.20.0.0/14",
      "region": "us-east-1",
      "service": "AMAZON"
    },
    ...

JSON 形式の情報から必要な情報のみ抽出

  • jq コマンドを利用して prefixes という key に設定されている配列の value のみ抽出する。
    • 参考 : jq
$ curl -s -X GET https://ip-ranges.amazonaws.com/ip-ranges.json | jq '.prefixes[]'

{
  "ip_prefix": "23.20.0.0/14",
  "region": "us-east-1",
  "service": "AMAZON"
}
{
  "ip_prefix": "27.0.0.0/22",
  "region": "ap-northeast-1",
  "service": "AMAZON"
}
...

東京 リージョン (=ap-northeast-1) でフィルタリング

  • select (.region == "ap-northeast-1") という具合に filter を掛ける。
$ curl -s -X GET https://ip-ranges.amazonaws.com/ip-ranges.json | jq '.prefixes[] | select (.region == "ap-northeast-1")'

{
  "ip_prefix": "27.0.0.0/22",
  "region": "ap-northeast-1",
  "service": "AMAZON"
}
...
{
  "ip_prefix": "46.51.224.0/19",
  "region": "ap-northeast-1",
  "service": "EC2"
}
...
{
  "ip_prefix": "54.248.220.0/26",
  "region": "ap-northeast-1",
  "service": "ROUTE53_HEALTHCHECKS"
}

EC2 でフィルタリング

  • select (.service == "EC2") という具合に filter を掛ける。
$ curl -s -X GET https://ip-ranges.amazonaws.com/ip-ranges.json | jq '.prefixes[] | select (.region == "ap-northeast-1") | select (.service == "EC2")'

{
  "ip_prefix": "46.51.224.0/19",
  "region": "ap-northeast-1",
  "service": "EC2"
}
{
  "ip_prefix": "52.68.0.0/15",
  "region": "ap-northeast-1",
  "service": "EC2"
}
{
  "ip_prefix": "52.95.243.0/24",
  "region": "ap-northeast-1",
  "service": "EC2"
}
...

IP アドレス レンジ 情報のみを抽出

  • 今回必要な情報のみを抽出する。
$ curl -s -X GET https://ip-ranges.amazonaws.com/ip-ranges.json | jq '.prefixes[] | select (.region == "ap-northeast-1") | select (.service == "EC2") | .ip_prefix'

"46.51.224.0/19"
"52.68.0.0/15"
"52.95.243.0/24"
"52.95.255.48/28"
"52.192.0.0/15"
"52.196.0.0/14"
"54.64.0.0/15"
"54.92.0.0/17"
"54.95.0.0/16"
"54.150.0.0/16"
"54.168.0.0/16"
"54.178.0.0/16"
"54.199.0.0/16"
"54.238.0.0/16"
"54.248.0.0/15"
"54.250.0.0/16"
"103.4.8.0/21"
"175.41.192.0/18"
"176.32.64.0/19"
"176.34.0.0/19"
"176.34.32.0/19"

" (ダブルコーテーション) 除去

  • " (ダブルコーテーション) が不要な場合は jq コマンドに -r オプションを付与する。
$ curl -s -X GET https://ip-ranges.amazonaws.com/ip-ranges.json | jq -r '.prefixes[] | select (.region == "ap-northeast-1") | select (.service == "EC2") | .ip_prefix'

46.51.224.0/19
52.68.0.0/15
52.95.243.0/24
52.95.255.48/28
52.192.0.0/15
52.196.0.0/14
54.64.0.0/15
54.92.0.0/17
54.95.0.0/16
54.150.0.0/16
54.168.0.0/16
54.178.0.0/16
54.199.0.0/16
54.238.0.0/16
54.248.0.0/15
54.250.0.0/16
103.4.8.0/21
175.41.192.0/18
176.32.64.0/19
176.34.0.0/19
176.34.32.0/19

おまけ

AWS CLI を使用して Security Group に https を通す対応をする場合。

参考 : aws ec2 authorize-security-group-ingress

#!/bin/bash

AWS_SECURITY_GROUP_ID="sg-xxxxxxxx"
for CIDR in $(curl -s -X GET https://ip-ranges.amazonaws.com/ip-ranges.json | jq -r '.prefixes[] | select (.region == "ap-northeast-1") | select (.service == "EC2") | .ip_prefix'); do
  aws ec2 authorize-security-group-ingress --group-id $AWS_SECURITY_GROUP_ID --protocol tcp --port 443 --cidr $CIDR
done

exit 0

何が嬉しいのかはさておき…。

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5