terraformazure-api-managementterraform-provider-azureazure-rmapim

Terraform AzureRM Not working to create api managment (APIM) backend ValidationError


I'm trying to create a APIM backend with terraform, and it always gives me this error:

module.apim_backends.azurerm_api_management_backend.example: Creating...
╷
│ Error: creating/updating Backend (Subscription: "e*******-ce34-****-****-0151ca9****4"
│ Resource Group Name: "**-**-**-devqa"
│ Service Name: "*******"
│ Backend: "example"): unexpected status 400 (400 Bad Request) with error: ValidationError: One or more fields contain incorrect values:
│
│   with module.apim_backends.azurerm_api_management_backend.example,
│   on ..\modules\apimgmt\backends\backends.tf line 1, in resource "azurerm_api_management_backend" "example":
│    1: resource "azurerm_api_management_backend" "example" {

The code in the terraform (sending only this part because the others code works, and all variables are correct, I've checked multiple times):

resource "azurerm_api_management_backend" "example" {
  name        = format("%s%s", "example", var.env)
  description = format("%s%s", "example", var.env)

  api_management_name = var.apim_name
  resource_group_name = var.resource_group_name

  protocol    = "http"
  url         = format("%s%s%s", "https://***", var.env, ".azurewebsites.net/api")
  resource_id = var.example_id

  tls {
    validate_certificate_chain = false
    validate_certificate_name  = false
  }
}

The terraform plan:

  + resource "azurerm_api_management_backend" "example" {
      + api_management_name = "*****"
      + description         = "***"
      + id                  = (known after apply)
      + name                = "example"
      + protocol            = "http"
      + resource_group_name = "rg-*****-*****-*****"
      + resource_id         = "/subscriptions/e*******-ce34-****-****-0151ca9****4/resourceGroups/rg-*****-*****-devqa/providers/Microsoft.Web/sites/example"
      + url                 = "https://*******.azurewebsites.net"

      + tls {
          + validate_certificate_chain = false
          + validate_certificate_name  = false
        }
    }

The idea is only to create the APIM backend, because the functions pipelines will create the rest through Swagger, like the APIs and the policies

<set-backend-service id="apim-generated-policy" backend-id="example" />

I haven't tried many things other than that, just some searches.

provider:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=3.107.0"
    }
  }
  backend "azurerm" {
    resource_group_name  = "rg-*****-****-devqa"
    storage_account_name = "state"
    container_name       = "tfstatedev"
    key                  = "terraform.tfstate"
  }
}

provider "azurerm" {
  features {}

  skip_provider_registration = true
}

Solution

  • I fixed the problem, for some reason I needed to add the "https://management.azure.com" before the function app id.

    resource "azurerm_api_management_backend" "example" {
      name        = format("%s%s", "example", var.env)
      description = format("%s%s", "example", var.env)
    
      api_management_name = var.apim_name
      resource_group_name = var.resource_group_name
    
      protocol    = "http"
      url         = format("%s%s%s", "https://***", var.env, ".azurewebsites.net/api")
      resource_id = format("%s%s", "https://management.azure.com", var.example)
    
      tls {
        validate_certificate_chain = false
        validate_certificate_name  = false
      }
    }
    

    I will open an issue on github to understand about that.

    But the problem fixed.