admin管理员组

文章数量:1314308

When trying to create an azurerm_api_management_api_diagnostic resource within an existing APIM, an azurerm_api_management_logger id is required.

Currently, Terraform only provides a resource block for the azurerm_api_management_logger, which therefore presents a huge challenge for me and this is why. We already have an azurerm_api_management_logger resource in APIM which I'd like to consume or reference.....instead of having to create brand new logger, using a Resource block.

Typically, if this was for some other Azure resource in a similar scenario, I would simply have used a Data block to reference the existing resource. Azurerm_api_management_logger however does not appear to support any Data blocks, meaning there is no way of me accessing or referencing the existing logger id required for my Terraform configuration.

Any suggestions on how I can get round this? The intent here is not to manage the existing logger but just reference it.

When trying to create an azurerm_api_management_api_diagnostic resource within an existing APIM, an azurerm_api_management_logger id is required.

Currently, Terraform only provides a resource block for the azurerm_api_management_logger, which therefore presents a huge challenge for me and this is why. We already have an azurerm_api_management_logger resource in APIM which I'd like to consume or reference.....instead of having to create brand new logger, using a Resource block.

Typically, if this was for some other Azure resource in a similar scenario, I would simply have used a Data block to reference the existing resource. Azurerm_api_management_logger however does not appear to support any Data blocks, meaning there is no way of me accessing or referencing the existing logger id required for my Terraform configuration.

Any suggestions on how I can get round this? The intent here is not to manage the existing logger but just reference it.

Share Improve this question edited Feb 3 at 13:07 Jahnavi 8,0581 gold badge6 silver badges12 bronze badges Recognized by Microsoft Azure Collective asked Jan 30 at 12:47 hitman126hitman126 9511 gold badge27 silver badges59 bronze badges 4
  • 1 Keep it simple - why don't you declare a variable named api_management_logger_id and then go to the Azure Portal and grab the correspondent resource id and set the variable in a .tfvars file? – Rui Jarimba Commented Jan 30 at 13:00
  • Can you share the code tried so far! – Jahnavi Commented Jan 31 at 3:32
  • Thanks Rui Jarimba. That was an absolutely brilliant suggestion. It worked a treat !! – hitman126 Commented Jan 31 at 10:26
  • You can also try adding az_api data block to automate the deployment. @hitman126 – Jahnavi Commented Jan 31 at 11:11
Add a comment  | 

1 Answer 1

Reset to default 0

Usually in Terraform, to reference existing resources and get their relevant properties one main approach is to use a data block. But after a workaround on your issue, I too found that there is no data block existed for APIM service logger resource.

As a workaround, I figured out that data azapi_resource block helps out in this scenario as shown below.

data "azapi_resource" "logger" {
  type      = "Microsoft.ApiManagement/service/loggers@2024-06-01-preview"
  name      = "newloger"
  parent_id = data.azurerm_api_management.example.id
  response_export_values = ["id"]
}
output "logger_id" {
  value = data.azapi_resource.logger.id
}

Also, thanks @Rui Jarimba for suggesting another workaround by declaring logger id under a variable block.

Complete code using data azapi resource:

terraform {
  required_providers {
    azapi = {
      source = "Azure/azapi"
      version = "2.2.0"
    }
  }
}

provider "azapi" {
  # Configuration options
}
provider "azurerm" {
  features {}
  subscription_id="47xxxxb014"
}

data "azurerm_resource_group" "example" {
  name     = "Jahnavi"
}

data "azurerm_api_management" "example" {
  name                = "newapimjah"
  resource_group_name = data.azurerm_resource_group.example.name
}
data "azapi_resource" "logger" {
  type      = "Microsoft.ApiManagement/service/loggers@2024-06-01-preview"
  name      = "newloger"
  parent_id = data.azurerm_api_management.example.id
  response_export_values = ["id"]
}
output "logger_id" {
  value = data.azapi_resource.logger.id
}
resource "azurerm_application_insights" "example" {
  name                = "examplej-appinsightsnew"
  location            = data.azurerm_resource_group.example.location
  resource_group_name = "newresources"
  application_type    = "web"
}
resource "azurerm_api_management_diagnostic" "example" {
  identifier               = "applicationinsights"
  resource_group_name      = "newresources"
  api_management_name      = "latestnewapimj"
  api_management_logger_id = data.azapi_resource.logger.id

  sampling_percentage       = 10.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",
    ]
  }
}

Output:

Reference: azurerm_api_management_diagnostic

本文标签: terraformReferencing an existing azurermapimanagementlogger in AzureStack Overflow