title: Azure Function ARM Deployment with listkeys results in BadRequest Error
body: <p>I have a simple ARM template that deploys two Azure Functions, an App Service Plan and a Storage Account:</p>
  
  <p><a href="https://i.stack.imgur.com/zkxXK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zkxXK.png" alt="enter image description here"></a></p>
  
  <p>The only "special" thing is, that the function <code>function-key-issue-two</code> adds the <strong>default host key</strong> from the function <code>function-key-issue-one</code> as an app setting:</p>
  
  <pre><code>"FunctionOneKey": "[listkeys(concat(variables('functionTwoAppId'), '/host/default/'),'2016-08-01').functionKeys.default]",
  </code></pre>
  
  <p>If I deploy this template to a <em>new</em> resource group, it works the first time. Every subsequent deployment fails with a <strong>Bad Request</strong> Error on the Resource <strong>function-key-issue-one/default</strong>:</p>
  
  <p><a href="https://i.stack.imgur.com/cBk2U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cBk2U.png" alt="enter image description here"></a></p>
  
  <p>This is how the operation details looks like:</p>
  
  <pre><code>{
      "Code": "BadRequest",
      "Message": "Encountered an error (ServiceUnavailable) from host runtime.",
      "Target": null,
      "Details": [
          {
              "Message": "Encountered an error (ServiceUnavailable) from host runtime."
          },
          {
              "Code": "BadRequest"
          },
          {
              "ErrorEntity": {
                  "Code": "BadRequest",
                  "Message": "Encountered an error (ServiceUnavailable) from host runtime."
              }
          }
      ],
      "Innererror": null
  }
  </code></pre>
  
  <p>If I remove the <code>FunctionOneKey</code> App Setting, the deployment works. Also If I don't specify the App Setting <code>WEBSITE_RUN_FROM_PACKAGE</code>, the deployment also works. </p>
  
  <p>The function code is deployed later using the <code>AzureFunctionApp@1</code> Azure DevOps Task as a Zip package (that is why I set <code>WEBSITE_RUN_FROM_PACKAGE</code> to <code>1</code>).</p>
  
  <hr>
  
  <p><strong>How to reproduce:</strong></p>
  
  <p>The ARM template I am using is available <a href="https://gist.github.com/mjisaak/d335447be4b6e25ce718f8fcc461afcf" rel="nofollow noreferrer">here</a>. 
  You can deploy it using. e. g. the <code>New-AzResourceGroupDeployment</code> cmdlet:</p>
  
  <pre><code>New-AzResourceGroupDeployment -ResourceGroupName 'function-key-issue-rg' -TemplateFile "D:\sources\issues\functionDeployment\azuredeploy.json" -name "azuredeploy-$(New-Guid)"
  </code></pre>
  
  <hr>
  
  <p><strong>Update 1:</strong></p>
  
  <p>The reason for the <code>ServiceUnavailable</code> error is probably because Kudu adds a web.config with a rewrite rule (because I use WEBSITE_RUN_FROM_PACKAGE but don't have deployed the function):</p>
  
  <p><a href="https://i.stack.imgur.com/WaEH3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WaEH3.png" alt="enter image description here"></a></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;configuration&gt;
      &lt;system.webServer&gt;
          &lt;rewrite&gt;
              &lt;rules&gt;
                  &lt;rule name = "Site Unavailable" stopProcessing = "true"&gt;
                      &lt;match url = ".*" /&gt;
                      &lt;action type = "CustomResponse" statusCode = "503" subStatusCode = "0" statusReason = "Site Unavailable" statusDescription = "Could not download zip" /&gt;
                  &lt;/rule&gt;
              &lt;/rules&gt;
          &lt;/rewrite&gt;
      &lt;/system.webServer&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>My next attempt was to prevent Kudu from doing this by setting
  <code>SCM_TOUCH_WEBCONFIG_AFTER_DEPLOYMENT</code> to <code>0</code> (See: <a href="https://github.com/projectkudu/kudu/wiki/Configurable-settings#dont-touch-webconfig-at-the-end-of-the-deployment" rel="nofollow noreferrer">Don't touch web.config at the end of the deployment</a>). And now it looks like, subsequent deployments <em>sometimes</em> succeed:</p>
  
  <p><a href="https://i.stack.imgur.com/YWjBf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YWjBf.png" alt="enter image description here"></a></p>
  
  <p>But still not a reliable solution :-/.</p>
  
  <hr>
  
  <p><strong>Update 2:</strong></p>
  
  <ul>
  <li>Same issue with the Azure Function Runtime <code>~2</code>.</li>
  <li>Switching the Azure Function to Linux also doesn't solve the issue.</li>
  </ul>
  
  <hr>
  
  <p><strong>Update 3:</strong></p>
  
  <ul>
  <li>I opend a <a href="https://github.com/Azure/azure-functions-host/issues/5722" rel="nofollow noreferrer">GitHub issue</a> regarding this topic.</li>
  </ul>
  
  <hr>
  
  <p>Any idea what is wrong here? Any workarounds?</p>