Exclude part of dynamic block if specific env variable is set. Or need advice with better solution. azurerm_application_gateway

Hello everyone,

I need some help. I want to reuse same manifest to provision azurerm_application_gateway and use dynamic blocks for some parts of this resource for example for http_listener. Content of http_listener dynamic block I want to store in variables.tf it will be list/array of http_listener’s. Also I want to have ability to exclude some listeners from this list depends on which env I’m provisioning. For example I have following env variable TF_VAR_env=DEV and for DEV I want to exclude 2 of 3 listeners or exactly one of these listeners. I’m never used dynamic blocks before. I’m newbie with these stuff so I will appreciate any advices. Maybe my approach completely bad and you can advice me something better. Thanks in advance. Here example of my manifest main.tf:{

resource "azurerm_application_gateway" "test" {
    dynamic "http_listener" {
      for_each = var.http_listeners
      content {
        frontend_ip_configuration_name = http_listener.value.frontend_ip_configuration_name
        frontend_port_name             = http_listener.value.frontend_port_name
        host_name                      = http_listener.value.host_name
        host_names                     = http_listener.value.host_names
        name                           = http_listener.value.name
        protocol                       = http_listener.value.protocol
        require_sni                    = http_listener.value.require_sni
        ssl_certificate_name           = http_listener.value.ssl_certificate_name
      }
    }
}

My variables.tf:

variable "http_listeners" {
  default     = [
{
    frontend_ip_configuration_name = "value"
    frontend_port_name             = "value"
    host_name                      = "value"
    host_names                     = "value"
    name                           = "listner_PROD"
    protocol                       = "value"
    require_sni                    = "value"
    ssl_certificate_name           = "value"
},
{
    frontend_ip_configuration_name = "value"
    frontend_port_name             = "value"
    host_name                      = "value"
    host_names                     = "value"
    name                           = "listner_TEST"
    protocol                       = "value"
    require_sni                    = "value"
    ssl_certificate_name           = "value"
},
{
    frontend_ip_configuration_name = "value"
    frontend_port_name             = "value"
    host_name                      = "value"
    host_names                     = "value"
    name                           = "listner_DEV"
    protocol                       = "value"
    require_sni                    = "value"
    ssl_certificate_name           = "value"
},
  ]
}