What is ignore_changes in Terraform: Explanation and Example
ignore_changes is a lifecycle argument in Terraform that tells Terraform to ignore changes to specified resource attributes during updates. This means Terraform will not try to modify those attributes even if they differ from the configuration, helping avoid unwanted updates.How It Works
Imagine you have a resource in Terraform, like a server or a database, and some of its settings can change outside Terraform, like through a manual update or another tool. Normally, Terraform tries to fix any differences it finds between your configuration and the actual resource, which can cause unwanted changes.
The ignore_changes setting acts like a filter. It tells Terraform to "ignore" certain parts of the resource when checking for changes. So, if those parts change outside Terraform, Terraform will not try to update or revert them during the next apply.
This is useful when some resource attributes are managed outside Terraform or change frequently and you want to avoid constant updates or conflicts.
Example
This example shows a simple AWS EC2 instance where Terraform ignores changes to the tags attribute. Even if tags are changed manually, Terraform will not try to update them.
resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" tags = { Name = "MyInstance" } lifecycle { ignore_changes = ["tags"] } }
When to Use
Use ignore_changes when some resource attributes are managed outside Terraform or change often without your control. For example:
- Tags or metadata updated manually or by other tools.
- Attributes that are automatically updated by the cloud provider.
- Settings that you want to keep stable in Terraform but allow temporary changes.
This helps prevent Terraform from constantly trying to fix changes it did not make, reducing unnecessary updates and potential conflicts.
Key Points
ignore_changesis part of thelifecycleblock in Terraform resources.- It accepts a list of resource attribute names to ignore during updates.
- It helps avoid unwanted updates for attributes changed outside Terraform.
- Use it carefully to avoid ignoring important changes you want Terraform to manage.
Key Takeaways
ignore_changes tells Terraform to skip updates on specified resource attributes.lifecycle block of a resource.