diff --git a/command/meta_backend.go b/command/meta_backend.go index ab39348a5..ea96e7455 100644 --- a/command/meta_backend.go +++ b/command/meta_backend.go @@ -237,6 +237,12 @@ func (m *Meta) backendConfig(opts *BackendOpts) (*config.Backend, error) { backend.RawConfig = backend.RawConfig.Merge(rc) } + // Validate the backend early. We have to do this before the normal + // config validation pass since backend loading happens earlier. + if errs := backend.Validate(); len(errs) > 0 { + return nil, multierror.Append(nil, errs...) + } + // Return the configuration which may or may not be set return backend, nil } diff --git a/command/meta_backend_test.go b/command/meta_backend_test.go index 02a1d2c40..dda5db644 100644 --- a/command/meta_backend_test.go +++ b/command/meta_backend_test.go @@ -267,6 +267,24 @@ func TestMetaBackend_emptyLegacyRemote(t *testing.T) { } } +// Verify that interpolations result in an error +func TestMetaBackend_configureInterpolation(t *testing.T) { + // Create a temporary working directory that is empty + td := tempDir(t) + copy.CopyDir(testFixturePath("backend-new-interp"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() + + // Setup the meta + m := testMetaBackend(t, nil) + + // Get the backend + _, err := m.Backend(&BackendOpts{Init: true}) + if err == nil { + t.Fatal("should error") + } +} + // Newly configured backend func TestMetaBackend_configureNew(t *testing.T) { // Create a temporary working directory that is empty diff --git a/command/test-fixtures/backend-new-interp/main.tf b/command/test-fixtures/backend-new-interp/main.tf new file mode 100644 index 000000000..136d0f3ba --- /dev/null +++ b/command/test-fixtures/backend-new-interp/main.tf @@ -0,0 +1,7 @@ +variable "foo" { default = "bar" } + +terraform { + backend "local" { + path = "${var.foo}" + } +}