Azure Functionsプロジェクトの作成方法

コマンドプロンプトで作成

必要なツールのインストール

〇Node.js(Windows 64bit版 LTS)
以下のサイトからインストール。
https://nodejs.org/ja/download/

〇Azure Functions Core Tools
コマンドプロンプトで以下のコマンドを実行する。参考

npm i -g azure-functions-core-tools@3 --unsafe-perm true
プロジェクトを作成したいディレクトリに移動して以下のコマンドを実行する。
func init 作成したいプロジェクト名 --実行環境

実行例:

func init SampleFunctions --dotnet
作成したプロジェクトのディレクトリに移動し、以下のコマンドを実行する。
func new -n 作成するファンクション名 -t "テンプレート名" -a "承認レベル"

実行例:

cd SampleFunctions
func new -n HttpTriggerExample -t "HTTP trigger" -a "anonymous"

〇オプションについて

オプション 説明
-n(--name) ファンクション名。
-t(--template) テンプレート名。
-a(--authlevel) 承認レベル。指定可能な認証レベルは承認レベルの違いについて参照。

作成可能なテンプレートの一覧は以下のコマンドで確認可能。

func templates list

上記コマンド実行結果

C# Templates:
  Azure Blob Storage trigger
  Azure Cosmos DB trigger
  Durable Functions activity
  Durable Functions HTTP starter
  Durable Functions orchestrator
  Azure Event Grid trigger
  Azure Event Hub trigger
  HTTP trigger
  IoT Hub (Event Hub)
  Azure Queue Storage trigger
  RabbitMQ trigger
  SendGrid
  Azure Service Bus Queue trigger
  Azure Service Bus Topic trigger
  SignalR negotiate HTTP trigger
  Timer trigger

Custom Templates:
  Azure Blob Storage trigger
  Azure Cosmos DB trigger
  Azure Event Grid trigger
  Azure Event Hub trigger
  HTTP trigger
  IoT Hub (Event Hub)
  Azure Queue Storage trigger
  RabbitMQ trigger
  SendGrid
  Azure Service Bus Queue trigger
  Azure Service Bus Topic trigger
  SignalR negotiate HTTP trigger
  Timer trigger

JavaScript Templates:
  Azure Blob Storage trigger
  Azure Cosmos DB trigger
  Durable Functions activity
  Durable Functions HTTP starter
  Durable Functions orchestrator
  Azure Event Grid trigger
  Azure Event Hub trigger
  HTTP trigger
  IoT Hub (Event Hub)
  Azure Queue Storage trigger
  RabbitMQ trigger
  SendGrid
  Azure Service Bus Queue trigger
  Azure Service Bus Topic trigger
  SignalR negotiate HTTP trigger
  Timer trigger

PowerShell Templates:
  Azure Blob Storage trigger
  Azure Cosmos DB trigger
  Durable Functions activity (preview)
  Durable Functions HTTP starter (preview)
  Durable Functions orchestrator (preview)
  Azure Event Grid trigger
  Azure Event Hub trigger
  HTTP trigger
  IoT Hub (Event Hub)
  Azure Queue Storage trigger
  SendGrid
  Azure Service Bus Queue trigger
  Azure Service Bus Topic trigger
  SignalR negotiate HTTP trigger
  Timer trigger

Python Templates:
  Azure Blob Storage trigger
  Azure Cosmos DB trigger
  Durable Functions activity
  Durable Functions HTTP starter
  Durable Functions orchestrator
  Azure Event Grid trigger
  Azure Event Hub trigger
  HTTP trigger
  Azure Queue Storage trigger
  RabbitMQ trigger
  Azure Service Bus Queue trigger
  Azure Service Bus Topic trigger
  Timer trigger

TypeScript Templates:
  Azure Blob Storage trigger
  Azure Cosmos DB trigger
  Durable Functions activity
  Durable Functions HTTP starter
  Durable Functions orchestrator
  Azure Event Grid trigger
  Azure Event Hub trigger
  HTTP trigger
  IoT Hub (Event Hub)
  Azure Queue Storage trigger
  RabbitMQ trigger
  SendGrid
  Azure Service Bus Queue trigger
  Azure Service Bus Topic trigger
  SignalR negotiate HTTP trigger
  Timer trigger

Powershell Templates:
  RabbitMQ trigger

ローカルでのファンクション起動方法

Functionsプロジェクトのディレクトリに移動して、以下のコマンドを実行する。

func start

起動に成功すると、以下のようにローカルでのファンクションのURLが表示される

Azure Functions Core Tools
Core Tools Version:       3.0.3388 Commit hash: fb42a4e0b7fdc85fbd0bcfc8d743ff7d509122ae
Function Runtime Version: 3.0.15371.0

[2021-04-08T07:37:44.888Z] Found C:\work_n\git\ArchitectureStudy\mvc\SampleFunctions\SampleFunctions.csproj. Using for user secrets file configuration.

Functions:

        HttpExample: [GET,POST] http://localhost:7071/api/HttpTriggerExample

For detailed output, run func with --verbose flag.

ファンクションを停止させる場合はCtrl + c


承認レベルの違いについて

コマンドで指定できる承認レベルは以下の3種類存在する。

承認レベル 説明
Anonymous URLを知っていれば誰でもファンクションを実行できる。
Function 関数アプリ固有もしくは関数固有のAPI キーを指定しないとファンクションを実行できない。
APIキーを指定せずに実行した場合、401(Unauthorized) が返される。
Admin 関数アプリのマスターキーを指定しないとファンクションを実行できない。
APIキーを指定せずに実行した場合や、マスターキー以外のキーを指定して実行した場合は、401(Unauthorized) が返される。

APIキーの種類

キー 説明
マスターキー 各関数アプリに_masterという管理レベルのキーとして存在する。
このキーを削除することや、キーの値を変更することはできない。
ホストキー 関数アプリ内のすべての関数で使用可能なキー。
関数キー 特定の関数でのみ使用可能なキー。

ファンクション実行時のAPIキーの指定方法

ファンクション実行時のAPIキーの指定方法には、クエリパラメータに指定する方法とヘッダーに指定する方法の2種類ある。 
設定例では以下の値を使用する。

項目
関数のURL https://sample-func.azurewebsites.net/api/HttpTriggerExample
APIキー 6Gyuw5RnxjP0sjeidYt6IJksjd67wgKtFTyaCFVb67sBHus3kO8H2wdw==

クエリパラメータに指定する場合(Getリクエスト)

クエリパラメータに指定する際は、codeAPIキーを指定する。
設定例:

GET https://sample-func.azurewebsites.net/api/HttpTriggerExample?code=6Gyuw5RnxjP0sjeidYt6IJksjd67wgKtFTyaCFVb67sBHus3kO8H2wdw==

ヘッダーに指定する場合(Postリクエスト)

ヘッダーに指定する際は、x-functions-keyAPIキーを指定する。
ヘッダー設定例:

x-functions-key:6Gyuw5RnxjP0sjeidYt6IJksjd67wgKtFTyaCFVb67sBHus3kO8H2wdw==