diff --git a/bundle/deploy/terraform/init.go b/bundle/deploy/terraform/init.go index 0ce15acb..eb3e99d1 100644 --- a/bundle/deploy/terraform/init.go +++ b/bundle/deploy/terraform/init.go @@ -70,6 +70,23 @@ func (m *initialize) findExecPath(ctx context.Context, b *bundle.Bundle, tf *con return tf.ExecPath, nil } +// This function inherits some environment variables for Terraform CLI. +func inheritEnvVars(env map[string]string) error { + // Include $HOME in set of environment variables to pass along. + home, ok := os.LookupEnv("HOME") + if ok { + env["HOME"] = home + } + + // Include $TF_CLI_CONFIG_FILE to override terraform provider in development. + configFile, ok := os.LookupEnv("TF_CLI_CONFIG_FILE") + if ok { + env["TF_CLI_CONFIG_FILE"] = configFile + } + + return nil +} + // This function sets temp dir location for terraform to use. If user does not // specify anything here, we fall back to a `tmp` directory in the bundle's cache // directory @@ -145,10 +162,9 @@ func (m *initialize) Apply(ctx context.Context, b *bundle.Bundle) error { return err } - // Include $HOME in set of environment variables to pass along. - home, ok := os.LookupEnv("HOME") - if ok { - env["HOME"] = home + err = inheritEnvVars(env) + if err != nil { + return err } // Set the temporary directory environment variables diff --git a/bundle/deploy/terraform/init_test.go b/bundle/deploy/terraform/init_test.go index aafe5660..79e18170 100644 --- a/bundle/deploy/terraform/init_test.go +++ b/bundle/deploy/terraform/init_test.go @@ -272,3 +272,19 @@ func TestSetProxyEnvVars(t *testing.T) { require.NoError(t, err) assert.ElementsMatch(t, []string{"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"}, maps.Keys(env)) } + +func TestInheritEnvVars(t *testing.T) { + env := map[string]string{} + + t.Setenv("HOME", "/home/testuser") + t.Setenv("TF_CLI_CONFIG_FILE", "/tmp/config.tfrc") + + err := inheritEnvVars(env) + + require.NoError(t, err) + + require.Equal(t, map[string]string{ + "HOME": "/home/testuser", + "TF_CLI_CONFIG_FILE": "/tmp/config.tfrc", + }, env) +}