LoginSignup
2
1

More than 1 year has passed since last update.

Azure API Management - Functions連携

Last updated at Posted at 2021-09-23

目的

以下のように Blank APIOpenAPI からインポートして作成したAPIのBackendにFunctionsを連携させる

image-20210923153521784.png

API Management - Functions連携について

API Management と Functions を下図のように連携する
image-20210924142646271.png

① クライアント → API Management

https://<APIMリソース名>.azure-api.net/<API URL suffix> は以下から確認できる
image-20210924143116203.png

<operationパス>は以下から確認できる
<operationパス>はそのままFunctionsへのリクエストに付与され、Functionsのルーティング設定と整合性が取れている必要がある
image-20210924145639320.png

②,③ API Management → Functions

https://<関数アプリ名>.azurewebsites.net/api/<operationパス>は以下から確認できる
image-20210923160433391.png

<operationパス>のデフォルトは関数名(上図例は関数名がHttpTrigger1のためURLはhttps://<関数アプリ名>.azurewebsites.net/api/HttpTrigger1
<operationパス>は、関数に紐づいたfunction.jsonrouteで変更が可能
対応メソッドはmethodsで指定する(詳細はルーティング設定参照)

以下function.jsonの例

function.json
{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get"
      ],
      "route": "data1/{id:int}"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}

またリクエストにはx-functions-keyヘッダが必須、値はアプリキーを使用する

アプリキーは以下から確認できる

image-20210923161347326.png

API Management の Backend設定

名前付きの値の定義

Functionsのアプリキーを名前付きの値として定義する

image-20210923161804716.png

値には前述したFunctionsのアプリキー(_master, defaultまたは作成したアプリキーの値)を設定する

image-20210923161920331.png

Backendの登録

以下のようにBackendを追加する
種類にAzureリソースから該当のFunctionsを選択し、
ランタイムURL(リダイレクト先URL)はhttps://<関数アプリ名>.azurewebsites.net/apiにする(初期値としてhttps://<関数アプリ名>.azurewebsites.netが入力されるので/apiを追加する)
証明書チェーン、証明書名の検証は☑を外す

image-20210923162453795.png

認可資格情報としてヘッダーにx-functions-keyを追加し、値には定義した名前付きの値を使用する

image-20210923162928441.png

APIの作成

適当なAPI、operationを作成する

image-20210923163247243.png

APIにBackendの紐づけ

以下からInboundポリシーを編集する

image-20210923163752239.png

以下のようにset-backend-serviceポリシーを追加する
backend-idの値は登録したBackendの名前を設定する

<policies>
    <inbound>
        <base />
        <set-backend-service backend-id="test-backend" />  # 追加
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

set-backend-serviceポリシーを設定することで、Backend登録時に設定した<ランタイムURL>/<operationパス>https://<関数アプリ名>.azurewebsites.net/api/<operationパス>)にリダイレクトされる

テスト

結果

image-20210923165708946.png

Functions内容

image-20210923165842073.png

外部からのAPIコール

外部からAPIをコールする場合は、製品にAPIを追加する必要がある

image-20210923170051212.png

2
1
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
2
1