From 421a188904f918c6ff6483372ed3805edfa26cbb Mon Sep 17 00:00:00 2001 From: LeoCie Date: Tue, 28 Oct 2025 10:56:30 +0000 Subject: [PATCH] Improve wording of for_each key dependency error when using a set --- .changes/v1.15/BUG FIXES-20251028-104850.yaml | 5 +++++ internal/terraform/eval_for_each.go | 2 +- internal/terraform/eval_for_each_test.go | 6 +++--- 3 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 .changes/v1.15/BUG FIXES-20251028-104850.yaml diff --git a/.changes/v1.15/BUG FIXES-20251028-104850.yaml b/.changes/v1.15/BUG FIXES-20251028-104850.yaml new file mode 100644 index 000000000000..54abc637c18d --- /dev/null +++ b/.changes/v1.15/BUG FIXES-20251028-104850.yaml @@ -0,0 +1,5 @@ +kind: BUG FIXES +body: Improve wording of for_each key dependency error when using a set +time: 2025-10-28T10:48:50.112437+00:00 +custom: + Issue: "" diff --git a/internal/terraform/eval_for_each.go b/internal/terraform/eval_for_each.go index 0abac36dda13..e1113d2c8c51 100644 --- a/internal/terraform/eval_for_each.go +++ b/internal/terraform/eval_for_each.go @@ -220,7 +220,7 @@ func (ev *forEachEvaluator) ensureKnownForResource(forEachVal cty.Value) tfdiags var diags tfdiags.Diagnostics ty := forEachVal.Type() const errInvalidUnknownDetailMap = "The \"for_each\" map includes keys derived from resource attributes that cannot be determined until apply, and so Terraform cannot determine the full set of keys that will identify the instances of this resource.\n\nWhen working with unknown values in for_each, it's better to define the map keys statically in your configuration and place apply-time results only in the map values.\n\nAlternatively, you could use the -target planning option to first apply only the resources that the for_each value depends on, and then apply a second time to fully converge." - const errInvalidUnknownDetailSet = "The \"for_each\" set includes values derived from resource attributes that cannot be determined until apply, and so Terraform cannot determine the full set of keys that will identify the instances of this resource.\n\nWhen working with unknown values in for_each, it's better to use a map value where the keys are defined statically in your configuration and where only the values contain apply-time results.\n\nAlternatively, you could use the -target planning option to first apply only the resources that the for_each value depends on, and then apply a second time to fully converge." + const errInvalidUnknownDetailSet = "The \"for_each\" set includes elements derived from resource attributes that cannot be determined until apply, and so Terraform cannot determine the full set of keys that will identify the instances of this resource.\n\nWhen working with unknown values in for_each, it's better to use a map value where the map keys are defined statically in your configuration and only the map values contain apply-time results.\n\nAlternatively, you could use the -target planning option to first apply only the resources that the for_each value depends on, and then apply a second time to fully converge." if !forEachVal.IsKnown() { var detailMsg string diff --git a/internal/terraform/eval_for_each_test.go b/internal/terraform/eval_for_each_test.go index 81017804e55a..dbbb3d6aaf42 100644 --- a/internal/terraform/eval_for_each_test.go +++ b/internal/terraform/eval_for_each_test.go @@ -132,7 +132,7 @@ func TestEvaluateForEachExpression_errors(t *testing.T) { "unknown string set": { hcltest.MockExprLiteral(cty.UnknownVal(cty.Set(cty.String))), "Invalid for_each argument", - "set includes values derived from resource attributes that cannot be determined until apply", + "set includes elements derived from resource attributes that cannot be determined until apply", true, false, false, }, "unknown map": { @@ -174,13 +174,13 @@ func TestEvaluateForEachExpression_errors(t *testing.T) { "set containing unknown value": { hcltest.MockExprLiteral(cty.SetVal([]cty.Value{cty.UnknownVal(cty.String)})), "Invalid for_each argument", - "set includes values derived from resource attributes that cannot be determined until apply", + "set includes elements derived from resource attributes that cannot be determined until apply", true, false, false, }, "set containing dynamic unknown value": { hcltest.MockExprLiteral(cty.SetVal([]cty.Value{cty.UnknownVal(cty.DynamicPseudoType)})), "Invalid for_each argument", - "set includes values derived from resource attributes that cannot be determined until apply", + "set includes elements derived from resource attributes that cannot be determined until apply", true, false, false, }, "set containing sensitive values": {