diff --git a/terraform/context.go b/terraform/context.go index 75c922d98..d481bdc17 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -523,7 +523,7 @@ func (c *walkContext) Walk() error { // On Apply, we prune so that we don't do outputs if we destroyed mod.prune() } - if len(mod.Resources) == 0 { + if len(mod.Resources) == 0 && len(conf.Resources) != 0 { mod.Outputs = nil return nil } diff --git a/terraform/context_test.go b/terraform/context_test.go index 99b031853..b19d7945c 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -913,6 +913,35 @@ func TestContextApply(t *testing.T) { } } +func TestContextApply_emptyModule(t *testing.T) { + m := testModule(t, "apply-empty-module") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + ctx := testContext(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + if _, err := ctx.Plan(nil); err != nil { + t.Fatalf("err: %s", err) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(state.String()) + actual = strings.Replace(actual, " ", "", -1) + expected := strings.TrimSpace(testTerraformApplyEmptyModuleStr) + if actual != expected { + t.Fatalf("bad: \n%s\nexpect:\n%s", actual, expected) + } +} + func TestContextApply_createBeforeDestroy(t *testing.T) { m := testModule(t, "apply-good-create-before") p := testProvider("aws") diff --git a/terraform/state.go b/terraform/state.go index 529f33983..eb985c37a 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -294,12 +294,12 @@ func (m *ModuleState) GoString() string { } func (m *ModuleState) String() string { - if len(m.Resources) == 0 { - return "" - } - var buf bytes.Buffer + if len(m.Resources) == 0 { + buf.WriteString("") + } + names := make([]string, 0, len(m.Resources)) for name, _ := range m.Resources { names = append(names, name) diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index e9bc6ca7f..6ebd2f53f 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -164,6 +164,21 @@ aws_instance.foo: type = aws_instance ` +const testTerraformApplyEmptyModuleStr = ` + +Outputs: + +end = XXXX + +module.child: + +Outputs: + +aws_access_key = YYYYY +aws_route53_zone_id = XXXX +aws_secret_key = ZZZZ +` + const testTerraformApplyDependsCreateBeforeStr = ` aws_instance.lb: ID = foo diff --git a/terraform/test-fixtures/apply-empty-module/child/main.tf b/terraform/test-fixtures/apply-empty-module/child/main.tf new file mode 100644 index 000000000..6db38ea16 --- /dev/null +++ b/terraform/test-fixtures/apply-empty-module/child/main.tf @@ -0,0 +1,11 @@ +output "aws_route53_zone_id" { + value = "XXXX" +} + +output "aws_access_key" { + value = "YYYYY" +} + +output "aws_secret_key" { + value = "ZZZZ" +} diff --git a/terraform/test-fixtures/apply-empty-module/main.tf b/terraform/test-fixtures/apply-empty-module/main.tf new file mode 100644 index 000000000..50ce84f0b --- /dev/null +++ b/terraform/test-fixtures/apply-empty-module/main.tf @@ -0,0 +1,7 @@ +module "child" { + source = "./child" +} + +output "end" { + value = "${module.child.aws_route53_zone_id}" +}