How to Ignore Change of an Attribute in Terraform Blocks
TLDR
To ignore changes to a specific attribute in Terraform, use the lifecycle block with the ignore_changes argument. This is useful for attributes managed outside of Terraform or for temporary overrides.
Terraform's lifecycle block allows you to control how resources are managed. The ignore_changes argument is particularly useful for ignoring changes to specific attributes, such as those managed by external systems or temporary overrides.
Why Ignore Changes?
- External Management: Some attributes are managed outside of Terraform, such as by a cloud provider or another tool.
- Temporary Overrides: Ignore changes to attributes that are temporarily modified.
- Avoid Unnecessary Updates: Prevent Terraform from overwriting changes made outside of its control.
Using ignore_changes
The ignore_changes argument is part of the lifecycle block. You can specify one or more attributes to ignore.
Example: Ignoring a Single Attribute
resource "azurerm_virtual_machine" "example" {
name = "example-vm"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
network_interface_ids = [azurerm_network_interface.example.id]
vm_size = "Standard_DS1_v2"
lifecycle {
ignore_changes = ["vm_size"]
}
}
In this example, changes to the vm_size attribute are ignored.
Example: Ignoring Multiple Attributes
Here's how to ignore multiple attributes:
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "example-instance"
}
lifecycle {
ignore_changes = ["tags[\"Name\"]", "instance_type"]
}
}
In this example, changes to the Name tag and instance_type are ignored.
Best Practices
- Use Sparingly: Only ignore changes when absolutely necessary to avoid configuration drift.
- Document Reasons: Clearly document why changes are being ignored.
- Monitor Resources: Regularly review ignored attributes to ensure they are still relevant.
By using the ignore_changes argument, you can manage resources more flexibly in Terraform, avoiding unnecessary updates and conflicts.
We earn commissions when you shop through the links below.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
Acronis
The most secure backup
Acronis: the most secure backup solution for your data
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorFound an issue?