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.
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
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:00az_api
data block to automate the deployment. @hitman126 – Jahnavi Commented Jan 31 at 11:11