diff --git a/addrs/module_call.go b/addrs/module_call.go index c496cdc46..02163ef7d 100644 --- a/addrs/module_call.go +++ b/addrs/module_call.go @@ -80,6 +80,15 @@ type AbsModuleCallOutput struct { Name string } +// ModuleCallOutput returns the referenceable ModuleCallOutput for this +// particular instance. +func (co AbsModuleCallOutput) ModuleCallOutput() ModuleCallOutput { + return ModuleCallOutput{ + Call: co.Call.Call, + Name: co.Name, + } +} + func (co AbsModuleCallOutput) String() string { return fmt.Sprintf("%s.%s", co.Call.String(), co.Name) } diff --git a/states/state_string.go b/states/state_string.go index 1c326ece9..680acf7a4 100644 --- a/states/state_string.go +++ b/states/state_string.go @@ -55,9 +55,7 @@ func (s *State) String() string { buf.WriteByte('.') buf.WriteString(step.Name) if step.InstanceKey != addrs.NoKey { - buf.WriteByte('[') buf.WriteString(step.InstanceKey.String()) - buf.WriteByte(']') } } buf.WriteString(":\n") diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 98014080b..e2add81e4 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -10940,3 +10940,78 @@ func TestContext2Apply_ProviderMeta_refreshdata_setInvalid(t *testing.T) { t.Errorf("Expected unsupported argument error, none received") } } + +func TestContext2Apply_expandModuleVariables(t *testing.T) { + m := testModuleInline(t, map[string]string{ + "main.tf": ` +module "mod1" { + for_each = toset(["a"]) + source = "./mod" +} + +module "mod2" { + source = "./mod" + in = module.mod1["a"].out +} +`, + "mod/main.tf": ` +resource "aws_instance" "foo" { + foo = var.in +} + +variable "in" { + type = string + default = "default" +} + +output "out" { + value = aws_instance.foo.id +} +`, + }) + + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ + Config: m, + Providers: map[addrs.Provider]providers.Factory{ + addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p), + }, + }) + + _, diags := ctx.Plan() + if diags.HasErrors() { + t.Fatal(diags.ErrWithWarnings()) + } + + state, diags := ctx.Apply() + if diags.HasErrors() { + t.Fatal(diags.ErrWithWarnings()) + } + + expected := ` +module.mod1["a"]: + aws_instance.foo: + ID = foo + provider = provider["registry.terraform.io/hashicorp/aws"] + foo = default + type = aws_instance + + Outputs: + + out = foo +module.mod2: + aws_instance.foo: + ID = foo + provider = provider["registry.terraform.io/hashicorp/aws"] + foo = foo + type = aws_instance + + Dependencies: + module.mod1.aws_instance.foo` + + if state.String() != expected { + t.Fatalf("expected:\n%s\ngot:\n%s\n", expected, state) + } +} diff --git a/terraform/transform_reference.go b/terraform/transform_reference.go index 74df6628a..ef64eb2c1 100644 --- a/terraform/transform_reference.go +++ b/terraform/transform_reference.go @@ -262,6 +262,11 @@ func (m *ReferenceMap) References(v dag.Vertex) []dag.Vertex { subject = ri.ContainingResource() case addrs.ResourceInstancePhase: subject = ri.ContainingResource() + case addrs.AbsModuleCallOutput: + subject = ri.ModuleCallOutput() + default: + log.Printf("[WARN] ReferenceTransformer: reference not found: %q", subject) + continue } key = m.referenceMapKey(v, subject) }