簡體   English   中英

如何在 ARM 模板中創建一個天藍色的功能鍵?

[英]how to create an azure function key in ARM template?

我一整天都在為此苦苦掙扎,我試圖從 ARM 模板創建一個 Function App 功能鍵。

到目前為止,我已經能夠使用以下模板在主機級別創建我的功能鍵:

    {
      "type": "Microsoft.Web/sites/host/functionKeys",
      "apiVersion": "2018-11-01",
      "name": "[concat(parameters('appServiceName'), '/default/PortalFunctionKey')]",
      "properties": {
        "name": "PortalFunctionKey"
      }

然后我找到了幾篇文章和鏈接,顯示它可以通過 API 實現: https : //github.com/Azure/azure-functions-host/wiki/Key-management-API

我能夠通過此 API 發布到: https://{myfunctionapp}.azurewebsites.net/admin/functions/{MyFunctionName}/keys/{NewKeyName}?code={_masterKey}

但是我不能為了我弄清楚如何在我的 ARM 模板中做到這一點! 我嘗試了各種類型和名稱的組合,例如:

    {
      "type": "Microsoft.Web/sites/host/functionKeys",
      "apiVersion": "2018-11-01",
      "name": "[concat(parameters('appServiceName'), '/{myfunctionName}/PortalFunctionKey')]",
      "properties": {
        "name": "PortalFunctionKey"
      }

或 /functions/{myfunctionName}/PortalFunctionKey 如某些文章中所建議的那樣,但我無法使用任何內容,也無法在 ARM Microsoft.Web/sites/host/functionKeys 上找到太多文檔。

有沒有人成功在 ARM 模板中創建 FUNCTION 鍵(不是主機)? 我很高興聽到你是如何到達那里的:)!

基本上: 功能鍵

提前謝謝了,

伊曼紐爾

這現在可能會解決問題: https : //docs.microsoft.com/en-us/azure/templates/microsoft.web/2020-06-01/sites/functions/keys

我能夠通過傳遞如下所示的 ARM 模板資源來創建功能鍵:

{
    "type": "Microsoft.Web/sites/functions/keys",
    "apiVersion": "2020-06-01",
    "name": "{Site Name}/{Function App Name}/{Key Name}",
    "properties": {
        "value": "(optional)-key-value-can-be-passed-here"
    }
}

似乎即使您在屬性中沒有“值”,也必須至少傳遞一個空的屬性對象。

要解決這個問題,你可以參考這個 github issue。 第一個問題第二個問題,這些問題都是關於如何在ARM模板中獲取功能鍵的問題。

現在獲取鍵值的示例如下所示:

"properties": {
        "contentType": "text/plain",
        "value": "[listkeys(concat(variables('functionAppId'), '/host/default/'),'2016-08-01').functionKeys.default]"
        }

或使用下面來獲取關鍵對象。

"functionkeys": {
            "type": "object",
            "value": "[listkeys(concat(variables('functionAppId'), '/host/default'), '2018-11-01')]"                                                                                }
    }

你可以試試,希望對你有幫助。

因此目前無法在 ARM 模板中創建函數級別的函數鍵。

因此,我創建了一個功能請求,如果您對此感興趣,可以對其進行投票: https : //feedback.azure.com/forums/169385-web-apps/suggestions/39789043-create-function-level-keys-for-azure -功能輸入

不過現在,我們正在通過 powershell 部署任務步驟創建功能級功能鍵。 這是如何。

將輸出參數添加到您的 ARM 模板:

  "outputs": {
    "masterKey": {
      "type": "string",
      "value": "[listkeys(concat(resourceId(resourceGroup().name, 'Microsoft.Web/sites', parameters('appServiceName')), '/host/default'), '2018-11-01').masterKey]"
    },
    "appServiceName": {
      "type": "string",
      "value": "[parameters('appServiceName')]"
    },
    "functionKeys": {
      "type": "array",
      "value": [
        {
          "functionName": "myFunctionName",
          "keys": [ "FunctionKeyName1", "FunctionKeyName2" ]
        }
      ]
    }
  }

將以下 PS 腳本文件添加到您的部署項目中:

param (
    [Parameter(Mandatory=$true)]
    [string]
    $armOutputString
)

Write-Host $armOutputString
$armOutputObj = $armOutputString | convertfrom-json
Write-Host $armOutputObj

$masterKey = $armOutputObj.masterKey.value
$appServiceName = $armOutputObj.appServiceName.value

$httpHeaders = @{
    "x-functions-key" = $masterKey
}
$contentType = "application/json; charset=utf-8"

foreach($function in $armOutputObj.functionKeys.value){

    $retryCount = 5;
    while ($true) {
        try {
            $uriBase = "https://$($appServiceName).azurewebsites.net/admin/functions/$($function.functionName)/keys"
            $existingKeys = Invoke-RestMethod -Method Get -Uri $uriBase -Headers $httpHeaders -ContentType $contentType
            break;
        }
        catch {
            if ($_.Exception.Response.StatusCode.value__ -eq 502) {
                if ($retryCount-- -eq 0) {
                    throw;
                }
                else {
                    Write-Output ("Retry" + ": " + $_.Exception.Response.StatusCode + "; attempts=$retryCount")
                    [System.Threading.Thread]::Sleep(1000);
                    continue;
                }
            }
            else {
                throw;
            }
        }
    }

    foreach ($keyname in $function.keys){
        $keyExists = 0
        foreach($exstingKey in $existingKeys.keys){
            if($exstingKey.name -eq $keyname){
                $keyExists = 1
               Write-Host  "key $($keyname) already exists"
            }
        }

        if($keyExists -eq 0){
            $uri = "$($uriBase)/$($keyname)?code=$($masterKey)";
            Write-Host  "Adding $($keyname) key"
            Invoke-RestMethod -Method Post -Uri "$($uriBase)/$($keyname)" -Headers $httpHeaders -ContentType $contentType;
        }
    }
}

該腳本確保密鑰在已經存在的情況下不會被覆蓋,並在失敗時重試(該函數必須存在並且正在運行,API 才能工作)。

在“ Azure 資源組部署”任務中,在“高級”下將“部署輸出”設置為“ armDeployOutput ”。

在此處輸入圖片說明

添加 Powershell 任務、部署項目中 powershell 文件的腳本路徑,並將“參數”設置為“ -armOutputString '$(armDeployOutput)' ”。

在此處輸入圖片說明

就是這樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM