4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWS IP アドレスの範囲

Last updated at Posted at 2020-05-18

#はじめに
セキュリティグループのInBound、OutBoundにてAWSのサービス(API Gateway、S3など)を許可するように設定したい場合は、AWS IP アドレスの範囲を見て設定可能です。

#AWS IP アドレスの範囲
https://ip-ranges.amazonaws.com/ip-ranges.json

image.png

#AWS の IP アドレス範囲の変更通知
AWS の IP アドレス範囲に変更がある際に、SNS通知設定をし、受信したら差分をメンテする。

設定方法:https://docs.aws.amazon.com/ja_jp/general/latest/gr/aws-ip-ranges.html#subscribe-notifications

#PythonであるサービスのIPを取り出す例

import urllib.request
import json

def get_ip_groups_json(url):

    response = urllib.request.urlopen(url)
    ip_json = response.read()

    return ip_json

def get_ranges_for_service(ip_range_url, service, subset):

    ip_ranges_json = json.loads(get_ip_groups_json(ip_range_url))

    service_ranges = list()

    for prefix in ip_ranges_json['prefixes']:
        if prefix['service'] == service and subset == prefix['region']:
            service_ranges.append(prefix['ip_prefix'])

    return service_ranges


ip_ranges = get_ranges_for_service('https://ip-ranges.amazonaws.com/ip-ranges.json', 'S3', 'ap-southeast-1')

for ip in ip_ranges: print(str(ip))

結果:
image.png

#Lambdaで自動更新
メンテ作業を自動化にしたい場合は、Lambdaでセキュリティグループを更新することも可能です。
サンプル:update-security-groups

参考Doc:AWS IP アドレスの範囲

以上

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?