PrayingMantis
PrayingMantis

Reputation: 310

Unable to apply a policy inside of the my deployed Azure api manager

While trying to deploy a policy on my operation within my apim, I am unable to implement my policy.

Error log

Error: creating or updating API Policy (Resource Group "rg-opendata-dev" / API Management Service "apimopendata-dev" / API "apim-opendata-dev"): apimanagement.APIPolicyClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="ValidationError" Message="Entity with specified identifier not found"
│ 
│   with azurerm_api_management_api_policy.apipolicy,
│   on main.tf line 78, in resource "azurerm_api_management_api_policy" "apipolicy":
│   78: resource "azurerm_api_management_api_policy" "apipolicy" {
│ 
╵

I don't understand this, because I'm pretty sure I wrote the variables correctly, or have I given the variable the wrong name?

Do you also think that adding the policy with the xml code as shown below is the right way to deploy a policy?

Part of the main.tf file


resource "azurerm_api_management_api" "api" {
    name = "apim-opendata-${var.env}"
    resource_group_name = data.azurerm_resource_group.rg.name
    api_management_name = azurerm_api_management.apim.name
    revision = "1"
    display_name = "${var.display_name}"
    service_url = "${var.service_url}"
    protocols = ["https"]
}

resource "azurerm_api_management_api_operation" "apioperation" {
  operation_id = "get-data"
  api_name = azurerm_api_management_api.api.name
  api_management_name = azurerm_api_management.apim.name
  resource_group_name = data.azurerm_resource_group.rg.name
  display_name = "Get Data"
  method = "GET"
  url_template = "/"
  description = "Get data inside of the container"

  response {
    status_code = 200
  }
}

resource "azurerm_api_management_api_policy" "apipolicy" {
    api_name = azurerm_api_management_api.api.name
    api_management_name = azurerm_api_management.apim.name
    resource_group_name = data.azurerm_resource_group.rg.name

    xml_content = <<XML
<policies>
    <inbound>
        <set-variable name="ContainerName" value="@(context.Request.Headers.GetValueOrDefault("Container"))" />
        <set-variable name="BlobName" value="@(context.Request.Headers.GetValueOrDefault("Blob"))" />
        <base />
        <set-header name="Blob" exists-action="delete" />
        <set-header name="Container" exists-action="delete" />
        <set-header name="x-ms-version" exists-action="override">
            <value>@{string version = "2017-11-09"; return version;}</value>
        </set-header>
        <set-backend-service base-url="@{
string containerName = context.Variables.GetValueOrDefault<string>("ContainerName");
string blobName = context.Variables.GetValueOrDefault<string>("BlobName");
return String.Format("https://${var.storage_account_name}.blob.core.windows.net/{0}/{1}", containerName, blobName);
}" />
        <authentication-managed-identity resource="https://storage.azure.com/" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>
XML  
}

Upvotes: 0

Views: 3844

Answers (2)

Neha
Neha

Reputation: 11

I was facing same issue while creating APIs in APIM using OpenAPI JSON file.

I Was Able to solve this by adding depends_on = [name of dependent resource (azurerm_api_management_api_operation.apioperation in your case)] in azurerm_api_management_api_policy resource.

Hope it Helps !

Upvotes: 1

Delliganesh Sevanesan
Delliganesh Sevanesan

Reputation: 4778

The issue may happen because of not setting the base-URL & Backend-id property, in this situation API Manager service does not detect the backend component using the id. So we need to specify both base-URL and backend-id.

solutions for same issues

If still you are facing the problem please check below step:

This policy routes calls to the closest of two backend services and fails over to the secondary if an HTTP 404 is returned.

It assumes that the API Manager is deployed in 'East US' and 'West Europe'. Similarly the policy (as is) assumes two backend services, in the same regions, vis:

https://hello-eus.azurewebsites.net/ (for East US); and

https://hello-weu.azurewebsites.net/ (for West Europe)

If a failure (HTTP 404) is returned from the backend service, the policy will re-route the call to the fail-over region.

The policy uses cached values to track which service has returned an error in the last 10 seconds, to avoid routing new requests to a backend which will likely fail.

<retry condition="@(context.Response.StatusCode == 404)" count="2" interval="1" max-interval="10" delta="1" first-fast-retry="true">

Use the logic inside the retry policy refer here

Refer here

Upvotes: 1

Related Questions