【问题标题】:How to add a default function key with ARM template for a deployment slot in Azure Functions如何使用 ARM 模板为 Azure Functions 中的部署槽添加默认功能键
【发布时间】:2021-09-16 18:16:20
【问题描述】:

我想在 azure 中向我的部署槽添加一个默认功能键。因此,我将它添加到我的模板中,如下所示:

...,
{
   "type": "Microsoft.Web/sites/slots/functions/keys",
   "dependsOn":[
     "[resourceId('Microsoft.Web/sites/slots', variables('apiServiceName'),'deploy')]"
   ],
   "apiVersion": "2018-02-01",
   "name": "[concat(variables('apiServiceName'),'/deploy/default/eventgrid')]",
   "properties": {
       "name": "eventgrid"
   }
},...

不幸的是,我找不到如何让它工作,这是我能找到的最接近没有模板失败的方法(更少的段使模板无效) 现在我得到这个错误:

NotFound:创建或更新功能键时出错。

API 文档解释了它的工作原理,我认为它与我在此处的 ARM 模板相匹配,但我似乎无法弄清楚为什么会出现未找到错误... 有人试过这个用于天蓝色的功能插槽吗? 不过,我可以让它为生产槽工作:

...,
{
    "type": "Microsoft.Web/sites/host/functionKeys",
    "dependsOn":[
      "[resourceId('Microsoft.Web/sites', variables('apiServiceName'))]"
    ],
    "apiVersion": "2018-11-01",
    "name": "[concat(variables('apiServiceName'), '/default/eventgrid')]",
    "properties": {
        "name": "event-grid"
    }
},...

感谢任何帮助。

【问题讨论】:

  • 为什么要使用 ARM 模板添加默认密钥?实际上,它会在您创建插槽后自动创建。
  • 我的意思是一个非功能特定的键,我将它命名为 eventgrid 但它是默认的,所以在所有功能中......因为在部署时这些功能可能不会在那里发布......跨度>

标签: azure azure-functions azure-resource-manager azure-function-app


【解决方案1】:

这对我有用。我认为您在 name 属性中缺少插槽名称:

{
    "type": "Microsoft.Web/sites/slots/host/functionKeys",
    "apiVersion": "2018-11-01",
    "name": "[concat(variables('functionAppName'), '/staging', '/default/key')]",
    "properties": {
        "name": "key",
        "value": "[parameters('key')]"
    },
    "dependsOn": [
        "[resourceId('Microsoft.Web/sites/slots', variables('functionAppName'), 'staging')]"
    ]
},

【讨论】:

  • 是的,你是对的......那里的文档并不完整,因为它指的是 /sites/slots/functions/keys,而实际上它是 /sites/slots/hosts/functionKeys。 ..我得到了 Microsoft 的支持,与我一起查看它,通过更详细地检查并尝试其余的调用,我发现了它......(忘记更新这个问题)......感谢您的回答
【解决方案2】:

我还不能发表评论,所以我不得不写一个答案:-(我想添加一件小事,在使用上述答案时我发现它非常有用。我希望看到这个问题的人可能会发现在此处直接查看此链接很有帮助。

在尝试使用一个 ARM 模板部署多个环境时,我使用了上述答案并通过在参数文件中为每个环境创建一个参数来扩展它:

"parameters": {
        "subscription": {
            "value": "<mySub>"
        },
        "env": {
            "value": "<env>"
        },
        "devHostKey": {
            "reference": {
              "keyVault": {
                  "id": "/subscriptions/<mySubID>/resourceGroups/<myKvRG>/providers/Microsoft.KeyVault/vaults/<KvName>"
              },
              "secretName": "<KeyName>"
            }
        },
        "accHostKey": { .... },
        "prdHostKey": {..... }
    }

然后在我使用的 ARM 模板中

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "subscription": {
      "type": "string"
     },
    "env": {
      "type": "string"
     },
    "devHostKey": {
      "type": "securestring"
     },
    "accHostKey": {
      "type": "securestring"
     },
    "prdHostKey": {
      "type": "securestring"
     }
  },
  "resources":[
    {"type": "Microsoft.Web/sites/slots/host/functionKeys",
      "apiVersion": "2020-12-01",
      "name": "[concat(variables('func_name'),'/', variables('slot_name') ,'/default/default')]",
      "properties": {
        "name": "default",
        "value": "[if(equals(parameters('env'),'dev'),parameters('devHostKey'),if(equals(parameters('env'),'acc'),parameters('accHostKey'),parameters('prdHostKey')))]"
      },
      "dependsOn": [
        "[resourceId('Microsoft.Web/sites/slots', variables('func_name'),variables('slot_name'))]"
      ]
    }
  ]

这样我就可以将这个槽default槽主机密钥部署到我的所有环境中,只需更改我的参数文件中的“env”参数,然后重新运行脚本。

希望这对将来的某人有所帮助:-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多