Azure Functions使うので、初歩的なHTTP Trigger実装方法と少し躓いた点を記録しておきます。
ほとんどの処理をVSCodeで実施しています。
基本的には以下のチュートリアルの内容を実施しています。
Step
0. 前提
環境
Azure Functionsの従量課金プランはWindowsの方がクラウド上にリソース多そうだったので、Windowsにしています。
種類 | Version | 備考 |
---|---|---|
OS | Windows11 24H2 | |
Python | 3.12.8 | 3.13はVS CodeのExtensionで使えなかった |
Poetry | 2.1.3 | |
VS Code | 1.101.2 | |
Azure Functions Core Tools | 4.0.6821 |
VSCode 拡張
種類 | Version | 備考 |
---|---|---|
Azure Functions | 1.17.3 | |
Azurite | 3.34.0 |
Python パッケージ
種類 | Version | 備考 |
---|---|---|
Azure Functions | 1.23.0 |
その他
- Azure上にサブスクリプションとリソースグループ作成済
- ローカルのプロジェクトのディレクトリ作成、Poetryの初期設定済
1. Azure Functions作成
今回はお試しなので従量課金プランを選びます。フレックス従量課金だと、専用コンピューティングでコールドスタートを防止できますが、今回はそのメリットないので従量課金プランにします。
あと注意するのはPythonのバージョンでした。プレビューの3.13を選んでいましたが、VS Codeのローカルテスト時に3.13は使えないというエラーが出たので3.12にしています(作成後に変更可能)。
VS Codeから直接Azure Functionsを作成することも可能です。試すとリソースグループやホスティングオプションなど指定できなかったので、細かく作るのであればAzure Portalから作成した方が無難でしょう(少し試した程度なので指定方法あるかもしれません)。
2. ローカルにプロジェクト作成
コマンドパレットから「Azure Functions: Create New Project」を選択
これで、サンプルのAzure Functionsのプログラムがローカルに作成
主たるPython Scriptはfunction_app.py
です。
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
のパラメータを変えるとキーでの認証が必要になります。
import azure.functions as func
import logging
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
@app.route(route="http_trigger")
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
else:
return func.HttpResponse(
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
status_code=200
)
3. ローカルテスト
3.1. Azurite起動
Azuriteをコマンドパレットから起動します。これで Azure Functionsがローカルで動くわけではなく、動かす前提の処理です。
3.2. Function起動
Run And Debugから Attacch to Python FunctionをすることでLocalでFunctionが開始
確認したわけではないが、Terminalでfunc host start
するのと同じ処理っぽい(もちろん仮想環境有効化している)。
Terminalの出力。よくわからないが、エラーが出ることもあり(エラーが出ても実際には動いている)。
* Executing task: .venv\Scripts\activate ; func host start
Found Python version 3.12.8 (python3).
Azure Functions Core Tools
Core Tools Version: 4.0.6821 Commit hash: N/A +c09a2033faa7ecf51b3773308283af0ca9a99f83 (64-bit)
Function Runtime Version: 4.1036.1.23224
[2025-06-29T06:55:26.557Z] Worker process started and initialized.
Functions:
http_trigger: http://localhost:7071/api/http_trigger
For detailed output, run func with --verbose flag.
[2025-06-29T06:55:31.447Z] Host lock lease acquired by instance ID '000000000000000000000000184A4E01'.
3.3. ブラウザからテスト
http://localhost:7071/api/http_trigger
をブラウザで開くと以下出力。
Termianlには以下が追加される。
[2025-06-29T06:56:27.933Z] Executing 'Functions.http_trigger' (Reason='This function was programmatically called via the host APIs.', Id=024921f2-68eb-409f-8c96-9353ea94956d)
[2025-06-29T06:56:28.021Z] Python HTTP trigger function processed a request.
[2025-06-29T06:56:28.111Z] Executed 'Functions.http_trigger' (Succeeded, Id=024921f2-68eb-409f-8c96-9353ea94956d, Duration=228ms)
3.4. VS CodeのExtensionからテスト
メニューでAzureを開き、WORKSPACEから画面のように実行。
4. デプロイ
5. テスト
5.1. Azure Portalからテスト
関数詳細画面で、「テスト/実行」ボタンを押して、HTTPメソッドにPOSTを選択して本文にJSONを入れて実行。