Azure Data collection rule dynamic block

resource "azurerm_monitor_data_collection_rule" "DCR" {
  name                        = var.data_collection_rule_name
  resource_group_name         = var.resource_group_name
  location                    = var.location
  data_collection_endpoint_id = azurerm_monitor_data_collection_endpoint.DCE.id
  description = var.description
  tags =  local.tags
  dynamic "destinations" {
    for_each = var.destinations
    content {
        dynamic "azure_monitor_metrics" {
          for_each = lookup(destinations.value, "azure_monitor_metrics", {}) != {} ? [1] : [ ]
            content {
               name                        = lookup(destinations.value.azure_monitor_metrics, "name", null)
                }
              }
        dynamic "event_hub" {
          for_each = lookup(destinations.value, "event_hub", {}) != {} ? [1] : [ ]
          content {
            event_hub_id                = lookup(destinations.value.event_hub, "event_hub_id", null)
            name                        = lookup(destinations.value.event_hub, "name", null)
        }
      }
       dynamic "event_hub_direct" {
          for_each = lookup(destinations.value, "event_hub_direct", {}) != {} ? [1] : [ ]
        content {
            event_hub_id                = lookup(destinations.value.event_hub_direct, "event_hub_id", null)
            name                        = lookup(destinations.value.event_hub_direct, "name", null)
        }
       }
        dynamic "log_analytics" {
          for_each = lookup(destinations.value, "log_analytics", {}) != {} ? [1] : [ ]
          content {
            workspace_resource_id       = lookup(destinations.value.log_analytics, "workspace_resource_id", null)
            name                        = lookup(destinations.value.log_analytics, "name", null)
          }
        }
        dynamic "monitor_account" {
          for_each = lookup(destinations.value, "monitor_account", {}) != {} ? [1] : [ ]
          content{
            monitor_account_id          = lookup(destinations.value.monitor_account, "monitor_account_id", null)
            name                        = lookup(destinations.value.monitor_account, "name", null)
          }
        }
        dynamic "storage_blob" {
          for_each = lookup(destinations.value, "storage_blob", {}) != {} ? [1] : [ ]
          content {
            container_name              = lookup(destinations.value.storage_blob, "container_name", null)
            name                        = lookup(destinations.value.storage_blob, "name", null)
            storage_account_id          = lookup(destinations.value.storage_blob, "storage_account_id", null)
          }
        }
        dynamic "storage_blob_direct" {
          for_each = lookup(destinations.value, "storage_blob_direct", {}) != {} ? [1] : [ ]
          content {
            container_name              = lookup(destinations.value.storage_blob_direct, "container_name", null)
            name                        = lookup(destinations.value.storage_blob_direct, "name", null)
            storage_account_id          = lookup(destinations.value.storage_blob_direct, "storage_account_id", null)
        }
      }
    }
  }
  dynamic "data_flow" {
    for_each = var.data_flows
    content {
      streams      = data_flow.value.streams
      destinations = data_flow.value.destinations
      built_in_transform = lookup(data_flow.value, "built_in_transform", null)
      output_stream = lookup(data_flow.value, "output_stream", null)
      transform_kql = lookup(data_flow.value, "transform_kql", null)
    }
  }
  dynamic "data_sources" {
    for_each = length(var.var.data_sources) > 0 ? [var.var.data_sources] : [ ]
    content {
      dynamic "syslog"{
        for_each = lookup(data_sources.value, "syslog", {}) != {} ? [1] : [ ]
          content {
            streams                     = lookup(data_sources.value.syslog, "streams", null)
            name                        = lookup(data_sources.value.syslog, "name", null)
            facility_names              = lookup(data_sources.value.syslog, "facility_names", null)
            log_levels                  = lookup(data_sources.value.syslog, "log_levels", null)
          }
        }
      dynamic "data_import" {
         for_each = lookup(data_sources.value, "data_import", {}) != {} ? [1] : [ ]
         content {
          dynamic "event_hub_data_source" {
            for_each = lookup(data_sources.value.data_import, "event_hub_data_source", {}) != {} ? [1] : []
            content {
              stream                    = lookup(data_sources.value.data_import.event_hub_data_source, "stream", null)
              name                       = lookup(data_sources.value.data_import.event_hub_data_source, "name", null)
              consumer_group             = lookup(data_sources.value.data_import.event_hub_data_source, "consumer_group", null)
              }
            }
          }
        }
      dynamic "extension" {
        for_each = lookup(data_sources.value, "extension", {}) != {} ? [1] : []
          content {
            streams                       = lookup(data_sources.value.extension, "streams", null)
            name                          = lookup(data_sources.value.extension, "name", null)
            extension_name                = lookup(data_sources.value.extension, "extension_name", null)
            extension_json                = lookup(data_sources.value.extension, "extension_json", null)
            input_data_sources            = lookup(data_sources.value.extension, "input_data_sources", null)
          }
        }
      dynamic "iis_log" {
        for_each = lookup(data_sources.value, "extension", {}) != {} ? [1] : []
          content {
            streams                     = lookup(data_sources.value.iis_log, "streams", null)
            name                        = lookup(data_sources.value.iis_log, "name", null)
            log_directories              = lookup(data_sources.value.iis_log, "log_directories", null)
          }
        }
      dynamic "log_file" {
          for_each = lookup(data_sources.value, "log_file", {}) != {} ? [1] : []
            content {
              streams                     = lookup(data_sources.value.log_file, "streams", null)
              name                        = lookup(data_sources.value.log_file, "name", null)
              file_patterns              = lookup(data_sources.value.log_file, "file_patterns", null)
              format                     = lookup(data_sources.value.log_file, "format", null)
                dynamic "settings"{
                  for_each = lookup(data_sources.value.data_import, "settings", {}) != {} ? [1] : []
                  content {
                    dynamic "text" {
                      for_each = lookup(data_sources.value.data_import.settings, "text", {}) != {} ? [1] : []
                          content {
                    record_start_timestamp_format      = lookup(data_sources.value.log_file.settings.text, "record_start_timestamp_format", null)
                      }
                    }
                  }
                }
              }
            }
      dynamic "performance_counter" {
        for_each = lookup(data_sources.value, "performance_counter", {}) != {} ? [1] : [ ]
            content {
              streams                          = lookup(data_sources.value.performance_counter, "streams", null)
              name                             = lookup(data_sources.value.performance_counter, "name", null)
              counter_specifiers               = lookup(data_sources.value.performance_counter, "counter_specifiers", null)
              sampling_frequency_in_seconds    = lookup(data_sources.value.performance_counter, "sampling_frequency_in_seconds", null)
              }
            }
      dynamic "platform_telemetry"  {
        for_each = lookup(data_sources.value, "platform_telemetry", {}) != {} ? [1] : []
          content {
            streams                     = lookup(data_sources.value.platform_telemetry, "streams", null)
            name                        = lookup(data_sources.value.platform_telemetry, "name", null)
            }
          }
      dynamic "prometheus_forwarder" {
        for_each = lookup(data_sources.value, "prometheus_forwarder", {}) != {} ? [1] : []
          content {
            streams                     = lookup(data_sources.value.prometheus_forwarder, "streams", null)
            name                        = lookup(data_sources.value.prometheus_forwarder, "name", null)
              dynamic "label_include_filter"{
                for_each = lookup(data_sources.value.prometheus_forwarder, "label_include_filter", {}) != {} ? [1] : []
                  content {
                    label        = lookup(data_sources.value.prometheus_forwarder.label_include_filter, "label", null)
                    value        = lookup(data_sources.value.prometheus_forwarder.label_include_filter, "value", null)
                  }
                }
              }
            }
      dynamic "windows_event_log"  {
        for_each = lookup(data_sources.value, "windows_event_log", {}) != {} ? [1] : []
          content {
            streams                     = lookup(data_sources.value.windows_event_log, "streams", null)
            name                        = lookup(data_sources.value.windows_event_log, "name", null)
            x_path_queries              = lookup(data_sources.value.windows_event_log, "x_path_queries", null)
            }
           }
      dynamic "windows_firewall_log"  {
                for_each = lookup(data_sources.value, "windows_firewall_log", {}) != {} ? [1] : []
                content  {
                streams                     = lookup(data_sources.value.windows_firewall_log, "streams", null)
                name                        = lookup(data_sources.value.windows_firewall_log, "name", null)
                }
              }
    }
  }
  dynamic "stream_declaration" {
        for_each = var.stream_declaration != null ? [1] : [0]
        content {
          stream_name = stream_declaration.value.stream_name
          dynamic "column" {
            for_each = stream_declaration.value.columns
            content {
              name = column.value.name
              type = column.value.type
            }
          }
    }
  }
  dynamic "identity" {
        for_each = var.identity_type != null ? ["identity"] : []
        content {
          type = var.identity_type
          # Avoid perpetual changes if SystemAssigned and identity_ids is not null
          identity_ids = try(length(data.azurerm_user_assigned_identity.uami_list), null) == null ? [] : values(data.azurerm_user_assigned_identity.uami_list)[*].id
        }
      }
    depends_on = [azurerm_monitor_data_collection_endpoint.DCE]
}

  resource "azurerm_monitor_data_collection_rule_association" "main" {
      name                    = var.data_collection_rule_association_name
      target_resource_id      = var.target_resource_id
      data_collection_rule_id = azurerm_monitor_data_collection_rule.DCR.id

}

