Azure Functions Get Key from Terraform without InternalServerError
So you’re trying to use the Terraform azurerm_function_app_host_keys resource to get the keys from an Azure function after deployment. Sadly, as of 03/2021, this can fail intermittently 😢 (See issue 1and 2).+
Error making Read request on AzureRM Function App Hostkeys “***”: web.AppsClient#ListHostKeys: Failure responding to request: StatusCode=400 — Original Error: autorest/azure: Service returned an error. Status=400 Code=”BadRequest” Message=”Encountered an error (ServiceUnavailable) from host runtime”
Error: Error making Read request on AzureRM Function App Hostkeys “somefunx”: web.AppsClient#ListHostKeys: Failure responding to request: StatusCode=400
Looking into the errors returned by the azurerm_function_app_host_keys resource they’re intermittent and look like they’re related to a timing issue. Did you know the curl command support retrying out of the box?
So using the Shell provider we can create a simple script to make the REST request to the Azure API and use curls inbuilt retry support to have the request retried with an exponential back-off until it succeeds or 5mins is up!
Warning: This script uses –retry-all-errors which is only available inv7.71 and above. The version shipped with the distro your using might not be up-to-date user curl --version to check.
Here is a rough example of what you end up with:
terraform {
required_providers {
shell = {
source ="scottwinkler/shell"
version ="1.7.7"
}
}
}
resource"azurerm_function_app""functions" {
name ="${var.function_name}–${var.random_string}-premium"