hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.54k stars 4.61k forks source link

azurerm_api_management_backend not working as expected for resource_id #26342

Closed Thomaz-Peres closed 3 months ago

Thomaz-Peres commented 3 months ago

Is there an existing issue for this?

Community Note

Terraform Version

1.8.5

AzureRM Provider Version

3.107.0

Affected Resource(s)/Data Source(s)

azurerm_api_management_backend

Terraform Configuration Files

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://example", var.env, ".azurewebsites.net/api")
  resource_id         = var.example_id

  tls {
    validate_certificate_chain = false
    validate_certificate_name  = false
  }
}

Debug Output/Panic Output

module.apim_backends.azurerm_api_management_backend.example: Creating...
╷
│ Error: creating/updating Backend (Subscription: "e*******-****-****-****-0151ca9*****"
│ Resource Group Name: "**-**-**-****"
│ 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" {

### Expected Behaviour

Create the backend connected to the Function App

### Actual Behaviour

Actually this did not happen, and it occurs because it didn't find the Functon App, in my case

I'm fixed that adding the `"https://management.azure.com"` before the `resource_id`, like this:

```terraform
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
  }
}

The resource_id looks like this after formattting:

https://management.azure.com/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Web/sites/site1

I don't know if this is something about azure, ou if terraform should add this completion, but it fixes the error.

Steps to Reproduce

No response

Important Factoids

No response

References

https://stackoverflow.com/questions/78623569/terraform-azurerm-not-working-to-create-api-managment-apim-backend-validatione/78623973#78623973

sinbai commented 3 months ago

Hi @Thomaz-Peres thanks for opening this issue. I would like to explain that Terraform manages Azure resources through the Azure Rest API. The above error returned by Azure Rest API instead of Terraform. For resource_id, Terraform just passes the value of resource_id specified by the user to the API without doing any special processing. Also, per the description of resource_id, I assume that the The management URI of the backend host in an external system. means the resource_id should be a URI,taking the resource_id mentioned above as an example, it should contain the https://management.azure.com instead of just /subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Web/sites/site1.

Thomaz-Peres commented 3 months ago

Hi @sinbai, thanks for answering. I expected it to be an azure problem but I dind't know where open the issue (my mistake, I think to add it to the issue). Should I open this here ?

sinbai commented 3 months ago

Hi @Thomaz-Peres thank you for your feedback. I assume that you can open an issue here or contact Microsoft through Azure Portal for support.

Thomaz-Peres commented 3 months ago

Ok, thank you @sinbai!

I will close the issue

github-actions[bot] commented 2 months ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.