Using Terraform For_Each and For commands to filter elements

I have a Terraform locals.tf file like the below snippet, which contains my environment definition.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>locals {
environments = [
"dev",
"test",
"preprod",
"prod"
]
}
</code>
<code>locals { environments = [ "dev", "test", "preprod", "prod" ] } </code>
locals {
    environments = [
    "dev", 
    "test", 
    "preprod",
    "prod"
   ]
}

I am now looking to create an identical resource, an Azure monitor metric alert in this case, for each of the listed environments (except prod). I am attempting to do so using a For_Each loop in the resource block of my main.tf file, a snippet of which is shown below:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>resource "azurerm_monitor_metric_alert" "main" {
for_each = {
[for s in toset(local.environments) : if s != "prod"]
}
name = "${each.key}-metric-alert"
resource_group_name = azurerm_resource_group.rg.name
scopes = [azurerm_storage_account.to_monitor[each.key].id]
[..........ADDITIONAL NON-PROD RESOURCE CONFIG...........]
</code>
<code>resource "azurerm_monitor_metric_alert" "main" { for_each = { [for s in toset(local.environments) : if s != "prod"] } name = "${each.key}-metric-alert" resource_group_name = azurerm_resource_group.rg.name scopes = [azurerm_storage_account.to_monitor[each.key].id] [..........ADDITIONAL NON-PROD RESOURCE CONFIG...........] </code>
resource "azurerm_monitor_metric_alert" "main" {
  for_each = {
     [for s in toset(local.environments) : if s != "prod"] 
  }   
  name = "${each.key}-metric-alert"
  resource_group_name = azurerm_resource_group.rg.name
  scopes              = [azurerm_storage_account.to_monitor[each.key].id]
  [..........ADDITIONAL NON-PROD RESOURCE CONFIG...........]

For the prod environment though, the monitor alert requires a slightly different set of configuration from the others, and so what I’d like to do is filter out that environment from the locals.tf file and then using another azurerm_monitor_metric_alert resource block, configure it along the lines of the below snippet:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> resource "azurerm_monitor_metric_alert" "main" {
for_each = {
[for s in toset(local.environments) : if s == "prod"]
}
name = "${each.key}-metric-alert"
resource_group_name = azurerm_resource_group.rg.name
scopes = [azurerm_storage_account.to_monitor[each.key].id]
[..........ADDITIONAL PROD CONFIG...........]
</code>
<code> resource "azurerm_monitor_metric_alert" "main" { for_each = { [for s in toset(local.environments) : if s == "prod"] } name = "${each.key}-metric-alert" resource_group_name = azurerm_resource_group.rg.name scopes = [azurerm_storage_account.to_monitor[each.key].id] [..........ADDITIONAL PROD CONFIG...........] </code>
   resource "azurerm_monitor_metric_alert" "main" {
      for_each = {
         [for s in toset(local.environments) : if s == "prod"] 
      } 
      name = "${each.key}-metric-alert"
      resource_group_name = azurerm_resource_group.rg.name
      scopes              = [azurerm_storage_account.to_monitor[each.key].id] 
      [..........ADDITIONAL PROD CONFIG...........]

I have the tried the above and various other implementations, but unfortunately I simply can’t get this to work. Would greatly appreciate some assistance here.

Recognized by Microsoft Azure Collective

1

You didn’t specify what error you get but your for loops are missing one argument:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[for s in toset(local.environments) : s if s != "prod"]
</code>
<code>[for s in toset(local.environments) : s if s != "prod"] </code>
[for s in toset(local.environments) : s if s != "prod"]

And:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[for s in toset(local.environments) : s if s == "prod"]
</code>
<code>[for s in toset(local.environments) : s if s == "prod"] </code>
[for s in toset(local.environments) : s if s == "prod"]

Also I`m not sure if you need this toset there. It might be required to put the whole for loop inside toset and not local.environments.

There are a few different problems with the following expression:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> for_each = {
[for s in toset(local.environments) : if s != "prod"]
}
</code>
<code> for_each = { [for s in toset(local.environments) : if s != "prod"] } </code>
  for_each = {
     [for s in toset(local.environments) : if s != "prod"] 
  }
  1. The for expression is missing its result clause after the colon. If you intend to just return s directly then you can just specify that variable alone between the colon and the if keyword:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>[for s in toset(local.environments) : s if s != "prod"]
    </code>
    <code>[for s in toset(local.environments) : s if s != "prod"] </code>
    [for s in toset(local.environments) : s if s != "prod"] 
    
  2. The braces { } around this for expression are not valid. That syntax is for constructing an object-typed value, but for that to work you’d need to specify an attribute to assign the result of the for expression to.

    Since var.environments seems to be just a bunch of strings, I expect you probably want to construct a set of strings directly, without any wrapping map/object, like this:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>for_each = toset([for s in local.environments : s if s != "prod"])
    </code>
    <code>for_each = toset([for s in local.environments : s if s != "prod"]) </code>
    for_each = toset([for s in local.environments : s if s != "prod"])
    
  3. The inner toset you previously had for local.environments is not really adding anything here because a for expression effectively treats a set of strings like a list of those strings in lexical order. You can safely remove it like I did in my most recent example above, and that would be sufficient for this particular use of `local.environments.

    If you want to communicate that local.environments itself is semantically a set — which might be helpful for future maintainers of your module trying to understand what you intended — then you might choose to place the toset in the definition of the local value instead:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>locals {
    environments = toset([
    "dev",
    "test",
    "preprod",
    "prod",
    ])
    }
    </code>
    <code>locals { environments = toset([ "dev", "test", "preprod", "prod", ]) } </code>
    locals {
      environments = toset([
        "dev", 
        "test", 
        "preprod",
        "prod",
      ])
    }
    

    Doing this would have no effect on the for_each expression this question is about though, so this would be a meaningful change only if local.environments is used in other locations that should also treat it as a set, and that’s beyond the scope of this question.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật