Help us improve the AWS re:Post Knowledge Center by sharing your feedback in a brief survey. Your input can influence how we create and update our content to better support your AWS journey.
Amazon VPC におけるパブリック IPv4 アドレスの使用状況を特定、監視、最適化する方法を教えてください。
Amazon Virtual Private Cloud (Amazon VPC) における IPv4 アドレスの使用状況を特定、監視、最適化したいです。
解決策
注: AWS コマンドラインインターフェイス (AWS CLI) コマンドの実行中にエラーが発生した場合は、「AWS CLI で発生したエラーのトラブルシューティング」を参照してください。また、AWS CLI の最新バージョンを使用していることを確認してください。
すべてのパブリック IPv4 アドレスには、そのアドレスがサービスにアタッチされているかどうかにかかわらず、1 時間ごとに、各 IP アドレスに 0.005 USDの料金が発生します。Amazon Elastic Compute Cloud (Amazon EC2) の AWS 無料利用枠ユーザーには、750 時間の無料使用権が付与されます。この無料使用権は、パブリック IPv4 アドレスを持つ Amazon EC2 インスタンスを起動する場合に適用されます。
Public IP Insights を有効にする
Public IP Insights を使用すると、複数の AWS リージョンにわたり、サービスが現在使用しているパブリック IPv4 アドレスの監視、分析、監査を行うことができます。
Public IP Insights を有効化するには、Amazon VPC IP Address Manager (IPAM) プールを作成します。パブリック IPv4 アドレスの使用状況を特定するには、IPAM の Public IP Insights を単一の AWS アカウントまたは、AWS Organizations の各アカウントに統合します。
注: 無料利用枠では、Public IP Insights を無料で使用できます。Advanced レベルの価格情報については、Amazon VPC の料金ページで IPAM タブを参照してください。IPAM を組織に統合すると、無料利用枠を使用中の場合も、組織内のすべてのリージョンとアカウントに対し、Public IP Insights を使用できます。
結果の表示には、Amazon VPC コンソールまたは AWS CLI を使用できます。
注: IPAM プールを作成すると、Public IP Insights はパブリック IPv4 アドレスに関するレポートを作成します。レポートの生成に必要な時間は、組織の規模によって異なります。
Cost and Usage Report を有効化する
AWS Cost and Usage Report (CUR) には、使用中および未使用のパブリック IPv4 アドレスに関する使用状況データが含まれます。CUR の作成時に [リソース ID を含める] を選択すると、詳細なリソースレベルの分析情報を取得できます。CUR の生成後、AWS は、1 日に 1 回以上の頻度で月初来のデータを含むレポートファイルを更新します。レポートファイルを参照すると、過去の IPv4 アドレス使用状況データを分析できます。
注: レポートには、Amazon Simple Storage Service (Amazon S3) バケットからアクセスできます。AWS が Amazon S3 バケットにレポートを配信するまでに、最大 24 時間かかる場合があります。
パブリック IPv4 アドレスを即座に特定する
注: パブリック IPv4 アドレスの使用状況を計画的に分析するには、CUR と Public IP Insights の併用をおすすめします。
パブリック IPv4 アドレスを使用しているサービスを即座に特定するには、Amazon EC2 コンソールまたは AWS CLI を使用します。
Amazon EC2 コンソール
ネットワークインターフェイスを分析し、パブリック IPv4 アドレスを使用している AWS サービスを確認するには、次の手順を実行します。
- Amazon EC2 コンソールを開きます。
- ナビゲーションペインの [ネットワークとセキュリティ] で [ネットワークインターフェイス] を選択します。
- [パブリック IPv4 アドレス] のフィルターに <*> を入力すると、プライマリパブリック IPv4 アドレスを使用している Elastic ネットワークインターフェイスを特定できます。
- ネットワークインターフェイスの説明とインターフェイスタイプを参考に、パブリック IPv4 アドレスを使用しているサービスを識別します。
- リージョン内の、ネットワークインターフェイスを持つすべてのパブリック IP アドレスとセカンダリ IP アドレスを表示するには、次のコマンドを実行します。
注: example-region を目的のリージョンに置き換えてください。aws ec2 describe-network-interfaces --region example-region --query 'NetworkInterfaces[*].PrivateIpAddresses[?Association.PublicIp].Association.PublicIp[]' --output table
AWS Site-to-Site VPN が使用するパブリック IPv4 アドレスについては、「VPN トンネルの現在の状態を確認する方法を教えてください」を参照してください。
標準およびカスタムの AWS Global Accelerator が使用するパブリック IPv4 アドレスについては、「アクセラレータを確認する」を参照してください。
アイドル状態、または関連づけられている Elastic IP アドレスを確認するには、次の手順を実行します。
- Amazon EC2 コンソールを開きます。
- ナビゲーションペインの [ネットワークとセキュリティ] で [Elastic IP] を選択します。
- Elastic IP アドレスを選択すると、その Elastic IP アドレスを使用するリソースが表示されます。
注: 使用状況の詳細については、[ネットワークインターフェイス] または [インスタンス] フィールドを確認してください。
独自 IP アドレスの使用 (BYOIPs) には、料金はかかりません。AWS では、us-west-2 リージョンの Global Accelerator に対して料金が発生します。
AWS CLI
前提条件: AWS CLI コマンド aws configure を実行し、認証情報を設定する必要があります。Boto3 のインストールが必要です。詳細については、Boto3 のウェブサイトで「Boto3 のインストール」を参照してください。
パブリック IPv4 アドレスを使用しているサービスを即座に特定するには、AWS CloudShell または Linux 環境で次の手順を実行します。
-
次のファイルを作成します。
touch public_ipv4_recon.py -
次のコマンドを実行してファイルを編集します。
vim public_ipv4_recon.py -
次のコードをコピーしてファイルに貼り付け、ファイルを保存します。
#!/usr/bin/env python3 import pprint import boto3, sys profile = boto3.Session(profile_name=sys.argv[1]) aga = profile.client('globalaccelerator', region_name='us-west-2') ec2 = boto3.client('ec2') def global_public_ipv4_lookup(aga): try: # global accelerator next_token = None while True: if next_token: resource = aga.list_accelerators( NextToken = next_token ) else: resource = aga.list_accelerators( ) print('Describing world wide Global Accelerators...') print('Note: AWS Global Accelerators are billed in us-west-2....') print(f'Number of AGA: {len(resource["Accelerators"])}') print('-'*40) for item in resource["Accelerators"]: print(f'Name: {item["Name"]}') if 'IpSets' in item.keys(): for ip in item["IpSets"][0]["IpAddresses"]: print(f'Public IPv4: {ip}') print(f'Status: {item["Status"]}') print() next_token = resource.get("NextToken") if next_token is None: break print() # custom_routing_accelerators next_token = None while True: if next_token: custom_routing = aga.list_custom_routing_accelerators( NextToken = next_token ) else: custom_routing = aga.list_custom_routing_accelerators( ) print('Describing world wide Custom Routing Accelerators...') print('Note: AWS Global Accelerators are billed in us-west-2....') print(f'Number of custom AGA: {len(custom_routing["Accelerators"])}') print('-'*40) for item in custom_routing["Accelerators"]: if 'IpSets' in item.keys(): for ip in item["IpSets"][0]["IpAddresses"]: print(f'Public IPv4: {ip}') print(f'Status: {item["Status"]}') print() next_token = custom_routing.get("NextToken") if next_token is None: break print() except Exception as err: print(f'Error found: {err}...') pass def public_ipv4_lookup(ec2): try: # vpn next_token = None while True: if next_token: vpn = ec2.describe_vpn_connections( NextToken = next_token ) else: vpn = ec2.describe_vpn_connections( ) print('Describing VPNs...') print(f'Number of Vpn connections: {len(vpn["VpnConnections"])}') print('-'*40) for item in vpn["VpnConnections"]: if 'VpnConnectionId' in item.keys(): print(f'Vpn Id: {item["VpnConnectionId"]}') for ip in item["VgwTelemetry"]: print(f'Public ipv4: {ip["OutsideIpAddress"]}') print() next_token = vpn.get("NextToken") if next_token is None: break print() # elastic ip eip = ec2.describe_addresses( ) print('Describing Elastic IPs...') print(f'Number of Elastic Ips: {len(eip["Addresses"])}') print('-'*40) for item in eip["Addresses"]: if 'AllocationId' in item.keys(): print(f'Eip Id: {item["AllocationId"]}') print(f'Public ipv4: {item["PublicIp"]}') print() print() # network interfaces next_token = None while True: if next_token: interface = ec2.describe_network_interfaces( NextToken=next_token ) else: interface = ec2.describe_network_interfaces( ) print('Describing Network Interfaces...') print(f'Number of interfaces: {len(interface["NetworkInterfaces"])}') print('Only printing Interfaces with a public IPv4 address...') print('-'*40) for item in interface["NetworkInterfaces"]: for ip in item["PrivateIpAddresses"]: if 'Association' not in ip.keys(): pass else: print(f'Interface Id: {item["NetworkInterfaceId"]}') print(f'Description: {item["Description"]}') print(f'Status: {interface["NetworkInterfaces"][0]["Status"]}') print(f'Public Ip: {ip["Association"]["PublicIp"]}\n') next_token = interface.get("NextToken") if next_token is None: break except Exception as err: print(f'Error found: {err}...') pass # Run code if len(sys.argv) < 3 or not sys.argv[2]: global_public_ipv4_lookup(aga) regions_list = ec2.describe_regions( AllRegions=False ) for region in regions_list['Regions']: if region["OptInStatus"] == 'opted-in' or 'opt-in-not-required': print(f'\n**********-[{region["RegionName"]}]-**********\n') public_ipv4_lookup( ec2=profile.client('ec2', region_name=region["RegionName"]) ) elif sys.argv[2]: global_public_ipv4_lookup(aga) public_ipv4_lookup( ec2=profile.client('ec2', region_name=sys.argv[2]) ) -
すべてのリージョンで確認を行うには、次のコマンドを実行します。
python3 public_ipv4_recon.py example-cli-profile注: example-cli profile を、設定した AWS CLI プロファイル名、または .aws/credentials ファイルで定義したいずれかのプロファイル名に置き換えてください。Windows を使用する場合は、python3 を python に置き換えてください。
または、
特定のリージョンを確認するには、次のコマンドを実行します。python3 public_ipv4_recon.py example-cli-profile example-region注: example-cli profile を、設定した AWS CLI プロファイル名、または .aws/credentials ファイルで定義したいずれかのプロファイル名に置き換えてください。example-region を目的のリージョンに置き換えてください。
リクエストを実行する権限がない場合、またはアカウントでリージョンが無効になっている場合、次のエラーが発生する可能性があります。
「Error found: エラー (AuthFailure) が、 <API> 次の操作の呼び出し時に発生しました。 AWS は取得したアクセス認証情報を検証できませんでした...」
パブリック IPv4 アドレスの使用を最適化し、IPv6 アドレスを導入する
コストを最適化するには、現在のアーキテクチャを改善し、パブリック IPv4 アドレスの使用を最小化します。IPv6 への移行をおすすめします。IPv6 アドレスには料金はかかりません。
- 言語
- 日本語

関連するコンテンツ
- 質問済み 9ヶ月前