1
votes

See below configuration I am using to add a diagonstic setting to send App service logs to a Log analytics workspace.

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_diagnostic_setting

    resource "azurerm_app_service" "webapp" {
      for_each = local.apsvc_map_with_locations
    
      name                = "${var.regional_web_rg[each.value.location].name}-${each.value.apsvc_name}-apsvc"
      location            = each.value.location
      resource_group_name = var.regional_web_rg[each.value.location].name
      app_service_plan_id = azurerm_app_service_plan.asp[each.value.location].id
      https_only          = true
           
      identity {
        type         = "UserAssigned"
        identity_ids = each.value.identity_ids
      }
    }
    
    resource "azurerm_monitor_diagnostic_setting" "example" {
      for_each = local.apsvc_map_with_locations
    
      name                       = "example"
      target_resource_id         = "azurerm_app_service.webapp[${each.value.location}-${each.value.apsvc_name}].id"
      log_analytics_workspace_id = data.terraform_remote_state.pod_bootstrap.outputs.pod_log_analytics_workspace.id
      log {
        category = "AuditEvent"
        enabled  = false
    
        retention_policy {
          enabled = false
        }
      }
    
      metric {
        category = "AllMetrics"
    
        retention_policy {
          enabled = false
        }
      }
    }

Error:

Can not parse "target_resource_id" as a resource id: Cannot parse Azure ID: parse "azurerm_app_service.webapp[].id": invalid URI for request
2020-11-06T20:19:59.3344089Z 
2020-11-06T20:19:59.3346016Z   on .terraform\modules\web.web\pipeline\app\apsvc\app_hosting.tf line 127, in resource "azurerm_monitor_diagnostic_setting" "example":
2020-11-06T20:19:59.3346956Z  127: resource "azurerm_monitor_diagnostic_setting" "example" {
2020-11-06T20:19:59.3347091Z 
1

1 Answers

0
votes

You can try with the following:

target_resource_id         = azurerm_app_service.webapp["${each.value.location}-${each.value.apsvc_name}"].id

instead of:

target_resource_id         = "azurerm_app_service.webapp[${each.value.location}-${each.value.apsvc_name}].id"