Alternative Architecture DOJO

オルターブースのクラウドネイティブ特化型ブログです。

Azure Functions から Azure Storage Table にマネージド ID で接続できるようになりました

こんにちは、MLBお兄さんこと松村です。
MLB は開幕して2週間となりました。 CHC の鈴木誠也選手も LAA の大谷翔平選手も好調をキープしていますね。


さて少し前のアップデートとなりますが、Azure Functions の Azure Storage Table 拡張機能がアップデートされ、マネージド ID 接続に対応しました。

azure.microsoft.com

Azure Storage Table は安価に大量の構造化データを保管できるため、よく使用します。
アプリケーションのメインのデータストアとして使用されることはあまりないと思いますが、Azure Functions と組み合わせたバッチプログラムでのデータストア用途として適しています。

docs.microsoft.com

Azure Storage Table 拡張機能のリリースにより、Azure Cosmos DB など他のデータストア同様、Azure Functions の入力バインド・出力バインドとして Table にアクセスできるようになりました。
また Azure Functions から Table へのアクセスでは、従来の接続文字列による接続だけでなく、マネージドID での接続ができるようになりました。

準備

拡張機能

Table へのマネージドID接続をするには、こちらの拡張機能を使用します。
どうやら4月11日にバージョン 1.0.0 がリリースされているようです。

www.nuget.org

マネージド ID とロールの構成

他のリソースと同様、Azure Functions マネージド ID を有効にし、対象のストレージにロールを割り当てます。
なお、ロールによって適しているバインドは異なります。

ロール 入力バインド 出力バインド
ストレージ テーブル データ共同作成者
ストレージ テーブル データ閲覧者

接続文字列の構成

Azure Functions のアプリケーション設定に、マネージド ID 接続対象のストレージのエンドポイントを指定します。
例えば Azure Functions で MyTableService という接続名を使用している場合、以下のように構成します。

[
  {
    "name": "MyTableService__endpoint",
    "value": "https://{your storage name}.table.core.windows.net",
    "slotSetting": (true or false)
  }
]

入力バインド

docs.microsoft.com

まず、Table からデータを取得するための入力バインドを試します。

Table への入力バインドの定義は2種類の方法があります。いずれも関数の属性付き引数として定義します。

  1. 単一のエンティティを取得する方法
    • [Table("table-name", "partition-key", "row-key", Connection = "connection-name")] TableEntity entity
    • PartitionKey および RowKey に合致するデータを取得することができます
  2. TableClient を使用する方法
    • [Table("Items", Connection = "connection-name")] TableClient tableClient
    • TableClient でクエリを実行することができます

Table に対して Azure Functions のマネージド ID に適切なロールが設定されていない場合、関数の実行時に以下のようなエラーが発生します。

Azure.RequestFailedException: This request is not authorized to perform this operation using this permission.
RequestId:13420ca1-1002-0043-7290-51fc29000000
Time:2022-04-16T12:50:37.3908891Z
Status: 403 (Forbidden)
ErrorCode: AuthorizationPermissionMismatch

Content:
{"odata.error":{"code":"AuthorizationPermissionMismatch","message":{"lang":"en-US","value":"This request is not authorized to perform this operation using this permission.\nRequestId:13420ca1-1002-0043-7290-51fc29000000\nTime:2022-04-16T12:50:37.3908891Z"}}}

Headers:
Cache-Control: no-cache
Transfer-Encoding: chunked
Server: Windows-Azure-Table/1.0,Microsoft-HTTPAPI/2.0
x-ms-request-id: 13420ca1-1002-0043-7290-51fc29000000
x-ms-client-request-id: a30cf20c-d334-49f0-b06f-6c0bc7180622
x-ms-version: REDACTED
X-Content-Type-Options: REDACTED
Date: Sat, 16 Apr 2022 12:50:37 GMT
Content-Type: application/json; odata=minimalmetadata; streaming=true; charset=utf-8

   at Azure.Data.Tables.TableRestClient.QueryEntityWithPartitionAndRowKeyAsync(String table, String partitionKey, String rowKey, Nullable`1 timeout, QueryOptions queryOptions, CancellationToken cancellationToken)
   at Azure.Data.Tables.TableClient.GetEntitiyInternalAsync[T](Boolean async, String partitionKey, String rowKey, IEnumerable`1 select, CancellationToken cancellationToken)

出力バインド

docs.microsoft.com

次に、Table にデータを書き込むための出力バインドを試します。

Table への出力バインドの定義は2種類の方法があります。いずれも関数の戻り値 (return) とすることで書き込むことができます。

  1. 単一のエンティティを書き込む方法
    • インプロセス:[return: Table("table-name")]
    • アウトプロセス:[TableOutput("table-name", Connection = "connection-name")]
    • 関数の属性として定義します
  2. TableClient を使用する方法
    • 入力バインドで TableClient を使用する時と同じ記載です
    • AddEntity メソッドなどで書き込みます

「接続文字列よりマネージド ID を使用する」という、Azure でのリソース接続方法の基本形がようやく Azure Storage Table にも導入されることになったため、しっかり押さえておきたいところです。