APC 技術ブログ

株式会社エーピーコミュニケーションズの技術ブログです。

株式会社 エーピーコミュニケーションズの技術ブログです。

【AWS】【初心者向け】料金表データ取得 (AWS Price List Query API)

目次

はじめに

こんにちは、クラウド事業部の菅です。

AWSでは、各サービスを利用することによって発生する費用を料金表として公開しています。
この料金表のデータは以下2通りの方法で取得できるようになっています。

  • AWS Price List Query API
  • AWS Price List Bulk API

今回はAWS Price List Query APIを使用して料金表データの取得を行ってみます。

AWS Price List Query API

概要

AWS CLIなどでコマンドを実行し、JSON形式で結果を取得する方法です。

公式ドキュメントによると、次のような場合に向いている方法とされています。

  • 製品の料金情報を検索する
  • フィルタに一致する製品と料金を検索する
  • フロントエンド環境など、リソースが限られているアプリケーションを開発している場合に必要な製品および料金を迅速に見つける

AWS Price List Query APIでは、SavingsPlansの料金表データは取得できないようです。

docs.aws.amazon.com

AWS Price List Query APIはpricing.apiのエンドポイント経由で実行されます。
pricing.apiエンドポイントは限られたリージョンにしか存在しませんので、API実行時は以下の何れかのリージョンを指定する必要があります。

  • us-east-1
  • eu-central-1
  • ap-south-1

料金表データの取得

AWS Price List Query APIで料金表データを取得する際は、できるだけ細かく条件を指定して情報を抽出する必要があります。
取得される情報量が多い場合、処理時間が長くなり、目的の情報を特定し辛くなります。

今回は、以下の料金表の金額をAWS Price List Query APIで取得します。

aws.amazon.com

1. 条件として設定可能な項目取得

料金表ページで設定した条件に対応する項目を取得します。

条件として設定可能な項目はサービスによって異なります。
各サービスで設定可能な項目は、以下のコマンドで取得できます。

aws pricing describe-services --region regionCoderegionCodeには「us-east-1」「eu-central-1」「ap-south-1」の何れかの値を指定します。

AmazonEC2のサービスで指定可能な項目の一覧を取得します。

~ $ aws pricing describe-services --region us-east-1 --service-code AmazonEC2
{
    "Services": [
        {
            "ServiceCode": "AmazonEC2",
            "AttributeNames": [
                "termType",
                (省略)
                "locationType",
                "instanceType",
                "currentGeneration",
                (省略)
                "processorArchitecture",
                "tenancy",
                "operatingSystem",
                "licenseModel",
                "usagetype",
                (省略)
                "normalizationSizeFactor",
                "preInstalledSw",
                "processorFeatures",
                "regionCode",
                "servicename",
                (省略)
                "fromRegionCode"
            ]
        }
    ],
    "FormatVersion": "aws_v1"
}

※service-codeオプションを使用することで、特定のサービスの情報のみ抽出することができます。

実行結果から、今回指定する条件に対応する項目は以下であることがわかりました。

条件 項目名
リージョン regionCode
オペレーティングシステム operatingSystem
プリインストールソフトウェア preInstalledSw
テナンシー tenancy
インスタンスタイプ instanceType
ライセンスモデル licenseModel

※リザーブドインスタンスの期間や支払いオプションなどの条件も対象ですが、今回は省略しています。

2. 各項目に設定する値の取得

情報の抽出は完全一致のみ対応しており、部分一致には対応していません。
そのため、各項目で使用されている値を確認する必要があります。

各項目で使用されている値は、以下のコマンドで取得できます。

aws pricing get-attribute-values --service-code serviceCode --attribute-name attributeName --region regionCode
  ※各パラメータには以下の値を指定します。
      serviceCode:「AmazonEC2」などのServiceCodeの値 ※1.の結果からも確認できます
      attributeName:取得対象の項目名 ※1.で取得した項目名を指定
      regionCode:「us-east-1」「eu-central-1」「ap-south-1」の何れかの値

1.で取得した各項目で使用されている値を取得します。

~ $ aws pricing get-attribute-values --service-code AmazonEC2 --attribute-name operatingSystem --region us-east-1
{
    "AttributeValues": [
        {
            "Value": "Linux"
        },
        {
            "Value": "NA"
        },
        {
            "Value": "RHEL"
        },
        {
            "Value": "Red Hat Enterprise Linux with HA"
        },
        {
            "Value": "SUSE"
        },
        {
            "Value": "Ubuntu Pro"
        },
        {
            "Value": "Windows"
        }
    ]
}
~ $ aws pricing get-attribute-values --service-code AmazonEC2 --attribute-name preInstalledSw --region us-east-1 | jq -r '.AttributeValues[].Value'
NA
SQL Ent
SQL Std
SQL Web
~ $ aws pricing get-attribute-values --service-code AmazonEC2 --attribute-name tenancy --region us-east-1 | jq -r '.AttributeValues[].Value'
Dedicated
Host
NA
Reserved
Shared
~ $ aws pricing get-attribute-values --service-code AmazonEC2 --attribute-name licenseModel --region us-east-1 | jq -r '.AttributeValues[].Value'
Bring your own license
NA
No License requirequired

