😀

Azure Functions の http routePrefix を host.json はそのまま環境変数で変更し

に公開

Azure Functions の Http Trigger は、デフォルトで api という routePrefix が付きます。これを、host.json を変更せずに、Azure Functions の環境変数に設定して、routePrefix を変更してみました。

検証用 Azure Functions を用意

bash
prefix=mnrfunc
region=japaneast

az group create \
  --name ${prefix}-rg \
  --location $region

az storage account create \
  --name ${prefix}stor \
  --resource-group ${prefix}-rg \
  --sku Standard_LRS

az functionapp create \
  --name ${prefix} \
  --resource-group ${prefix}-rg \
  --consumption-plan-location $region \
  --runtime dotnet \
  --functions-version 4 \
  --storage-account ${prefix}stor \
  --disable-app-insights \
  --https-only \
  --os-type Linux

func init $prefix --dotnet

cd $prefix

func new --name httpsample --template "HTTP trigger" --authlevel "anonymous"

func azure functionapp publish $prefix

routePrefix 変更前の動作確認

bash
$ curl https://$prefix.azurewebsites.net/api/httpsample

This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.

環境変数に routePrefix を prod に設定

bash
az webapp config appsettings set \
  --name ${prefix} \
  --resource-group ${prefix}-rg \
  --settings AzureFunctionsJobHost__extensions__http__routePrefix=prod

routePrefix 変更後の動作確認

bash
$ curl -I https://$prefix.azurewebsites.net/api/httpsample

HTTP/2 404 
date: Fri, 15 Dec 2023 23:39:44 GMT
server: Kestrel

$ curl https://$prefix.azurewebsites.net/prod/httpsample

This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.

(参考) host.json で routePrefix を設定する場合

host.json
{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            }
        }
    },
    "extensions": {
        "http": {
            "routePrefix": "prod"
        }
    }
}

参考情報

https://learn.microsoft.com/ja-jp/azure/azure-functions/functions-bindings-http-webhook?tabs=isolated-process%2Cfunctionsv2&pivots=programming-language-csharp#hostjson-settings

https://learn.microsoft.com/ja-jp/azure/azure-functions/functions-host-json#override-hostjson-values

Discussion