diff --git a/terraform/context.go b/terraform/context.go index 47b9d1fae9..e2d8bbd33d 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -1611,19 +1611,25 @@ func (c *walkContext) computeResourceVariable( // Get the relevant module module := c.Context.state.ModuleByPath(c.Path) - r, ok := module.Resources[id] - if !ok { - if v.Multi && v.Index == 0 { + var r *ResourceState + if module != nil { + var ok bool + r, ok = module.Resources[id] + if !ok && v.Multi && v.Index == 0 { r, ok = module.Resources[v.ResourceId()] } if !ok { - return "", fmt.Errorf( - "Resource '%s' not found for variable '%s'", - id, - v.FullKey()) + r = nil } } + if r == nil { + return "", fmt.Errorf( + "Resource '%s' not found for variable '%s'", + id, + v.FullKey()) + } + if r.Primary == nil { goto MISSING } diff --git a/terraform/context_test.go b/terraform/context_test.go index ef4b3f5b12..5ad260763a 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -4110,6 +4110,22 @@ func TestContextRefresh_modules(t *testing.T) { } } +func TestContextRefresh_moduleInputComputedOutput(t *testing.T) { + m := testModule(t, "refresh-module-input-computed-output") + p := testProvider("aws") + p.DiffFn = testDiffFn + ctx := testContext(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + if _, err := ctx.Refresh(); err != nil { + t.Fatalf("err: %s", err) + } +} + // GH-70 func TestContextRefresh_noState(t *testing.T) { p := testProvider("aws") diff --git a/terraform/test-fixtures/refresh-module-input-computed-output/child/main.tf b/terraform/test-fixtures/refresh-module-input-computed-output/child/main.tf new file mode 100644 index 0000000000..fff03a60fc --- /dev/null +++ b/terraform/test-fixtures/refresh-module-input-computed-output/child/main.tf @@ -0,0 +1,9 @@ +variable "input" {} + +resource "aws_instance" "foo" { + foo = "${var.input}" +} + +output "foo" { + value = "${aws_instance.foo.foo}" +} diff --git a/terraform/test-fixtures/refresh-module-input-computed-output/main.tf b/terraform/test-fixtures/refresh-module-input-computed-output/main.tf new file mode 100644 index 0000000000..3a0576434f --- /dev/null +++ b/terraform/test-fixtures/refresh-module-input-computed-output/main.tf @@ -0,0 +1,8 @@ +module "child" { + input = "${aws_instance.bar.foo}" + source = "./child" +} + +resource "aws_instance" "bar" { + compute = "foo" +}