简体   繁体   中英

How to add a default function key with ARM template for a deployment slot in Azure Functions

I want to add a default function key to my deployment slot in azure. Therefore I've added it to my template like this:

...,
{
   "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"
   }
},...

Unfortunately, I cannot find how to get it to work, this is the closest I can find to not have the template fail (fewer segments make the template invalid) Now I get this error:

NotFound: Error creating or updating function key.

The API docs explain how it works, and I think it matches what I have here for the ARM template, but I can't seem to figure out why I get a not found error... Anyone ever tried this for an azure function slot? I can get it to work for the production slot though:

...,
{
    "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"
    }
},...

Any help is appreciated.

This worked for me. I think you're missing the slot name in the name property:

{
    "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')]"
    ]
},

I can't yet comment, so I am force to write an answer:-( I would like to add one small thing that I found super helpful when using the above answer. I expect that someone looking at this question might find it helpful to see this linked directly here.

While trying to deploy multiple environments with one ARM template I used the above answer and extended it by creating one parameter per environment in the parameter file:

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

Then in my ARM template I used

{
  "$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'))]"
      ]
    }
  ]

This way I can deploy this slot default slot Host key to all my environments just by changing the "env" parameter in my parameter file, and then rerunning the script.

Hope this helps someone in the future:-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM