Skip to content

Module for_each with for expression is not evaluated #7503

@pszypowicz

Description

@pszypowicz

When a module's for_each uses a for expression (e.g. { for k in var.list : k.name => k }), checkov stores it as a literal string and never expands the module. Direct map/set references work correctly.

Minimal reproducer (2 files)

modules/child/main.tf

variable "val" { type = string }
resource "terraform_data" "test" { input = var.val }

main.tf

variable "items" {
  type    = list(string)
  default = ["a", "b"]
}

module "child" {
  source   = "./modules/child"
  for_each = { for v in var.items : v => v }
  val      = each.value
}

Expected

Module expands to child["a"] and child["b"], resource resolves input.

Actual

Module child is not expanded. for_each stored as string:

"{for v in ['a', 'b'] : v :> v}"

Workaround

Use a direct map reference instead of a for expression:

variable "items" {
  type    = map(string)
  default = { a = "a", b = "b" }
}

module "child" {
  source   = "./modules/child"
  for_each = var.items
  val      = each.value
}

This expands child["a"] and child["b"] correctly.

Environment

Verified on main (8bd89be03). terraform validate passes on the reproducer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions