最近、Azure Functionsのお勉強をチマチマと始めました。色々と分からないことが多かったのでお勉強メモをまとめて記します。
最初の関数を作成する
以下のチュートリアルをやることにする。
以下の環境で実行。とりあえずお手軽そうなTypeScriptで。
- nodejs v14.15.5
- func 3.0.3388
ローカル環境で動作させる
npm start
でローカル環境でアプリが動きます。今回はHTTPトリガーで作っているので、ブラウザでアクセスすると動作が確認できます。
ファイルを修正したら、自動的にリロードされます。
全体概要や引数などのオブジェクトについて
以下のドキュメントに纏まっています。
function.json
について
function.json
はトリガーや出力の定義をする感じ。詳細は以下に書かれています。
Azureにデプロイする
ログイン
az login
コマンドを実行します。ブラウザが開いてログインが求められるので、ログインしたらOK。
リソースグループを作成
az group create
でリソースグループを作成します。
az group create --name リソースグループ名 --location 場所
場所はaz account list-locations
コマンドで得られるName
の値を指定する...と思っていたんだけれども、なんか使えないものもリストアップされる。
az account list-locations -o table
DisplayName Name RegionalDisplayName
------------------------ ------------------- -------------------------------------
East US eastus (US) East US
East US 2 eastus2 (US) East US 2
South Central US southcentralus (US) South Central US
West US 2 westus2 (US) West US 2
Australia East australiaeast (Asia Pacific) Australia East
Southeast Asia southeastasia (Asia Pacific) Southeast Asia
North Europe northeurope (Europe) North Europe
UK South uksouth (Europe) UK South
West Europe westeurope (Europe) West Europe
Central US centralus (US) Central US
North Central US northcentralus (US) North Central US
West US westus (US) West US
South Africa North southafricanorth (Africa) South Africa North
Central India centralindia (Asia Pacific) Central India
East Asia eastasia (Asia Pacific) East Asia
Japan East japaneast (Asia Pacific) Japan East
Korea Central koreacentral (Asia Pacific) Korea Central
Canada Central canadacentral (Canada) Canada Central
France Central francecentral (Europe) France Central
Germany West Central germanywestcentral (Europe) Germany West Central
Norway East norwayeast (Europe) Norway East
Switzerland North switzerlandnorth (Europe) Switzerland North
UAE North uaenorth (Middle East) UAE North
Brazil South brazilsouth (South America) Brazil South
Central US (Stage) centralusstage (US) Central US (Stage)
East US (Stage) eastusstage (US) East US (Stage)
East US 2 (Stage) eastus2stage (US) East US 2 (Stage)
North Central US (Stage) northcentralusstage (US) North Central US (Stage)
South Central US (Stage) southcentralusstage (US) South Central US (Stage)
West US (Stage) westusstage (US) West US (Stage)
West US 2 (Stage) westus2stage (US) West US 2 (Stage)
Asia asia Asia
Asia Pacific asiapacific Asia Pacific
Australia australia Australia
Brazil brazil Brazil
Canada canada Canada
Europe europe Europe
Global global Global
India india India
Japan japan Japan
United Kingdom uk United Kingdom
United States unitedstates United States
East Asia (Stage) eastasiastage (Asia Pacific) East Asia (Stage)
Southeast Asia (Stage) southeastasiastage (Asia Pacific) Southeast Asia (Stage)
Central US EUAP centraluseuap (US) Central US EUAP
East US 2 EUAP eastus2euap (US) East US 2 EUAP
West Central US westcentralus (US) West Central US
West US 3 westus3 (US) West US 3
South Africa West southafricawest (Africa) South Africa West
Australia Central australiacentral (Asia Pacific) Australia Central
Australia Central 2 australiacentral2 (Asia Pacific) Australia Central 2
Australia Southeast australiasoutheast (Asia Pacific) Australia Southeast
Japan West japanwest (Asia Pacific) Japan West
Korea South koreasouth (Asia Pacific) Korea South
South India southindia (Asia Pacific) South India
West India westindia (Asia Pacific) West India
Canada East canadaeast (Canada) Canada East
France South francesouth (Europe) France South
Germany North germanynorth (Europe) Germany North
Norway West norwaywest (Europe) Norway West
Switzerland West switzerlandwest (Europe) Switzerland West
UK West ukwest (Europe) UK West
UAE Central uaecentral (Middle East) UAE Central
Brazil Southeast brazilsoutheast (South America) Brazil Southeast
例えばjapan
ってものを指定すると、not available
って出てしまう。
$ az group create --name miyohide1 --location japan
The provided location 'japan' is not available for resource group. List of available regions is 'centralus,eastasia,southeastasia,eastus,eastus2,westus,westus2,northcentralus,southcentralus,westcentralus,northeurope,westeurope,japaneast,japanwest,brazilsouth,australiasoutheast,australiaeast,westindia,southindia,centralindia,canadacentral,canadaeast,uksouth,ukwest,koreacentral,koreasouth,francecentral,southafricanorth,uaenorth,australiacentral,switzerlandnorth,germanywestcentral,norwayeast,australiacentral2'.
日本リージョンではjapaneast
とかjapanwest
とかを指定すれば良いかなと思います。
ストレージアカウントの作成
Azure Functionsの実行プログラムを格納する場所を作成します。
az storage account create --name ストレージアカウント名 --location 場所 --resource-group リソースグループ名
Azure Functionsを作成する
az functionapp createコマンドを使ってAzure Functionsアプリを作成します。細かいオプションはリンク先を参照。
az functionapp create --resource-group リソースグループ名 --consumption-plan-location japaneast --runtime node --runtime-version 14 --functions-version 3 --name AzureFunctionsアプリ名 --storage-account ストレージアカウント名
上記のコマンドで、以下の環境が構築されます。
- 実行環境はNode.js 14.x系
- OSはWindows
- 従量課金プラン
- 作成と同時にApplication Insightsが作成
ビルド
本番環境用に手元にあるソースをビルドします。
npm run build:production
アプリをAzureに送信する
Azure Functions Core Toolsのコマンドであるfunc azuzre functionapp publishを実行して、ローカルにあるコードをAzureに送信します。
func azure functionapp publish AzureFunctionsアプリ名
こんな結果が表示されます。
Getting site publishing info...
Creating archive for current directory...
Uploading 10.8 KB [###########################################################]
Upload completed successfully.
Deployment completed successfully.
Syncing triggers...
Functions in miyohidefuncapp1:
HttpExample - [httpTrigger]
Invoke url: アクセス先URL
Invoke url
に?code=
からはじまる長い文字列がついているのは、関数のアクセスキーという機能がついているため。ローカルではこの値にかかわらずアクセスできるが、Azureに送信したアプリを動かすにはこのcode
の値が一致していないと動かない(401エラーが出る)。
ログを見る
Azure Functionsを作成したらそれに紐づいてApplication Insightsも作られているので、いちいち無効化してなければログをみることができます。
クエリのところで何を入れれば良いかがよく分からなかったんだけれども、traces
にアプリでcontext.log
の内容が見れました。
出力される項目がたくさんあるので、見たい項目を絞りたい場合はproject
で見たい項目だけを指定すれば良いようです。
docs.microsoft.com
具体的には以下のような記述をしました。
traces
| project timestamp, message, operation_id
ここでは日時とメッセージのほかoperation_id
というのを指定。これが同じものは同じリクエストでの処理っぽい。
requests
のduration
でその関数の実行時間が分かるみたい。
今回はNode.jsで動かしたんだけれども、20分ぐらい処理がないと、次の処理に時間がかかる感じ。今回の例では、250ms以内で納まっていたものが、一気に4.6秒に跳ね上がった。おそらくこの内容に該当している。
Understanding serverless cold start
今回はNode.jsだったんだけれども、Javaならどれぐらいかかるのか、Linuxならどうなのかを実験したい。
テストを書く
Azure Functionsにおけるテストの書き方については、Azure Functions のコードをテストするための戦略に記述があります。
上記のドキュメントはJavaScriptの例でTypeScriptはまた違う気がしたので、他の資料を探しました。結果、TypeScript Deep Dive日本語版のJestの記事を参考にしました。
細かい話ですが、log
とlog.info
の両方をモック化したい場合にどういうふうにテストを実装すれば良いか分からなかったのですが、StackoverflowにそのまんまのQAがありました。
stackoverflow.com
ソース
試したソースは以下に格納しました。
github.com