I have this terraform script to create two diagnostic setting for different Azure Signal R instances when var.environment == "prod". Kindly note that the Diagnostics setting are two Azure Signal R instances in different Azure regions, using two different Log Analytics workspaces, in two different regions as well.

resource "azurerm_monitor_diagnostic_setting" "Database_diagnostics_SignalR" {
  count = var.environment == "prod" ? 2 : 1
  name = var.environment != "prod" ? "${var.product}${var.environment}SignalRdiagnostics" : "${var.product}${var.environment}${keys(var.location)[count.index]}SignalRdiagnostics"
  target_resource_id = azurerm_signalr_service.product_signalr[count.index].id
  log_analytics_workspace_id =  var.log_analytics_workspace_ids[count.index]
  enabled_log {
    category_group = "allLogs"
  }
  metric {
    category = "AllMetrics"
  }
}

When I apply the script, I get this error

 Error: creating Monitor Diagnostics Setting "xxx-UK1-SignalRdiagnostics" for Resource "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.SignalRService/signalR/xxx-signalr-UK1": diagnosticsettings.DiagnosticSettingsClient#CreateOrUpdate: Failure sending request: StatusCode=409 -- Original Error: autorest/azure: Service returned an error. Status=<nil> <nil>
│ 
│   with azurerm_monitor_diagnostic_setting.Database_diagnostics_SignalR[0],
│   on tests/signalr_and_pep/signalr_and_pep.tf line 126, in resource "azurerm_monitor_diagnostic_setting" "Database_diagnostics_SignalR":
│  126: resource "azurerm_monitor_diagnostic_setting" "Database_diagnostics_SignalR" {
│ 
╵
╷
│ Error: creating Monitor Diagnostics Setting "xxx-UK2-SignalRdiagnostics" for Resource "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.SignalRService/signalR/xxx-signalr-UK2": diagnosticsettings.DiagnosticSettingsClient#CreateOrUpdate: Failure sending request: StatusCode=409 -- Original Error: autorest/azure: Service returned an error. Status=<nil> <nil>
│ 
│   with azurerm_monitor_diagnostic_setting.Database_diagnostics_SignalR[1],
│   on tests/signalr_and_pep/signalr_and_pep.tf line 126, in resource "azurerm_monitor_diagnostic_setting" "Database_diagnostics_SignalR":
│  126: resource "azurerm_monitor_diagnostic_setting" "Database_diagnostics_SignalR" {

I have ensured the Diagnostic setting have different names, and that they target two different Signal R resources in two different regions, and also ensure they use different Log Analytics workspaces, which are solutions proffered by other developers that face similar issue. Yet I keep getting the same error.

1

There are 1 best solutions below

3
Vinay B On

I tried to sort the issue AzureDiagnosticSettings Failure sending request: StatusCode=409 -- Original Error: autorest/azure: Service returned an error. Status= and I was able to provision the requirement successfully.

You shared a script module that applies these settings to various Azure regions, using different Log Analytics workspaces for each one. This error usually happens when there are Azure resource conflicts, such as when the resource you want to create already exists or has a conflicting configuration.

The error message StatusCode=409 indicates a conflict. This could be due to:

  1. **Existing Diagnostic Setting with the Same Name:**You can't have more than one diagnostic setting with the same name for a given target resource in Azure. This applies even across different regions. If your Terraform script generates duplicate names or tries to use a name that already exists, you'll get this error.

  2. Configuration Issues: A possible cause of the problem is that the resources have been set up incorrectly, for example, by using a wrong Log Analytics workspace ID or SignalR service ID.

To address this and meet your requirement, we'll ensure:

  • Diagnostic settings have unique names, based on the environment and location.
  • SignalR services and Log Analytics workspaces are correctly defined and referenced.
  • Conditional logic is used to create resources based on the var.environment value.

Terraform configuration:

provider "azurerm" {
  features {}
}

variable "environment" {}

variable "product" {}

variable "location" {
  type = map(string)
}

variable "log_analytics_workspace_ids" {
  type = list(string)
}

resource "azurerm_resource_group" "example" {
  for_each = var.location
  
  name     = "${var.product}${var.environment}${each.key}RG"
  location = each.value
}

resource "azurerm_signalr_service" "product_signalr" {
  for_each = var.location

  name                = "${var.product}-${var.environment}-${each.key}-signalr"
  location            = each.value
  resource_group_name = azurerm_resource_group.example[each.key].name

  sku {
    name     = "Standard_S1"
    capacity = 1
  }
}

resource "azurerm_monitor_diagnostic_setting" "database_diagnostics_signalr" {
  count = length(var.location)

  name                       = "${var.product}${var.environment}${element(keys(var.location), count.index)}SignalRdiagnostics"
  target_resource_id         = element(values(azurerm_signalr_service.product_signalr), count.index).id
  log_analytics_workspace_id = element(var.log_analytics_workspace_ids, count.index)

  enabled_log {
    category = "AllLogs"
    //enabled  = true
  }

  metric {
    category = "AllMetrics"
    enabled  = true
  }
}

terraform.tfvars:

environment = "dev"
product = "myapp"

location = {
  "EastUS" = "eastus"
  "WestUS" = "westus"
}

log_analytics_workspace_ids = [
  "/subscriptions/xxxx/resourceGroups/myapp-prod-EastUS/providers/Microsoft.OperationalInsights/workspaces/myappLogAnalyticsEastUS",
  "/subscriptions/xxxx/resourceGroups/myapp-prod-WestUS/providers/Microsoft.OperationalInsights/workspaces/myappLogAnalyticsWestUS"
]

Output:

enter image description here

enter image description here

enter image description here

enter image description here