I'm trying to create dynamic block for azure data collection rule using dynamic block as above. 

when i run the code getting below error.

Error: Too many data_sources blocks

 on ../modules/iac/data-collection/azurerm/1.2.0/main.tf line 103, in resource "azurerm_monitor_data_collection_rule" "DCR":

 103: content {

 No more than 1 "data_sources" blocks are allowed

Hi @raj.vedachalam,

Can you please reformat your post using the code block. Use 3x backtick ``` on the line before and the line after your code to

format it like this

This will make it more readable in the post as it will respect indentation, etc. It will also ensure that special characters are not introduced and prevent 66 99 ‘’ ‘’ type quotes which will mean the code can be copied and pasted by people to try and reproduce the issue using your provided code.

made changes as needed

pls close this issue. I manage to fix the issue. Below code is working.

resource "azurerm_monitor_data_collection_rule" "DCR" {
  name                        = var.data_collection_rule_name
  resource_group_name         = var.resource_group_name
  location                    = var.location
  data_collection_endpoint_id = azurerm_monitor_data_collection_endpoint.DCE.id
  description = var.description
  tags =  local.tags
  dynamic "destinations" {
     for_each = length(var.destinations) > 0 ? [var.destinations] : []
    content {
        dynamic "azure_monitor_metrics" {
          for_each = lookup(destinations.value, "azure_monitor_metrics", {}) != {} ? [1] : []
            content {
               name                        = lookup(destinations.value.azure_monitor_metrics, "name", null)
                }
              }
        dynamic "event_hub" {
          for_each = lookup(destinations.value, "event_hub", {}) != {} ? [1] : []
          content {
            event_hub_id                = lookup(destinations.value.event_hub, "event_hub_id", null)
            name                        = lookup(destinations.value.event_hub, "name", null)
        }
      }
       dynamic "event_hub_direct" {
          for_each = lookup(destinations.value, "event_hub_direct", {}) != {} ? [1] : []
        content {
            event_hub_id                = lookup(destinations.value.event_hub_direct, "event_hub_id", null)
            name                        = lookup(destinations.value.event_hub_direct, "name", null)
        }
       }
        dynamic "log_analytics" {
          for_each = lookup(destinations.value, "log_analytics", {}) != {} ? [1] : []
          content {
            workspace_resource_id       = lookup(destinations.value.log_analytics, "workspace_resource_id", null)
            name                        = lookup(destinations.value.log_analytics, "name", null)
          }
        }
        dynamic "monitor_account" {
          for_each = lookup(destinations.value, "monitor_account", {}) != {} ? [1] : []
          content{
            monitor_account_id          = lookup(destinations.value.monitor_account, "monitor_account_id", null)
            name                        = lookup(destinations.value.monitor_account, "name", null)
          }
        }
        dynamic "storage_blob" {
          for_each = lookup(destinations.value, "storage_blob", {}) != {} ? [1] : []
          content {
            container_name              = lookup(destinations.value.storage_blob, "container_name", null)
            name                        = lookup(destinations.value.storage_blob, "name", null)
            storage_account_id          = lookup(destinations.value.storage_blob, "storage_account_id", null)
          }
        }
        dynamic "storage_blob_direct" {
          for_each = lookup(destinations.value, "storage_blob_direct", {}) != {} ? [1] : []
          content {
            container_name              = lookup(destinations.value.storage_blob_direct, "container_name", null)
            name                        = lookup(destinations.value.storage_blob_direct, "name", null)
            storage_account_id          = lookup(destinations.value.storage_blob_direct, "storage_account_id", null)
        }
      }
    }
  }
  dynamic "data_flow" {
    for_each = var.data_flows
    content {
      streams      = data_flow.value.streams
      destinations = data_flow.value.destinations
      built_in_transform = lookup(data_flow.value, "built_in_transform", null)
      output_stream = lookup(data_flow.value, "output_stream", null)
      transform_kql = lookup(data_flow.value, "transform_kql", null)
    }
  }
  dynamic "data_sources" {
    for_each = length(var.data_sources) > 0 ? [var.data_sources] : []
    #for_each = var.data_sources == null ? [] : var.data_sources
     content {
      dynamic "syslog"{
        for_each = lookup(data_sources.value, "syslog", {}) != {} ? [1] : []
          content {
            streams                     = lookup(data_sources.value.syslog, "streams", null)
            name                        = lookup(data_sources.value.syslog, "name", null)
            facility_names              = lookup(data_sources.value.syslog, "facility_names", null)
            log_levels                  = lookup(data_sources.value.syslog, "log_levels", null)
          }
        }
      dynamic "data_import" {
         for_each = lookup(data_sources.value, "data_import", {}) != {} ? [1] : []
         content {
          dynamic "event_hub_data_source" {
            for_each = lookup(data_sources.value.data_import, "event_hub_data_source", {}) != {} ? [1] : []
            content {
              stream                    = lookup(data_sources.value.data_import.event_hub_data_source, "stream", null)
              name                       = lookup(data_sources.value.data_import.event_hub_data_source, "name", null)
              consumer_group             = lookup(data_sources.value.data_import.event_hub_data_source, "consumer_group", null)
              }
            }
          }
        }
      dynamic "extension" {
        for_each = lookup(data_sources.value, "extension", {}) != {} ? [1] : []
          content {
            streams                       = lookup(data_sources.value.extension, "streams", null)
            name                          = lookup(data_sources.value.extension, "name", null)
            extension_name                = lookup(data_sources.value.extension, "extension_name", null)
            extension_json                = lookup(data_sources.value.extension, "extension_json", null)
            input_data_sources            = lookup(data_sources.value.extension, "input_data_sources", null)
          }
        }
      dynamic "iis_log" {
        for_each = lookup(data_sources.value, "iis_log", {}) != {} ? [1] : []
          content {
            streams                     = lookup(data_sources.value.iis_log, "streams", null)
            name                        = lookup(data_sources.value.iis_log, "name", null)
            log_directories              = lookup(data_sources.value.iis_log, "log_directories", null)
          }
        }
      dynamic "log_file" {
          for_each = lookup(data_sources.value, "log_file", {}) != {} ? [1] : []
            content {
              streams                     = lookup(data_sources.value.log_file, "streams", null)
              name                        = lookup(data_sources.value.log_file, "name", null)
              file_patterns              = lookup(data_sources.value.log_file, "file_patterns", null)
              format                     = lookup(data_sources.value.log_file, "format", null)
                dynamic "settings"{
                  for_each = lookup(data_sources.value.data_import, "settings", {}) != {} ? [1] : []
                  content {
                    dynamic "text" {
                      for_each = lookup(data_sources.value.data_import.settings, "text", {}) != {} ? [1] : []
                          content {
                    record_start_timestamp_format      = lookup(data_sources.value.log_file.settings.text, "record_start_timestamp_format", null)
                      }
                    }
                  }
                }
              }
            }
      dynamic "performance_counter" {
        for_each = lookup(data_sources.value, "performance_counter", {}) != {} ? [1] : []
            content {
              streams                          = lookup(data_sources.value.performance_counter, "streams", null)
              name                             = lookup(data_sources.value.performance_counter, "name", null)
              counter_specifiers               = lookup(data_sources.value.performance_counter, "counter_specifiers", null)
              sampling_frequency_in_seconds    = lookup(data_sources.value.performance_counter, "sampling_frequency_in_seconds", null)
              }
            }
      dynamic "platform_telemetry"  {
        for_each = lookup(data_sources.value, "platform_telemetry", {}) != {} ? [1] : []
          content {
            streams                     = lookup(data_sources.value.platform_telemetry, "streams", null)
            name                        = lookup(data_sources.value.platform_telemetry, "name", null)
            }
          }
      dynamic "prometheus_forwarder" {
        for_each = lookup(data_sources.value, "prometheus_forwarder", {}) != {} ? [1] : []
          content {
            streams                     = lookup(data_sources.value.prometheus_forwarder, "streams", null)
            name                        = lookup(data_sources.value.prometheus_forwarder, "name", null)
              dynamic "label_include_filter"{
                for_each = lookup(data_sources.value.prometheus_forwarder, "label_include_filter", {}) != {} ? [1] : []
                  content {
                    label        = lookup(data_sources.value.prometheus_forwarder.label_include_filter, "label", null)
                    value        = lookup(data_sources.value.prometheus_forwarder.label_include_filter, "value", null)
                  }
                }
              }
            }
      dynamic "windows_event_log"  {
        for_each = lookup(data_sources.value, "windows_event_log", {}) != {} ? [1] : []
          content {
            streams                     = lookup(data_sources.value.windows_event_log, "streams", null)
            name                        = lookup(data_sources.value.windows_event_log, "name", null)
            x_path_queries              = lookup(data_sources.value.windows_event_log, "x_path_queries", null)
            }
           }
      dynamic "windows_firewall_log"  {
                for_each = lookup(data_sources.value, "windows_firewall_log", {}) != {} ? [1] : []
                content  {
                streams                     = lookup(data_sources.value.windows_firewall_log, "streams", null)
                name                        = lookup(data_sources.value.windows_firewall_log, "name", null)
                }
              }
     }
  }
  dynamic "stream_declaration" {
        for_each = var.stream_declaration != null ? [1] : []
        content {
          stream_name = stream_declaration.value.stream_name
          dynamic "column" {
            for_each = stream_declaration.value.columns
            content {
              name = column.value.name
              type = column.value.type
            }
          }
      }
  }
  dynamic "identity" {
        for_each = var.identity_type != null ? ["identity"] : []
        content {
          type = var.identity_type
          # Avoid perpetual changes if SystemAssigned and identity_ids is not null
          identity_ids = try(length(data.azurerm_user_assigned_identity.uami_list), null) == null ? [] : values(data.azurerm_user_assigned_identity.uami_list)[*].id
        }
      }
    depends_on = [azurerm_monitor_data_collection_endpoint.DCE]
}

Hi @raj.vedachalam as the OP you should be able to mark your last post including the working code as the solution to indicate it is solved and to help direct people to the answer if they are searching for similar issues.