※リージョンとインスタンスタイプは値が推測できますので省略しています。
 また、ブログ記載の都合によりoperatingSystem以外の実行結果をjqコマンドで整形しています。

実行結果から、各項目に設定する値は以下であることがわかりました。

条件 項目名 設定値
リージョン regionCode ap-northeast-3
オペレーティングシステム operatingSystem Windows
プリインストールソフトウェア preInstalledSw SQL Std
テナンシー tenancy Shared
インスタンスタイプ instanceType t3.xlarge
ライセンスモデル licenseModel No License requirequired

3. 料金表データの取得

1.と2.で取得した値を使用して料金表データを取得します。

料金表データは以下のコマンドで取得できます。

aws pricing get-products --service-code serviceCode --filters Type=TERM_MATCH,Field=attributeName,Value="value" --region regionCode
  ※各パラメータには以下の値を設定します。
      serviceCode:「AmazonEC2」などのServiceCodeの値 ※1.の結果からも確認できます
      attributeName:1.で取得した項目名
      value:2.で確認した設定値
      regionCode:「us-east-1」「eu-central-1」「ap-south-1」の何れかの値

filterオプションですが、上記形式の他にJSONファイルを指定することも可能なようです。
今回は、JSONファイルではなく直接値を指定しています。

~ $ aws pricing get-products --service-code AmazonEC2 --region us-east-1 --filters \
> Type=TERM_MATCH,Field=regionCode,Value="ap-northeast-3" \
> Type=TERM_MATCH,Field=operatingSystem,Value="Windows" \
> Type=TERM_MATCH,Field=preInstalledSw,Value="SQL Std" \
> Type=TERM_MATCH,Field=tenancy,Value="Shared" \
> Type=TERM_MATCH,Field=instanceType,Value="t3.xlarge" \
> Type=TERM_MATCH,Field=licenseModel,Value="No License requirequired" \
> | jq -r '.PriceList[]' | jq '.'
{
  "product": {
    "productFamily": "Compute Instance",
    "attributes": {
      "enhancedNetworkingSupported": "No",
      "intelTurboAvailable": "Yes",
      "memory": "16 GiB",
      "dedicatedEbsThroughput": "Up to 2780 Mbps",
      "vcpu": "4",
      "classicnetworkingsupport": "false",
      "capacitystatus": "Used",
      "locationType": "AWS Region",
      "storage": "EBS only",
      "instanceFamily": "General purpose",
      "operatingSystem": "Windows",
      "intelAvx2Available": "Yes",
      "regionCode": "ap-northeast-3",
      "physicalProcessor": "Intel Skylake E5 2686 v5",
      "clockSpeed": "3.1 GHz",
      "ecu": "NA",
      "networkPerformance": "Up to 5 Gigabit",
      "servicename": "Amazon Elastic Compute Cloud",
      "gpuMemory": "NA",
      "vpcnetworkingsupport": "true",
      "instanceType": "t3.xlarge",
      "tenancy": "Shared",
      "usagetype": "APN3-BoxUsage:t3.xlarge",
      "normalizationSizeFactor": "8",
      "intelAvxAvailable": "Yes",
      "processorFeatures": "AVX; AVX2; Intel AVX; Intel AVX2; Intel AVX512; Intel Turbo",
      "servicecode": "AmazonEC2",
      "licenseModel": "No License requirequired",
      "currentGeneration": "Yes",
      "preInstalledSw": "SQL Std",
      "location": "Asia Pacific (Osaka)",
      "processorArchitecture": "64-bit",
      "marketoption": "OnDemand",
      "operation": "RunInstances:0006",
      "availabilityzone": "NA"
    },
    "sku": "4YN89ZSXY92DK7Q8"
  },
  "serviceCode": "AmazonEC2",
  "terms": {
    "OnDemand": {
      "4YN89ZSXY92DK7Q8.JRTCKXETXF": {
        "priceDimensions": {
          "4YN89ZSXY92DK7Q8.JRTCKXETXF.6YS6EN2CT7": {
            "unit": "Hrs",
            "endRange": "Inf",
            "description": "$0.7712 per On Demand Windows with SQL Std t3.xlarge Instance Hour",
            "appliesTo": [],
            "rateCode": "4YN89ZSXY92DK7Q8.JRTCKXETXF.6YS6EN2CT7",
            "beginRange": "0",
            "pricePerUnit": {
              "USD": "0.7712000000"
            }
          }
        },
        "sku": "4YN89ZSXY92DK7Q8",
        "effectiveDate": "2025-04-01T00:00:00Z",
        "offerTermCode": "JRTCKXETXF",
        "termAttributes": {}
      }
    },
    "Reserved": {
      "4YN89ZSXY92DK7Q8.MZU6U2429S": {
        (省略)
      },
      (省略)
      "4YN89ZSXY92DK7Q8.4NA7Y494T4": {
        "priceDimensions": {
          "4YN89ZSXY92DK7Q8.4NA7Y494T4.6YS6EN2CT7": {
            "unit": "Hrs",
            "endRange": "Inf",
            "description": "Windows with SQL Server Standard (Amazon VPC), t3.xlarge reserved instance applied",
            "appliesTo": [],
            "rateCode": "4YN89ZSXY92DK7Q8.4NA7Y494T4.6YS6EN2CT7",
            "beginRange": "0",
            "pricePerUnit": {
              "USD": "0.6907000000"
            }
          }
        },
        "sku": "4YN89ZSXY92DK7Q8",
        "effectiveDate": "2025-04-25T22:24:13Z",
        "offerTermCode": "4NA7Y494T4",
        "termAttributes": {
          "LeaseContractLength": "1yr",
          "OfferingClass": "standard",
          "PurchaseOption": "No Upfront"
        }
      },
      (省略)
    }
  },
  "version": "20250425220941",
  "publicationDate": "2025-04-25T22:09:41Z"
}

