1

I am trying to create API Management Service using terraform. I am able to map most of the components from the UI to the documentation. But, I cannot see any resource block or configuration option in

azurerm_api_management_api_operation

to add a backend URL for an operation. How do I add it through terraform?

Azure portal overview

Azure portal setup

I saw this resource block in the documentation for the backend

resource "azurerm_api_management_backend" "example" {
  name                = "example-backend"
  resource_group_name = azurerm_resource_group.example.name
  api_management_name = azurerm_api_management.example.name
  protocol            = "http"
  url                 = "https://backend"
}

But there is no way to link it to an operation in an API

bursson
  • 495
  • 4
  • 14
Sadiq
  • 11
  • 1

2 Answers2

0

This might work...

Set the operation policy using https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/api_management_api_operation_policy

e.g.

resource "azurerm_api_management_api_operation_policy" "policy" {
  resource_group_name = data.azurerm_api_management.this.resource_group_name
  api_management_name = data.azurerm_api_management.this.name
  api_name            = azurerm_api_management_api.this.name
  operation_id        = var.operationId
  xml_content         = var.operationPolicy
  depends_on = [
    azurerm_api_management_api.this
  ]
}

then in the policy xml set the back end url using the set-backend-service policy. e.g.

<policies>
    <inbound>
        <base />
        <set-backend-service base-url="https://back-end-url" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>
Que Dee
  • 51
  • 1
  • 3
0

You better take the backend reference from terraform state,

   resource "azurerm_api_management_api_operation_policy" "set-backend" {
  api_name = azurerm_api_management_api.api.name
  operation_id        = var.operationId
  api_management_name = azurerm_api_management.apim.name
  resource_group_name = var.resource_group_name
   xml_content = <<XML
<policies>
  <inbound>
     <set-backend-service backend-id="${azurerm_api_management_backend.apim-backend.name}" />
  </inbound>
</policies>
XML
}
Hiran
  • 1,102
  • 11
  • 18