azureterraformconnectionstatusazapi

How to deploy Azure API Connection through Terraform with the status 'connected'


I am using Azure/azapi provider (azapi_resource) to create API connection on Azure. Right now the connection gets deployed with everything I need. However the status of the connector is error until I authorize it through the portal. Given below is the code for my api connection module.

resource "azapi_resource" "createApiConnectionABC" {
  type      = "Microsoft.Web/connections@2016-06-01"
  name      = var.connection_name
  parent_id = var.resource_group.id
  location  = var.resource_group.location
  tags      = var.resource_tags

  body = jsonencode({
    properties = {
      displayName = var.connection_name
      statuses = [
        {
          "status" : "Connected"
        }
      ]
      parameterValues       = {}
      customParameterValues = {}

      api = {
        name        = var.connection_name
        displayName = "Sample Name"
        description = "Sample Description"
        iconUri     = "sample-uri"
        brandColor  = "#0072C6"
        id          = "/subscriptions/${var.subscription_id}/providers/Microsoft.Web/locations/${var.resource_group.location}/managedApis/${var.connection_name}"
        type        = "Microsoft.Web/locations/managedApis"
      }
    }
  })
}

Even though I have set the status as Connected in code, the status of deployed connector is error. Does anybody know a way to deploy the connector with the status Connected when its deploy?


Solution

  • The reason for the Azure API connection resource's status is "error" is that the connection must be authorized before it can be used. This is an Azure security feature that ensures only authorized users have access to the API connection.

    To authorize the API connection once the resource is deployed into the Azure:

    enter image description here

    Note: It is not possible to deploy an Azure API connection with the status as connected in terraform because the authorization process is needed for the security reasons. You can use PowerShell or CLI to automate this process.

    Alternatively, you can use azurerm_api_connection instead AZAPI provider as detailed in terraform registry.