0

I'm creating an APIM in Azure using Terraform. So far I've been able to create the APIM instance, the API and an operation within the API. Because I want each operation for the API to point to an individual Logic App, my understanding is I cannot set this as an azurerm_api_management_backend and instead need to set it in the operation policy in XML.

This is what my operation policy looks like:

resource "azurerm_api_management_api_operation_policy" "apim1_ss_cmpcomplaints_api_dev_get_policy" {
  api_name            = azurerm_api_management_api.apim1_ss_api_dev.name
  api_management_name = azurerm_api_management.test-apimManagement.name
  resource_group_name = azurerm_resource_group.apimResourceGroup.name
  operation_id        = "get-complaints"
  xml_content         = <<XML
  <policies>
    <inbound>
        <base />
        <set-method id="apim-generated-policy">GET</set-method>
        <set-backend-service id="apim-generated-policy" backend-id="/subscriptions/xxx/resourceGroups/hm-iac-msdn-neu-rg/providers/Microsoft.Logic/workflows/testLogicApp" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
  </policies>
  XML
}

I have created the Logic App and retrieved the resource ID from the Azure CLI and included it in the set-backend-service node. But despite getting the ID from the CLI, I am getting the following response:

│ Error: creating or updating API Operation Policy (Resource Group "apim-resource-group" / API Management Service "harry-test-apim" / API "test_api_dev" / Operation "get-complaints"): apimanagement.APIOperationPolicyClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="ValidationError" Message="One or more fields contain incorrect values:" Details=[{"code":"ValidationError","message":"Error in element 'set-backend-service' on line 5, column 10: Backend with id '/subscriptions/xxx/resourceGroups/hm-iac-msdn-neu-rg/providers/Microsoft.Logic/workflows/testLogicApp' could not be found.","target":"set-backend-service"}]

I'd prefer to set this using the resource ID instead of using base-url.

HarryMoy
  • 142
  • 3
  • 20

2 Answers2

0

It's possible.

I'm using the following

resource "azurerm_api_management_backend" "backend_1" {
  name                = "backend-${var.project_name}-${var.environment}-ert45"
  resource_group_name = azurerm_resource_group.rg_app.name
  api_management_name = azurerm_api_management.apim.name

  protocol = "http"
  url      = "yoururl"
}

resource "azurerm_api_management_api_policy" "api_1_policy" {
  api_name            = azurerm_api_management_api.api_1.name
  resource_group_name = azurerm_resource_group.rg_app.name
  api_management_name = azurerm_api_management.apim.name

  xml_content = <<XML
<policies>
  <inbound>
     <base />
     <set-backend-service backend-id="${azurerm_api_management_backend.backend_1.name}" />

  </inbound>
  <backend>
      <base />
  </backend>
  <outbound>
      <base />
  </outbound>
  <on-error>
      <base />
  </on-error>
</policies>
XML

  depends_on = [
    azurerm_api_management_backend.backend_1,
    azurerm_api_management_api.api_1
  ]
}

Sven
  • 2,345
  • 2
  • 21
  • 43
-1

The backend-id is different to the ARM id. It's not easy to find, so the solution is to generate the policy in the APIM front-end and then apply that in code afterwards.

HarryMoy
  • 142
  • 3
  • 20
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 01 '21 at 15:18