この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
こんにちは、せーのです。今日はAWSサービスとアプリコーディングの融合の為に必要なTipsをご紹介します。
AWSのIPレンジはハードコーディングしてはいけない
AWSの各サービスに振分けられているIPアドレスは定期的に変わるので、例えばアプリ内で「リクエストがCloudfront以外のものはエラー」というような処理をする場合、その時点で発表されているIPレンジ(例:https://forums.aws.amazon.com/ann.jspa?annID=2051)をハードコーディングしても時間と共に必ず穴が開いてきます。基本的にAWSにおける「IP」をアプリに組み込むのはバッドノウハウというのが定説です。
ではどうするのか
それでもどうしても「リクエストがCloudfront以外のものはエラー」というような仕様が外せない場合はどうしたら良いのでしょう。その一番現実的な解は「リクエスト時にリアルタイムでIPレンジを取得してマッチング」という実装となります。
IPレンジのリアルタイムな取得
IPレンジのリアルタイムな取得はREST APIで用意されています。
こちらを叩くとJSONフォーマットで結果が返ってきます。
https://ip-ranges.amazonaws.com/ip-ranges.json
{
"syncToken": "1420548129",
"createDate": "2015-01-06-12-46-01",
"prefixes": [
{
"ip_prefix": "50.19.0.0/16",
"region": "us-east-1",
"service": "AMAZON"
},
{
"ip_prefix": "54.239.98.0/24",
"region": "us-east-1",
"service": "AMAZON"
},
{
"ip_prefix": "205.251.254.0/24",
"region": "GLOBAL",
"service": "AMAZON"
},
{
"ip_prefix": "75.101.128.0/17",
"region": "us-east-1",
"service": "AMAZON"
},
.........
- syncToken:UNIX エポック時刻形式での公開時刻
- createDate:公開日時
- ip_prefix:CIDR 表記でのパブリック IP アドレス範囲
- region:AWS リージョンまたはエッジロケーションの GLOBAL。※CLOUDFRONT および ROUTE53 範囲は GLOBAL であることに注意
- service:IP アドレス範囲のサブセット。すべての IP アドレス範囲を取得するには AMAZON を指定します(たとえば、EC2 サブセットの範囲は、AMAZON サブセットにも含まれます)。※一部の IP アドレス範囲は、AMAZON サブセットのみに含まれていることに注意
こちらをプログラムに組み込むことでリアルタイムなIPレンジを取得したコーディングが可能です。JSONフォーマットでも充分使いやすいのですが、JSONをパースするのがめんどくさい、という方に朗報です。最近のアップデートではなんとコマンド一発で色々な形で取得できるんです。そう、Windowsならね。
PowerShellによる取得
まずはPowerShellによる取得です。PowerShellのインストールや概要はこちらの記事を参考にしてください。
- AWS Tools for Windows PowerShell : http://aws.amazon.com/jp/powershell/
インストールしたら起動してみましょう。手元にあるWinodowsがWin10 Technical Previewなのでこちらで試してみます。
起動時には「管理者で実行」するように気をつけてください。これを忘れると
The term 'Initialize-AWSDefaults' is not recognized as the name of a cmdlet, function, script file, or operable program.
...後略...
というエラーが出て上手く起動しません。
初めて起動した方はRegionやCredentials情報を聞かれるかと思いますのでIAM UserでAPI Key, API Secretを取得しておきましょう。では起動したら早速IPレンジを取得してみます。取得方法は簡単、Get-AWSPublicIpAddressRangeこれだけです。
PS C:\Windows\system32> Get-AWSPublicIpAddressRange
IpPrefix Region Service
-------- ------ -------
50.19.0.0/16 us-east-1 AMAZON
54.239.98.0/24 us-east-1 AMAZON
205.251.254.0/24 GLOBAL AMAZON
75.101.128.0/17 us-east-1 AMAZON
54.240.192.0/22 ap-southeast-2 AMAZON
54.194.0.0/15 eu-west-1 AMAZON
54.231.224.0/21 ap-northeast-1 AMAZON
54.239.192.0/19 GLOBAL AMAZON
54.208.0.0/15 us-east-1 AMAZON
54.172.0.0/15 us-east-1 AMAZON
50.16.0.0/15 us-east-1 AMAZON
54.239.54.0/23 eu-central-1 AMAZON
54.222.0.0/20 cn-north-1 AMAZON
54.76.0.0/15 eu-west-1 AMAZON
54.220.0.0/16 eu-west-1 AMAZON
46.51.216.0/21 ap-southeast-1 AMAZON
54.156.0.0/14 us-east-1 AMAZON
54.232.0.0/16 sa-east-1 AMAZON
54.240.216.0/22 us-east-1 AMAZON
54.254.0.0/16 ap-southeast-1 AMAZON
205.251.228.0/22 us-west-1 AMAZON
54.238.0.0/16 ap-northeast-1 AMAZON
54.64.0.0/15 ap-northeast-1 AMAZON
174.129.0.0/16 us-east-1 AMAZON
54.231.232.0/21 us-west-1 AMAZON
54.250.0.0/16 ap-northeast-1 AMAZON
175.41.192.0/18 ap-northeast-1 AMAZON
54.179.0.0/16 ap-southeast-1 AMAZON
54.231.160.0/19 us-west-2 AMAZON
54.199.0.0/16 ap-northeast-1 AMAZON
176.34.32.0/19 ap-northeast-1 AMAZON
205.251.232.0/22 us-west-2 AMAZON
204.246.176.0/20 GLOBAL AMAZON
54.246.0.0/16 eu-west-1 AMAZON
46.137.128.0/18 eu-west-1 AMAZON
54.92.128.0/17 us-east-1 AMAZON
176.32.120.0/22 us-east-1 AMAZON
54.240.240.0/24 eu-central-1 AMAZON
......後略......
簡単に取得出来ました。またこちらは絞り込みも可能です。Serviceで絞り込んでみます。
PS C:\Windows\system32> Get-AWSPublicIpAddressRange -ServiceKey ec2
IpPrefix Region Service
-------- ------ -------
50.19.0.0/16 us-east-1 EC2
75.101.128.0/17 us-east-1 EC2
54.194.0.0/15 eu-west-1 EC2
54.208.0.0/15 us-east-1 EC2
54.172.0.0/15 us-east-1 EC2
50.16.0.0/15 us-east-1 EC2
54.76.0.0/15 eu-west-1 EC2
54.220.0.0/16 eu-west-1 EC2
46.51.216.0/21 ap-southeast-1 EC2
54.156.0.0/14 us-east-1 EC2
54.232.0.0/16 sa-east-1 EC2
54.254.0.0/16 ap-southeast-1 EC2
54.238.0.0/16 ap-northeast-1 EC2
54.64.0.0/15 ap-northeast-1 EC2
174.129.0.0/16 us-east-1 EC2
54.250.0.0/16 ap-northeast-1 EC2
175.41.192.0/18 ap-northeast-1 EC2
54.179.0.0/16 ap-southeast-1 EC2
54.199.0.0/16 ap-northeast-1 EC2
176.34.32.0/19 ap-northeast-1 EC2
54.246.0.0/16 eu-west-1 EC2
46.137.128.0/18 eu-west-1 EC2
54.92.128.0/17 us-east-1 EC2
54.94.0.0/16 sa-east-1 EC2
52.64.0.0/17 ap-southeast-2 EC2
54.223.0.0/16 cn-north-1 EC2
184.72.0.0/18 us-west-1 EC2
54.247.0.0/16 eu-west-1 EC2
54.224.0.0/15 us-east-1 EC2
54.215.0.0/16 us-west-1 EC2
......後略......
次はリージョンです。
PS C:\Windows\system32> Get-AWSPublicIpAddressRange -Region ap-northeast-1
IpPrefix Region Service
-------- ------ -------
54.231.224.0/21 ap-northeast-1 AMAZON
54.238.0.0/16 ap-northeast-1 AMAZON
54.64.0.0/15 ap-northeast-1 AMAZON
54.250.0.0/16 ap-northeast-1 AMAZON
175.41.192.0/18 ap-northeast-1 AMAZON
54.199.0.0/16 ap-northeast-1 AMAZON
176.34.32.0/19 ap-northeast-1 AMAZON
54.240.200.0/24 ap-northeast-1 AMAZON
46.51.224.0/19 ap-northeast-1 AMAZON
54.248.0.0/15 ap-northeast-1 AMAZON
54.240.225.0/24 ap-northeast-1 AMAZON
54.150.0.0/16 ap-northeast-1 AMAZON
54.178.0.0/16 ap-northeast-1 AMAZON
54.92.0.0/17 ap-northeast-1 AMAZON
176.32.64.0/19 ap-northeast-1 AMAZON
54.239.52.0/23 ap-northeast-1 AMAZON
103.246.150.0/23 ap-northeast-1 AMAZON
176.34.0.0/19 ap-northeast-1 AMAZON
54.168.0.0/16 ap-northeast-1 AMAZON
54.239.96.0/24 ap-northeast-1 AMAZON
27.0.0.0/22 ap-northeast-1 AMAZON
54.95.0.0/16 ap-northeast-1 AMAZON
103.4.8.0/21 ap-northeast-1 AMAZON
54.238.0.0/16 ap-northeast-1 EC2
54.64.0.0/15 ap-northeast-1 EC2
54.250.0.0/16 ap-northeast-1 EC2
175.41.192.0/18 ap-northeast-1 EC2
54.199.0.0/16 ap-northeast-1 EC2
176.34.32.0/19 ap-northeast-1 EC2
46.51.224.0/19 ap-northeast-1 EC2
54.248.0.0/15 ap-northeast-1 EC2
54.150.0.0/16 ap-northeast-1 EC2
54.178.0.0/16 ap-northeast-1 EC2
54.92.0.0/17 ap-northeast-1 EC2
176.32.64.0/19 ap-northeast-1 EC2
176.34.0.0/19 ap-northeast-1 EC2
54.168.0.0/16 ap-northeast-1 EC2
54.95.0.0/16 ap-northeast-1 EC2
103.4.8.0/21 ap-northeast-1 EC2
54.248.220.0/26 ap-northeast-1 ROUTE53_HEALTHCHECKS
54.250.253.192/26 ap-northeast-1 ROUTE53_HEALTHCHECKS
どうですか。簡単ですね。また絞り込みはカンマ区切りでOR検索されます。
PS C:\Windows\system32> Get-AWSPublicIpAddressRange -Region ap-northeast-1,us-west-1 -ServiceKey ec2,route53_healthchecks
IpPrefix Region Service
-------- ------ -------
54.238.0.0/16 ap-northeast-1 EC2
54.64.0.0/15 ap-northeast-1 EC2
54.250.0.0/16 ap-northeast-1 EC2
175.41.192.0/18 ap-northeast-1 EC2
54.199.0.0/16 ap-northeast-1 EC2
176.34.32.0/19 ap-northeast-1 EC2
184.72.0.0/18 us-west-1 EC2
54.215.0.0/16 us-west-1 EC2
54.176.0.0/15 us-west-1 EC2
54.193.0.0/16 us-west-1 EC2
50.18.0.0/16 us-west-1 EC2
184.169.128.0/17 us-west-1 EC2
46.51.224.0/19 ap-northeast-1 EC2
54.241.0.0/16 us-west-1 EC2
54.67.0.0/16 us-west-1 EC2
204.236.128.0/18 us-west-1 EC2
54.151.0.0/17 us-west-1 EC2
54.248.0.0/15 ap-northeast-1 EC2
54.150.0.0/16 ap-northeast-1 EC2
54.178.0.0/16 ap-northeast-1 EC2
54.92.0.0/17 ap-northeast-1 EC2
176.32.64.0/19 ap-northeast-1 EC2
54.183.0.0/16 us-west-1 EC2
176.34.0.0/19 ap-northeast-1 EC2
54.168.0.0/16 ap-northeast-1 EC2
54.219.0.0/16 us-west-1 EC2
54.95.0.0/16 ap-northeast-1 EC2
103.4.8.0/21 ap-northeast-1 EC2
54.241.32.64/26 us-west-1 ROUTE53_HEALTHCHECKS
54.248.220.0/26 ap-northeast-1 ROUTE53_HEALTHCHECKS
54.250.253.192/26 ap-northeast-1 ROUTE53_HEALTHCHECKS
54.183.255.128/26 us-west-1 ROUTE53_HEALTHCHECKS
発行日時も絞り込みで取得できますので、定期バッチでこちらを回して更新されていたらDBにDelete-Insert、みたいな処理もできますね。
PS C:\Windows\system32> Get-AWSPublicIpAddressRange -OutputPublicationDate
2015年1月6日 12:46:01
最後にService一覧の取得です。
PS C:\Windows\system32> Get-AWSPublicIpAddressRange -OutputServiceKeys
AMAZON
EC2
ROUTE53
ROUTE53_HEALTHCHECKS
CLOUDFRONT
まとめ
いかがでしたでしょうか。Windows以外ならJSONフォーマット、Windows系をお使いならPowerShellを使ってより細かなセキュリティ実装をしてみてはいかがでしょうか。 また.NET系をお使いの方ならAWS SDK for .NETのAmazon.Util名前空間にあるAWSPublicIpAddressRangesクラスでも同じことが可能なのでこちらもチェックしてみてくださいね!
参考サイト
- http://blogs.aws.amazon.com/net/post/Tx22LH60TXQP3B2/Querying-the-Public-IP-Address-Ranges-for-AWS
- https://aws.amazon.com/blogs/aws/aws-ip-ranges-json/
- http://docs.aws.amazon.com/ja_jp/general/latest/gr/aws-ip-ranges.html
- http://aws.amazon.com/releasenotes/PowerShell/5286787901368546
- https://forums.aws.amazon.com/thread.jspa?messageID=540410