※料金表で表示した条件以外のリザーブドインスタンス料金表データは省略しています。

料金表ページと同じ金額を取得できました!

注意

filtersオプションでは、"product"の項目しかフィルタできないようです。

今回省略したリザーブドインスタンスの条件を指定してみましたが、"product"以外の項目は反映されていませんでした。

~ $ aws pricing get-products --service-code AmazonEC2 --region us-east-1 --filters \
> Type=TERM_MATCH,Field=regionCode,Value="ap-northeast-3" \
> Type=TERM_MATCH,Field=operatingSystem,Value="Windows" \
> Type=TERM_MATCH,Field=preInstalledSw,Value="SQL Std" \
> Type=TERM_MATCH,Field=tenancy,Value="Shared" \
> Type=TERM_MATCH,Field=instanceType,Value="t3.xlarge" \
> Type=TERM_MATCH,Field=licenseModel,Value="No License requirequired" \
> Type=TERM_MATCH,Field=termType,Value="Reserved" \
> Type=TERM_MATCH,Field=LeaseContractLength,Value="1yr" \
> Type=TERM_MATCH,Field=PurchaseOption,Value="No Upfront" \
> Type=TERM_MATCH,Field=OfferingClass,Value="standard" \
> | jq -r '.PriceList[]' | jq '.'
{
  "product": {
    "productFamily": "Compute Instance",
    "attributes": {
      "enhancedNetworkingSupported": "No",
      "intelTurboAvailable": "Yes",
      "memory": "16 GiB",
      "dedicatedEbsThroughput": "Up to 2780 Mbps",
      "vcpu": "4",
      "classicnetworkingsupport": "false",
      "capacitystatus": "Used",
      "locationType": "AWS Region",
      "storage": "EBS only",
      "instanceFamily": "General purpose",
      "operatingSystem": "Windows",
      "intelAvx2Available": "Yes",
      "regionCode": "ap-northeast-3",
      "physicalProcessor": "Intel Skylake E5 2686 v5",
      "clockSpeed": "3.1 GHz",
      "ecu": "NA",
      "networkPerformance": "Up to 5 Gigabit",
      "servicename": "Amazon Elastic Compute Cloud",
      "gpuMemory": "NA",
      "vpcnetworkingsupport": "true",
      "instanceType": "t3.xlarge",
      "tenancy": "Shared",
      "usagetype": "APN3-BoxUsage:t3.xlarge",
      "normalizationSizeFactor": "8",
      "intelAvxAvailable": "Yes",
      "processorFeatures": "AVX; AVX2; Intel AVX; Intel AVX2; Intel AVX512; Intel Turbo",
      "servicecode": "AmazonEC2",
      "licenseModel": "No License required",
      "currentGeneration": "Yes",
      "preInstalledSw": "SQL Std",
      "location": "Asia Pacific (Osaka)",
      "processorArchitecture": "64-bit",
      "marketoption": "OnDemand",
      "operation": "RunInstances:0006",
      "availabilityzone": "NA"
    },
    "sku": "4YN89ZSXY92DK7Q8"
  },
  "serviceCode": "AmazonEC2",
  "terms": {
    "OnDemand": {
      "4YN89ZSXY92DK7Q8.JRTCKXETXF": {
        "priceDimensions": {
          "4YN89ZSXY92DK7Q8.JRTCKXETXF.6YS6EN2CT7": {
            "unit": "Hrs",
            "endRange": "Inf",
            "description": "$0.7712 per On Demand Windows with SQL Std t3.xlarge Instance Hour",
            "appliesTo": [],
            "rateCode": "4YN89ZSXY92DK7Q8.JRTCKXETXF.6YS6EN2CT7",
            "beginRange": "0",
            "pricePerUnit": {
              "USD": "0.7712000000"
            }
          }
        },
        "sku": "4YN89ZSXY92DK7Q8",
        "effectiveDate": "2025-04-01T00:00:00Z",
        "offerTermCode": "JRTCKXETXF",
        "termAttributes": {}
      }
    },
    "Reserved": {
      "4YN89ZSXY92DK7Q8.MZU6U2429S": {
        "priceDimensions": {
          "4YN89ZSXY92DK7Q8.MZU6U2429S.2TG2D8R56U": {
            "unit": "Quantity",
            "description": "Upfront Fee",
            "appliesTo": [],
            "rateCode": "4YN89ZSXY92DK7Q8.MZU6U2429S.2TG2D8R56U",
            "pricePerUnit": {
              "USD": "17496"
            }
          },
          "4YN89ZSXY92DK7Q8.MZU6U2429S.6YS6EN2CT7": {
            "unit": "Hrs",
            "endRange": "Inf",
            "description": "USD 0.0 per Windows with SQL Server Standard (Amazon VPC), t3.xlarge reserved instance applied",
            "appliesTo": [],
            "rateCode": "4YN89ZSXY92DK7Q8.MZU6U2429S.6YS6EN2CT7",
            "beginRange": "0",
            "pricePerUnit": {
              "USD": "0.0000000000"
            }
          }
        },
        "sku": "4YN89ZSXY92DK7Q8",
        "effectiveDate": "2025-04-25T22:24:11Z",
        "offerTermCode": "MZU6U2429S",
        "termAttributes": {
          "LeaseContractLength": "3yr",
          "OfferingClass": "convertible",
          "PurchaseOption": "All Upfront"
        }
      },
      "4YN89ZSXY92DK7Q8.BPH4J8HBKS": {
        "priceDimensions": {
          "4YN89ZSXY92DK7Q8.BPH4J8HBKS.6YS6EN2CT7": {
            "unit": "Hrs",
            "endRange": "Inf",
            "description": "Windows with SQL Server Standard (Amazon VPC), t3.xlarge reserved instance applied",
            "appliesTo": [],
            "rateCode": "4YN89ZSXY92DK7Q8.BPH4J8HBKS.6YS6EN2CT7",
            "beginRange": "0",
            "pricePerUnit": {
              "USD": "0.6476000000"
            }
          }
        },
        "sku": "4YN89ZSXY92DK7Q8",
        "effectiveDate": "2025-04-25T22:24:14Z",
        "offerTermCode": "BPH4J8HBKS",
        "termAttributes": {
          "LeaseContractLength": "3yr",
          "OfferingClass": "standard",
          "PurchaseOption": "No Upfront"
        }
      },
      (省略)
    }
  },
  "version": "20250425220941",
  "publicationDate": "2025-04-25T22:09:41Z"
}

まとめ

今回はAWS Price List Query APIを使用して料金表データを取得しました。

コマンド1つで最新の料金表データを取得できるので、同じ条件で繰り返し最新料金を確認する場合に便利です。
一方、JSON形式しか対応していないため、複数の料金表データをまとめて比較したい場合や取得した情報を加工して使用する場合には、AWS Price List Bulk APIを使用したほうがよさそうです。

次回は、AWS Price List Bulk APIを使用して、料金表データを取得します。

お知らせ

APCはAWS Advanced Tier Services (アドバンストティアサービスパートナー) 認定を受けております。

その中で私達クラウド事業部はAWSなどのクラウド技術を活用したSI/SESのご支援をしております。

www.ap-com.co.jp

また、一緒に働いていただける仲間も募集中です!
今年もまだまだ組織規模拡大中なので、ご興味持っていただけましたらぜひお声がけください。

www.ap-com.co.jp