diff --git a/terraform/context_test.go b/terraform/context_test.go index caee0f761..d4597f11f 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -5988,6 +5988,46 @@ func TestContext2Apply_vars(t *testing.T) { } } +func TestContext2Apply_varsEnv(t *testing.T) { + m := testModule(t, "apply-vars-env") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + // Set the env var + old := tempEnv(t, "TF_VAR_ami", "baz") + defer os.Setenv("TF_VAR_ami", old) + + w, e := ctx.Validate() + if len(w) > 0 { + t.Fatalf("bad: %#v", w) + } + if len(e) > 0 { + t.Fatalf("bad: %s", e) + } + + if _, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(testTerraformApplyVarsEnvStr) + if actual != expected { + t.Fatalf("bad: \n%s", actual) + } +} + func TestContext2Apply_createBefore_depends(t *testing.T) { m := testModule(t, "apply-depends-create-before") h := new(HookRecordApplyOrder) diff --git a/terraform/interpolate.go b/terraform/interpolate.go index aee283a78..b0af5f659 100644 --- a/terraform/interpolate.go +++ b/terraform/interpolate.go @@ -11,6 +11,12 @@ import ( "github.com/hashicorp/terraform/config/module" ) +const ( + // VarEnvPrefix is the prefix of variables that are read from + // the environment to set variables here. + VarEnvPrefix = "TF_VAR_" +) + // Interpolater is the structure responsible for determining the values // for interpolations such as `aws_instance.foo.bar`. type Interpolater struct { @@ -43,6 +49,11 @@ func (i *Interpolater) Values( } for _, v := range mod.Config().Variables { for k, val := range v.DefaultsMap() { + envKey := VarEnvPrefix + strings.TrimPrefix(k, "var.") + if v := os.Getenv(envKey); v != "" { + val = v + } + result[k] = ast.Variable{ Value: val, Type: ast.TypeString, diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index bb54ef6ac..38be7b9fd 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -47,6 +47,14 @@ func tempDir(t *testing.T) string { return dir } +// tempEnv lets you temporarily set an environment variable. It returns +// the old value that should be set via a defer. +func tempEnv(t *testing.T, k string, v string) string { + old := os.Getenv(k) + os.Setenv(k, v) + return old +} + func testConfig(t *testing.T, name string) *config.Config { c, err := config.Load(filepath.Join(fixtureDir, name, "main.tf")) if err != nil { @@ -621,6 +629,13 @@ aws_instance.foo: type = aws_instance ` +const testTerraformApplyVarsEnvStr = ` +aws_instance.bar: + ID = foo + foo = baz + type = aws_instance +` + const testTerraformPlanStr = ` DIFF: diff --git a/terraform/test-fixtures/apply-vars-env/main.tf b/terraform/test-fixtures/apply-vars-env/main.tf new file mode 100644 index 000000000..b686da065 --- /dev/null +++ b/terraform/test-fixtures/apply-vars-env/main.tf @@ -0,0 +1,7 @@ +variable "ami" { + default = "foo" +} + +resource "aws_instance" "bar" { + foo = "${var.ami}" +}