0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

👀概要

AzureでAPIを構築・公開する際に使用されるAzure API Managementについて、実際の操作画面を交えながら解説します。
AWS API Gatewayとの比較を交えながら基本概念を整理しました。

👨‍👩‍👧‍👦対象者(Who)

  • Azureを初めて触る方(特にAWS経験者)
  • インフラエンジニア、クラウドエンジニア

対象外

  • 開発者ポータルの機能は解説しません。

📌関連リンク

🗒️目次

  • Azure API Managementとは?
  • Azure API Managementの構成要素
    • 基本機能
    • エンドポイントごとの処理先の振り分け
    • 製品
    • サブスクリプション
  • ポリシー(Policy)の概念

📝 内容

Azure API Managementとは?

Azure API Managementは、APIを構築・公開するためのAzureのマネージドサービスで、APIの公開、保護、監視、分析を一元的に行うことが可能です。
AWSではAmazon API Gatewayに該当するサービスです。

Azure API Managementの構成要素

基本機能

実際のAPIリクエストを受け取り、バックエンドに転送する実行エンジンです。

主な機能

  • URLの発行
  • エンドポイントごとの処理先の振り分け
  • サブスクリプションによる認証機能
  • キャッシング
  • レート制限の実行

Azure API Managementの構築が完了すると以下のようなコンソール画面が確認でき、その中にはリソース名に応じたゲートウェイのURL(以下の例だと https://test-minegishi.azure-api.net)が表示されます。

Azure コンソール画面

価格レベル

Azure API Managementではスペックと提供される機能に応じた「価格レベル」を選択することが可能です。
主な違いはサービスレベルで、提供するサービスがPoC段階なのか、本番リリース段階なのかに応じて選択してください。

各価格レベルとその性能は以下のリンクより確認可能です。

https://learn.microsoft.com/en-au/azure/api-management/upgrade-and-scale

Azure コンソール画面

過去に私が関わったAzureを使用したプロダクトでは、PoC段階のプロダクトであったため、Developer段階の価格レベルを選択してました。

エンドポイントごとの処理先の振り分け

APIs タブ → API にアクセスすると、そのAzure API Managementリソースが公開するAPIのエンドポイントごとの振り分け処理が確認できます。
デフォルトでは「Echo API」というサンプルAPIが作成されており、動作確認に使用できます。

Azure コンソール画面

上記のエンドポイントでは、/resource への GET メソッドによるアクセス時の処理フローが記述されています。
/resource エンドポイントは param1param2 の2つのクエリパラメータを受け取り、以下のようなレスポンスを返却します。

HTTP/1.1 200 OK
accept: */*
accept-encoding: gzip,deflate,br
accept-language: ja,en;q=0.9,en-GB;q=0.8,en-US;q=0.7
cache-control: no-cache, no-store
client-ip: 4.241.156.150:4104
content-length: 0
date: Mon, 06 Jul 2026 04:15:19 GMT
disguised-host: echo.playground.azure-api.net
host: echo.playground.azure-api.net
max-forwards: 10
ocp-apim-subscription-key: xxxxxxxxxxxxxxxxxxx
referer: https://apimanagement.hosting.portal.azure.net/
sec-ch-ua: "Not(A:Brand";v="8","Chromium";v="144","Microsoft Edge";v="144"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: cross-site
vary: Origin
was-default-hostname: echoapiservicewebapp.azurewebsites.net
x-appservice-proto: https
x-arr-log-id: dc8008e9-79fb-4425-a35c-da1af96ead7d
x-arr-ssl: 2048|256|CN=GeoTrust TLS RSA CA G1,OU=www.digicert.com,O=DigiCert Inc,C=US|CN=echo.playground.azure-api.net
x-forwarded-for: 119.171.161.24,24.239.141.22,13.91.254.72,4.241.156.150:4104
x-forwarded-proto: https
x-forwarded-tlsversion: 1.2
x-original-url: /api/resource?param1=sample
x-powered-by: Azure API Management - http://api.azure.com/,ASP.NET
x-site-deployment-id: EchoApiServiceWebApp
x-waws-unencoded-url: /api/resource?param1=sample

動作確認は、Testタブから確認が可能です。

Azure コンソール画面

製品

複数のAPIをグルーピングし、公開する単位 をプロダクト(製品)と呼びます。
APIsタブ > 製品タブ から確認が可能で、デフォルトではStarter , Unlimited が作成されています。

Azure コンソール画面

Starterをクリックすると、先ほど紹介したEcho APIと結びついていることが確認できます。
このEcho API には後述するサブスクリプションを用いる方法で、すでにアクセスが可能になっています。

Azure コンソール画面

この製品は AWS API Gatewayのステージに相当する概念で、この 製品を作成し公開しない限り、Azure API Managementのエンドポイントを修正しても本番URLには反映されません。

サブスクリプション

API利用者がAPIにアクセスするための 認証キー を発行する仕組みです。
サブスクリプションには Primary KeySecondary Key が発行され、キーローテーション時にダウンタイムなしで切り替えが可能です。

注意: Azureサブスクリプションとは別概念

ここで言う「サブスクリプション」は、Azure API Management固有の概念です。
先述したAzureの課金単位である「サブスクリプション」とは全く異なります。

  • Azureサブスクリプション: 課金の単位(AWS アカウントIDに相当)
  • Azure API Management サブスクリプション: APIアクセスキーの管理単位

同じ用語が異なる文脈で使われるため、初学者は混乱しやすいポイントです。

先ほど紹介したEcho APIに、このキーを用いてアクセスしてみます。 APIs > サブスクリプション にアクセスしてみます。

Azure コンソール画面

製品 Starter と結びついた サブスクリプションの3点リーダーを押し、キーの表示/非表示を押すとサブスクリプションキーの確認が可能です。

このキーを用いて以下のように Ocp-Apim-Subscription-Keyヘッダーを用いることで、 どこからでもアクセスが可能です。

curl -X GET "https://test-minegishi.azure-api.net/echo/resource-cached?param1=sample" -I -H "Ocp-Apim-Subscription-Key: xxxxxxxxxxxxxxxxxxxx2dfe0f6af0"
HTTP/1.1 200 OK
Content-Length: 0
Date: Mon, 06 Jul 2026 04:21:53 GMT
Server: Microsoft-IIS/10.0
Cache-Control: no-store, must-revalidate, no-cache
Expires: Mon, 06 Jul 2026 04:21:53 GMT
Pragma: no-cache
Set-Cookie: ARRAffinity=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;Path=/;HttpOnly;Secure;Domain=echo.playground.azure-api.net
Set-Cookie: ARRAffinitySameSite=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;Path=/;HttpOnly;SameSite=None;Secure;Domain=echo.playground.azure-api.net
Accept: */*
Host: echo.playground.azure-api.net
Max-Forwards: 10
User-Agent: curl/8.20.0
Ocp-Apim-Subscription-Key: xxxxxxxxxxxxxxxxxxx
X-Forwarded-For: xx.xx.xx.xx
X-ARR-LOG-ID: xxxx-xxxx-xx-xxxxx
CLIENT-IP: xx.xx.xx.xx
DISGUISED-HOST: echo.playground.azure-api.net
X-SITE-DEPLOYMENT-ID: EchoApiServiceWebApp
WAS-DEFAULT-HOSTNAME: echoapiservicewebapp.azurewebsites.net
X-Forwarded-Proto: https
X-AppService-Proto: https
X-ARR-SSL: 2048|256|CN=GeoTrust TLS RSA CA G1,OU=www.digicert.com,O=DigiCert Inc,C=US|CN=echo.playground.azure-api.net
X-Forwarded-TlsVersion: 1.2
X-Original-URL: /api/resource?param1=sample
X-WAWS-Unencoded-URL: /api/resource?param1=sample
X-Powered-By: Azure API Management - http://api.azure.com/,ASP.NET

ポリシー(Policy)の概念

Azure API Managementの最も強力な機能が ポリシー です。
ポリシーはXML形式で記述され、リクエスト/レスポンスの各フェーズで処理をカスタマイズできます。

Echo API/resource に対する GET リクエストのポリシーを見てみましょう。

ポリシーは 各エンドポイントの Code Editor ボタンから確認可能です。

Azure コンソール画面

<!--
    - Policies are applied in the order they appear.
    - Position <base/> inside a section to inherit policies from the outer scope.
    - Comments within policies are not preserved.
-->
<!-- Add policies as children to the <inbound>, <outbound>, <backend>, and <on-error> elements -->
<policies>
    <!-- Throttle, authorize, validate, cache, or transform the requests -->
    <inbound>
        <base />
    </inbound>
    <!-- Control if and how the requests are forwarded to services  -->
    <backend>
        <base />
    </backend>
    <!-- Customize the responses -->
    <outbound>
        <base />
    </outbound>
    <!-- Handle exceptions and customize error responses  -->
    <on-error>
        <base />
    </on-error>
</policies>

このコードは実際には何もしていない初期状態であるため、以下のコードに書き換えてみます。

<policies>
    <inbound>
        <base />
        <rate-limit calls="100" renewal-period="60" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
        <set-header name="X-Custom-Header" exists-action="override">
            <value>MyValue</value>
        </set-header>
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

このポリシーを適応して数分待つと、以下のようにヘッダーX-Custom-Headerが確認できます。

curl -i GET "https://test-minegishi.azure-api.net/echo/resource?param1=sample" -H "Ocp-Apim-Subscription-Key: xxxxxxxxxxxxxxxxxx"
curl: (6) Could not resolve host: GET
HTTP/1.1 200 OK
Content-Length: 0
Date: Mon, 06 Jul 2026 04:56:30 GMT
Server: Microsoft-IIS/10.0
Set-Cookie: ARRAffinity=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;Path=/;HttpOnly;Secure;Domain=echo.playground.azure-api.net
Set-Cookie: ARRAffinitySameSite=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;Path=/;HttpOnly;SameSite=None;Secure;Domain=echo.playground.azure-api.net
Accept: */*
Host: echo.playground.azure-api.net
Max-Forwards: 10
User-Agent: curl/8.20.0
Ocp-Apim-Subscription-Key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
X-Forwarded-For: xx.xx.xx.xx
X-ARR-LOG-ID: xxxxxxx-xxxx-xxxx-xxxxxxxxxxxx
CLIENT-IP: xx.xx.xx.xx
DISGUISED-HOST: echo.playground.azure-api.net
X-SITE-DEPLOYMENT-ID: EchoApiServiceWebApp
WAS-DEFAULT-HOSTNAME: echoapiservicewebapp.azurewebsites.net
X-Forwarded-Proto: https
X-AppService-Proto: https
X-ARR-SSL: 2048|256|CN=GeoTrust TLS RSA CA G1,OU=www.digicert.com,O=DigiCert Inc,C=US|CN=echo.playground.azure-api.net
X-Forwarded-TlsVersion: 1.2
X-Original-URL: /api/resource?param1=sample
X-WAWS-Unencoded-URL: /api/resource?param1=sample
X-Powered-By: Azure API Management - http://api.azure.com/,ASP.NET
X-Custom-Header: MyValue

このように、レスポンスの最後に X-Custom-Header: MyValue が追加されていることが確認できます。

まとめ

  • Azure API Management は、APIの公開・保護・監視を一元管理するマネージドサービス(AWS API Gatewayに相当)
  • 主要な構成要素は以下の4つ:
    • ゲートウェイURL: リソース作成時に自動発行される公開エンドポイント
    • 製品(Product): APIをグルーピングして公開する単位(AWS API Gatewayのステージに相当)
    • サブスクリプション: APIアクセスキーの管理単位(Azureサブスクリプションとは別概念)
    • ポリシー: XML形式でリクエスト/レスポンスをカスタマイズ(レート制限、ヘッダー追加など)
  • ポリシー<inbound><backend><outbound><on-error> の4つのセクションで構成され、保存後すぐに反映される
  • 価格レベル はPoC段階ではDeveloper、本番環境ではBasic以上を選択することが推奨される
  • デフォルトで作成される Echo API を使って、すぐに動作確認が可能
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?