diff --git a/config/config.go b/config/config.go index f2c8b41b5..012fcee6f 100644 --- a/config/config.go +++ b/config/config.go @@ -155,6 +155,17 @@ func (c *Config) Validate() error { } dupped = nil + // Make sure all dependsOn are valid in resources + for n, r := range resources { + for _, d := range r.DependsOn { + if _, ok := resources[d]; !ok { + errs = append(errs, fmt.Errorf( + "%s: resource depends on non-existent resource '%s'", + n, d)) + } + } + } + for source, vs := range vars { for _, v := range vs { rv, ok := v.(*ResourceVariable) diff --git a/config/config_test.go b/config/config_test.go index 9b141fa8f..508a281ab 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -16,6 +16,13 @@ func TestConfigValidate(t *testing.T) { } } +func TestConfigValidate_badDependsOn(t *testing.T) { + c := testConfig(t, "validate-bad-depends-on") + if err := c.Validate(); err == nil { + t.Fatal("should not be valid") + } +} + func TestConfigValidate_badMultiResource(t *testing.T) { c := testConfig(t, "validate-bad-multi-resource") if err := c.Validate(); err == nil { diff --git a/config/test-fixtures/validate-bad-depends-on/main.tf b/config/test-fixtures/validate-bad-depends-on/main.tf new file mode 100644 index 000000000..60ca18c7e --- /dev/null +++ b/config/test-fixtures/validate-bad-depends-on/main.tf @@ -0,0 +1,3 @@ +resource "aws_instance" "web" { + depends_on = ["aws_instance.db"] +} diff --git a/config/test-fixtures/validate-good/main.tf b/config/test-fixtures/validate-good/main.tf index 965877a10..5a4f87159 100644 --- a/config/test-fixtures/validate-good/main.tf +++ b/config/test-fixtures/validate-good/main.tf @@ -32,4 +32,6 @@ resource aws_instance "web" { device_index = 0 description = "Main network interface" } + + depends_on = ["aws_security_group.firewall"] }