LoginSignup
3
1

More than 1 year has passed since last update.

Azure Functions - ローカルでのテスト、デバッグ(Python)

Posted at

目的

Azure Functionsをローカルでテスト、デバッグする方法を確認する

準備

必要なもの

  • Pyhon
  • Azure Functions Core Tools
  • Azure Functions(VS Code拡張機能)
  • Azurite(VS Code拡張機能)

Azure Functions Core Tools

ローカルPC上のターミナルから関数を開発およびテストするためのツール

以下からインストール
Azure Functions Core Tools のインストール
1.png

Azure Functions の VS Code Extension

VSCodeの拡張機能からインストール
2.png

Azurite

3.png

Azure Storage Emulatorを使用しようとしたが、開発とテストに Azure ストレージ エミュレーターを使用するAzuriteが推奨されているので、
VSCodeの拡張機能からAzuriteをインストール
4.png

プロジェクト、関数の作成

プロジェクトの作成

ローカル関数プロジェクトを作成するより以下でプロジェクトを作成(pyFunctionsは任意のプロジェクト名)

ターミナル
$ func init pyFunctions

ランタイムの選択が出てくるので今回はPythonを選択
5.png

関数の作成

関数を作成するより以下で関数を作成

ターミナル
$ func new

関数トリガの選択が出てくるので今回はHTTP triggerAzure Event Grid triggerで作成、
それぞれ関数名をhttpTrigger-testeventGridTrigger-testとした
6.png

指定した関数名のサブフォルダーに関数のコードが生成されるので、今回は変更せず使用する
7.png

関数のテスト

Azuriteの起動

VSCodeのコマンドパレットからAzurite:Startを実行
8.png

関数の起動

関数をローカルで実行するより以下で関数を起動

ターミナル
$ func start

起動すると以下のように出力される
9.png

関数の実行

今回、関数の実行にPostmanを使用

HTTP trigger

HTTP と webhook でトリガーされる関数より以下のURLで関数を実行することができる

URL
http://localhost:{port}/api/{function_name}

以下Postmanの実行結果
10.png

以下ターミナルの出力結果
11.png

Azure Event Grid trigger

HTTP でトリガーされない関数には「HTTP トリガー、Webhook トリガー、Event Grid トリガーを除く、あらゆる種類の関数の場合、管理エンドポイントを呼び出すことによって、関数をローカルでテストできます。」と記載があり、Event Grid トリガの記載はなく、
要求を手動で投稿するに記載がある

以下の設定が必要

  • Content-Type: application/json ヘッダを設定
  • aeg-event-type: Notification ヘッダを設定
  • 要求本文に RequestBin のデータを貼り付け
URL
http://localhost:7071/runtime/webhooks/eventgrid?functionName={FUNCTION_NAME}

要求本文にはイベント スキーマを参考に張り付け

以下Postmanにヘッダ、ボディを設定して実行した結果
12.png
13.png

以下ターミナルの出力結果
14.png

関数のデバッグ

プロジェクトの初期化

拡張機能Azure Functionsを選択し、initialize Project for Use with VS Code...をクリック
15.png

Pythonを選択
16.png

以下初期化完了状態
17.png

デバッグ

以下のAttach to Python Functionsをクリック
18.png

関数が起動したら、関数のテスト時と同様にPostmanから関数を実行するとブレークすることができる
20.png

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