LoginSignup
5
6

More than 5 years have passed since last update.

[Azure Functions] Queue StorageバインドでFunctionから別Functionを呼び出す

Last updated at Posted at 2017-12-13

はじめに

Azure FunctionsのQueue Storageを利用した、あるFunctionから別のFunctionを呼び出す手順を示します。

ソースコード → https://github.com/momotaro98/azure-functions-queue-storage-sample

対象者

  • これからAzure Functionsを利用したサービスを作成したい人
  • Azure FunctionsでのQueue Storageの使い方がよくわかっていない人

前提条件

  • 本記事で記載の実装はC#です。Azure内の設定はその他の言語でも共通です
  • Azureのアカウントを所持
  • Azure Functionsアプリケーションは生成済み

事前準備

Azure FunctionsにてQueue Storageを利用するには下記の内容を行う必要があります。

  • Azure StorageにてQueue Storageを作成する
  • Microsoft Azure Storage Exploreを利用してキューを確認する

Azure StorageにてQueue Storageを作成する

① Azure Portal画面から"Storage Accounts"を選択

② 生成済みのAzure Functionsのアプリと紐付いているStorage Accountを選択する

③ "Queues"を選択し、"+Queue"ボタンから作成したい名前のキューを作る

skitch.png

Microsoft Azure Storage Exploreを利用してキューを確認する

作成したテーブルのコンテンツへアクセスするには、Microsoft Azure Storage Explore(無料)というMicrosoftが提供する、専用のクライアントアプリを利用する必要があります。(Windows, Mac, Linux3つともアプリ有り)

アプリをダウンロードし、Queuesに作成したキューがあるかを確認します。

pic2.png

Queue StorageバインドでFunctionから別Functionを呼び出す

本記事は時間トリガー型のFunctionがQueueトリガーのFunctionを発火するといった構成の2つのFunctionを記述します。

呼び出すFunction

Queue Storageを用いて別のFunctionを呼び出す側のFunctionにて、"Outpus"へQueue Storageを設定します。

pic3.png

pic6.png

実装

function.json
{
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    },
    {
      "type": "queue",
      "name": "outputQueueItem",
      "queueName": "sample-queue",
      "connection": "AzureWebJobsDashboard",
      "direction": "out"
    }
  ],
  "disabled": false
}
run.csx
using System;

public static void Run(TimerInfo myTimer, ICollector<string> outputQueueItem, TraceWriter log)
{
    // Call CalledFunction by using queue
    outputQueueItem.Add("queue-string");
}

呼び出されるFunction

Queue Storageを用いて呼び出される側のFunctionを作成します。

下記図のように新規Function作成にて"QueueTrigger"のFunction形式を選択し、"Queue name"にはStorage Accountsで作成したキュー名を指定します。

pic5.png

実装

呼び出されるFunctionの実装です。

function.json
{
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "sample-queue",
      "connection": "AzureWebJobsDashboard"
    }
  ],
  "disabled": false
}
run.csx
using System;

public static void Run(string myQueueItem, TraceWriter log)
{
    log.Info($"C# Queue trigger function processed: {myQueueItem}");
}

実行

Functionの呼び出し側である、CallerFunction実行します。

C# Queue trigger function processed: queue-string

呼び出し側がキューへあずけたqueue-stringをトリガーとして呼び出され側のFunctionが実行されることが確認できました。

おわりに

Function間のやり取りは今後Durable Functionと呼ばれるプラットフォームが展開されていくようです。
https://docs.microsoft.com/en-us/azure/azure-functions/durable-functions-overview

こちらの動向もおさえていきたいです。

参考

5
6
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
5
6