diff --git a/config/config.go b/config/config.go index d98ad854b..da2186b37 100644 --- a/config/config.go +++ b/config/config.go @@ -570,6 +570,15 @@ func (c *Config) Validate() error { } } } + + // Verify ignore_changes contains valid entries + for _, v := range r.Lifecycle.IgnoreChanges { + if strings.Contains(v, "*") && v != "*" { + errs = append(errs, fmt.Errorf( + "%s: ignore_changes does not support using a partial string "+ + "together with a wildcard: %s", n, v)) + } + } } for source, vs := range vars { diff --git a/config/config_test.go b/config/config_test.go index bbbb7fa06..92a6ea890 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -244,6 +244,20 @@ func TestConfigValidate_dupResource(t *testing.T) { } } +func TestConfigValidate_ignoreChanges(t *testing.T) { + c := testConfig(t, "validate-ignore-changes") + if err := c.Validate(); err != nil { + t.Fatalf("err: %s", err) + } +} + +func TestConfigValidate_ignoreChangesBad(t *testing.T) { + c := testConfig(t, "validate-ignore-changes-bad") + if err := c.Validate(); err == nil { + t.Fatal("should not be valid") + } +} + func TestConfigValidate_moduleNameBad(t *testing.T) { c := testConfig(t, "validate-module-name-bad") if err := c.Validate(); err == nil { diff --git a/config/test-fixtures/validate-ignore-changes-bad/main.tf b/config/test-fixtures/validate-ignore-changes-bad/main.tf new file mode 100644 index 000000000..85ab837d8 --- /dev/null +++ b/config/test-fixtures/validate-ignore-changes-bad/main.tf @@ -0,0 +1,21 @@ +variable "foo" { + default = "ami-abcd1234" +} + +variable "bar" { + default = "t2.micro" +} + +provider "aws" { + access_key = "foo" + secret_key = "bar" +} + +resource aws_instance "web" { + ami = "${var.foo}" + instance_type = "${var.bar}" + + lifecycle { + ignore_changes = ["ami", "instance*"] + } +} diff --git a/config/test-fixtures/validate-ignore-changes/main.tf b/config/test-fixtures/validate-ignore-changes/main.tf new file mode 100644 index 000000000..b0ca35b3b --- /dev/null +++ b/config/test-fixtures/validate-ignore-changes/main.tf @@ -0,0 +1,21 @@ +variable "foo" { + default = "ami-abcd1234" +} + +variable "bar" { + default = "t2.micro" +} + +provider "aws" { + access_key = "foo" + secret_key = "bar" +} + +resource aws_instance "web" { + ami = "${var.foo}" + instance_type = "${var.bar}" + + lifecycle { + ignore_changes = ["*"] + } +} diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index c9e09bedc..d0d9bcc2b 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -5034,6 +5034,47 @@ func TestContext2Apply_ignoreChangesWithDep(t *testing.T) { } } +func TestContext2Apply_ignoreChangesWildcard(t *testing.T) { + m := testModule(t, "apply-ignore-changes-wildcard") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + if p, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } else { + t.Logf(p.String()) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + mod := state.RootModule() + if len(mod.Resources) != 1 { + t.Fatalf("bad: %s", state) + } + + actual := strings.TrimSpace(state.String()) + // Expect no changes from original state + expected := strings.TrimSpace(` +aws_instance.foo: + ID = foo + required_field = set + type = aws_instance +`) + if actual != expected { + t.Fatalf("expected:\n%s\ngot:\n%s", expected, actual) + } +} + // https://github.com/hashicorp/terraform/issues/7378 func TestContext2Apply_destroyNestedModuleWithAttrsReferencingResource(t *testing.T) { m := testModule(t, "apply-destroy-nested-module-with-attrs") diff --git a/terraform/context_plan_test.go b/terraform/context_plan_test.go index 01b7773ed..76db4aa87 100644 --- a/terraform/context_plan_test.go +++ b/terraform/context_plan_test.go @@ -2326,6 +2326,56 @@ func TestContext2Plan_ignoreChanges(t *testing.T) { } } +func TestContext2Plan_ignoreChangesWildcard(t *testing.T) { + m := testModule(t, "plan-ignore-changes-wildcard") + p := testProvider("aws") + p.DiffFn = testDiffFn + s := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: rootModulePath, + Resources: map[string]*ResourceState{ + "aws_instance.foo": &ResourceState{ + Primary: &InstanceState{ + ID: "bar", + Attributes: map[string]string{ + "ami": "ami-abcd1234", + "instance_type": "t2.micro", + }, + }, + }, + }, + }, + }, + } + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + Variables: map[string]interface{}{ + "foo": "ami-1234abcd", + "bar": "t2.small", + }, + State: s, + }) + + plan, err := ctx.Plan() + if err != nil { + t.Fatalf("err: %s", err) + } + + if len(plan.Diff.RootModule().Resources) > 0 { + t.Fatalf("bad: %#v", plan.Diff.RootModule().Resources) + } + + actual := strings.TrimSpace(plan.String()) + expected := strings.TrimSpace(testTerraformPlanIgnoreChangesWildcardStr) + if actual != expected { + t.Fatalf("bad:\n%s\n\nexpected\n\n%s", actual, expected) + } +} + func TestContext2Plan_moduleMapLiteral(t *testing.T) { m := testModule(t, "plan-module-map-literal") p := testProvider("aws") diff --git a/terraform/eval_diff.go b/terraform/eval_diff.go index 8fe476688..e9eea189a 100644 --- a/terraform/eval_diff.go +++ b/terraform/eval_diff.go @@ -188,7 +188,7 @@ func (n *EvalDiff) processIgnoreChanges(diff *InstanceDiff) error { ignorableAttrKeys := make(map[string]bool) for _, ignoredKey := range ignoreChanges { for k := range diff.CopyAttributes() { - if strings.HasPrefix(k, ignoredKey) { + if ignoredKey == "*" || strings.HasPrefix(k, ignoredKey) { ignorableAttrKeys[k] = true } } diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index f8244ce00..880de448a 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -1407,6 +1407,19 @@ aws_instance.foo: ami = ami-abcd1234 ` +const testTerraformPlanIgnoreChangesWildcardStr = ` +DIFF: + + + +STATE: + +aws_instance.foo: + ID = bar + ami = ami-abcd1234 + instance_type = t2.micro +` + const testTerraformPlanComputedValueInMap = ` DIFF: diff --git a/terraform/test-fixtures/apply-ignore-changes-wildcard/main.tf b/terraform/test-fixtures/apply-ignore-changes-wildcard/main.tf new file mode 100644 index 000000000..a2bc76fde --- /dev/null +++ b/terraform/test-fixtures/apply-ignore-changes-wildcard/main.tf @@ -0,0 +1,7 @@ +resource "aws_instance" "foo" { + required_field = "set" + + lifecycle { + ignore_changes = ["*"] + } +} diff --git a/terraform/test-fixtures/plan-ignore-changes-wildcard/main.tf b/terraform/test-fixtures/plan-ignore-changes-wildcard/main.tf new file mode 100644 index 000000000..9d85a17a9 --- /dev/null +++ b/terraform/test-fixtures/plan-ignore-changes-wildcard/main.tf @@ -0,0 +1,12 @@ +variable "foo" {} + +variable "bar" {} + +resource "aws_instance" "foo" { + ami = "${var.foo}" + instance_type = "${var.bar}" + + lifecycle { + ignore_changes = ["*"] + } +} diff --git a/website/source/docs/configuration/resources.html.md b/website/source/docs/configuration/resources.html.md index 8606ef640..e63dce738 100644 --- a/website/source/docs/configuration/resources.html.md +++ b/website/source/docs/configuration/resources.html.md @@ -83,6 +83,10 @@ include `create_before_destroy`. Referencing a resource that does not include ~> **NOTE on ignore\_changes:** Ignored attribute names can be matched by their name, not state ID. For example, if an `aws_route_table` has two routes defined and the `ignore_changes` list contains "route", both routes will be ignored. +Additionally you can also use a single entry with a wildcard (e.g. `"*"`) +which will match all attribute names. Using a partial string together with a +wildcard (e.g. `"rout*"`) is **not** supported. + -------------