1
votes

I'm managing my azure API management (APIs and policies) by using terraform. Most of the things are working fine and the documentation on https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs is great. But now I need to activate application insights diagnostic logs with verbosity error on All-APIs level. Unfortunately I don't understand how to do that by checking the documentation. Can someone help me how to do that?

This is how it looks like when I set it through the UI.

this is what I want to achieve with terraform

I hope someone can help ????

1
Is this the screenshot from your azure portal you added? - Pankaj Devrani
Do you have application insight instance created in your resource group? - Pankaj Devrani
yes this screenshot is from my azure portal. if I enable the diagnostics logs for one specific API though terraform everything works fine, but I don't understand how I can set it on "All APIs" level. azurerm_api_management_api_diagnostic wants an api_name but I don't have an azurerm_api_management_api object for "All APIs". registry.terraform.io/providers/hashicorp/azurerm/latest/docs/… - sschoebinger

1 Answers

2
votes

azurerm_api_management_diagnostic should be what you are looking for

resource "azurerm_application_insights" "example" {
  name                = "example-appinsights"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  application_type    = "web"
}

resource "azurerm_api_management" "example" {
  name                = "example-apim"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  publisher_name      = "My Company"
  publisher_email     = "company@terraform.io"
  sku_name            = "Developer_1"
}
resource "azurerm_api_management_logger" "example" {
  name                = "example-apimlogger"
  api_management_name = azurerm_api_management.example.name
  resource_group_name = azurerm_resource_group.example.name

  application_insights {
    instrumentation_key = azurerm_application_insights.example.instrumentation_key
  }
}

resource "azurerm_api_management_diagnostic" "example" {
  identifier               = "applicationinsights"
  resource_group_name      = azurerm_resource_group.example.name
  api_management_name      = azurerm_api_management.example.name
  api_management_logger_id = azurerm_api_management_logger.example.id

  sampling_percentage       = 5.0
  always_log_errors         = true
  log_client_ip             = true
  verbosity                 = "Verbose"
  http_correlation_protocol = "W3C"

  frontend_request {
    body_bytes = 32
    headers_to_log = [
      "content-type",
      "accept",
      "origin",
    ]
  }

  frontend_response {
    body_bytes = 32
    headers_to_log = [
      "content-type",
      "content-length",
      "origin",
    ]
  }

  backend_request {
    body_bytes = 32
    headers_to_log = [
      "content-type",
      "accept",
      "origin",
    ]
  }

  backend_response {
    body_bytes = 32
    headers_to_log = [
      "content-type",
      "content-length",
      "origin",
    ]
  }
}