diff --git a/config/config.go b/config/config.go index 67e0a2e7e..ef8be866d 100644 --- a/config/config.go +++ b/config/config.go @@ -255,6 +255,26 @@ func (c *Config) Validate() error { // Validate resources for n, r := range resources { + // Verify count variables + for _, v := range r.RawCount.Variables { + switch v.(type) { + case *ModuleVariable: + errs = append(errs, fmt.Errorf( + "%s: resource count can't reference module variable: %s", + n, + v.FullKey())) + case *ResourceVariable: + errs = append(errs, fmt.Errorf( + "%s: resource count can't reference resource variable: %s", + n, + v.FullKey())) + case *UserVariable: + // Good + default: + panic("Unknown type in count var: " + n) + } + } + for _, d := range r.DependsOn { if _, ok := resources[d]; !ok { errs = append(errs, fmt.Errorf( diff --git a/config/config_test.go b/config/config_test.go index a31df6b5a..636af0b58 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -53,6 +53,27 @@ func TestConfigValidate_badDependsOn(t *testing.T) { } } +func TestConfigValidate_countModuleVar(t *testing.T) { + c := testConfig(t, "validate-count-module-var") + if err := c.Validate(); err == nil { + t.Fatal("should not be valid") + } +} + +func TestConfigValidate_countResourceVar(t *testing.T) { + c := testConfig(t, "validate-count-resource-var") + if err := c.Validate(); err == nil { + t.Fatal("should not be valid") + } +} + +func TestConfigValidate_countUserVar(t *testing.T) { + c := testConfig(t, "validate-count-user-var") + if err := c.Validate(); err != nil { + t.Fatalf("err: %s", err) + } +} + func TestConfigValidate_dupModule(t *testing.T) { c := testConfig(t, "validate-dup-module") if err := c.Validate(); err == nil { diff --git a/config/test-fixtures/validate-count-module-var/main.tf b/config/test-fixtures/validate-count-module-var/main.tf new file mode 100644 index 000000000..16d901e3a --- /dev/null +++ b/config/test-fixtures/validate-count-module-var/main.tf @@ -0,0 +1,7 @@ +module "foo" { + source = "./bar" +} + +resource "aws_instance" "web" { + count = "${module.foo.bar}" +} diff --git a/config/test-fixtures/validate-count-resource-var/main.tf b/config/test-fixtures/validate-count-resource-var/main.tf new file mode 100644 index 000000000..e352a916c --- /dev/null +++ b/config/test-fixtures/validate-count-resource-var/main.tf @@ -0,0 +1,5 @@ +resource "aws_instance" "foo" {} + +resource "aws_instance" "web" { + count = "${aws_instance.foo.bar}" +} diff --git a/config/test-fixtures/validate-count-user-var/main.tf b/config/test-fixtures/validate-count-user-var/main.tf new file mode 100644 index 000000000..7b27c2202 --- /dev/null +++ b/config/test-fixtures/validate-count-user-var/main.tf @@ -0,0 +1,5 @@ +variable "foo" {} + +resource "aws_instance" "web" { + count = "${var.foo}" +}