diff --git a/terraform/context.go b/terraform/context.go index 922cc3627..519146d72 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -473,8 +473,8 @@ func (c *walkContext) Walk() error { if m == nil { return nil } - config := m.Config() - if len(config.Outputs) == 0 { + conf := m.Config() + if len(conf.Outputs) == 0 { return nil } @@ -490,11 +490,18 @@ func (c *walkContext) Walk() error { } outputs := make(map[string]string) - for _, o := range config.Outputs { + for _, o := range conf.Outputs { if err := c.computeVars(o.RawConfig); err != nil { return err } vraw := o.RawConfig.Config()["value"] + if vraw == nil { + // This likely means that the result of the output is + // a computed variable. + if o.RawConfig.Raw["value"] != nil { + vraw = config.UnknownVariableValue + } + } if vraw != nil { outputs[o.Name] = vraw.(string) } diff --git a/terraform/context_test.go b/terraform/context_test.go index cb12b9cb3..023a0e62d 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -1513,6 +1513,29 @@ func TestContextPlan_moduleVar(t *testing.T) { } } +func TestContextPlan_moduleVarComputed(t *testing.T) { + m := testModule(t, "plan-module-var-computed") + p := testProvider("aws") + p.DiffFn = testDiffFn + ctx := testContext(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + plan, err := ctx.Plan(nil) + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(plan.String()) + expected := strings.TrimSpace(testTerraformPlanModuleVarComputedStr) + if actual != expected { + t.Fatalf("bad:\n%s", actual) + } +} + func TestContextPlan_nil(t *testing.T) { m := testModule(t, "plan-nil") p := testProvider("aws") diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 1215eb2f0..46a4e9f33 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -545,6 +545,23 @@ STATE: ` +const testTerraformPlanModuleVarComputedStr = ` +DIFF: + +CREATE: aws_instance.bar + foo: "" => "" + type: "" => "aws_instance" + +module.child: + CREATE: aws_instance.foo + foo: "" => "" + type: "" => "aws_instance" + +STATE: + + +` + const testTerraformPlanOrphanStr = ` DIFF: diff --git a/terraform/test-fixtures/plan-module-var-computed/child/main.tf b/terraform/test-fixtures/plan-module-var-computed/child/main.tf new file mode 100644 index 000000000..20a301330 --- /dev/null +++ b/terraform/test-fixtures/plan-module-var-computed/child/main.tf @@ -0,0 +1,7 @@ +resource "aws_instance" "foo" { + compute = "foo" +} + +output "num" { + value = "${aws_instance.foo.foo}" +} diff --git a/terraform/test-fixtures/plan-module-var-computed/main.tf b/terraform/test-fixtures/plan-module-var-computed/main.tf new file mode 100644 index 000000000..b38f538a2 --- /dev/null +++ b/terraform/test-fixtures/plan-module-var-computed/main.tf @@ -0,0 +1,7 @@ +module "child" { + source = "./child" +} + +resource "aws_instance" "bar" { + foo = "${module.child.num}" +}