Day 9 - Terraform Lifecycle Meta Arguments in AWS

Introduction

In the previous days, I focused on creating resources using Terraform.
Today was different.

Day 09 helped me understand how Terraform manages changes over time.
Instead of only creating infrastructure, I learned how to control what happens when something is updated, replaced, or removed.

This is important because real environments are always changing.


What are lifecycle meta arguments

Lifecycle meta arguments allow us to control how Terraform behaves when it creates, updates, or destroys resources.

They help us:

  • Avoid downtime
  • Protect important resources
  • Handle changes made outside Terraform
  • Validate configurations before and after deployment

create before destroy

By default, Terraform destroys a resource first and then creates a new one.

With create before destroy, Terraform creates the new resource first and then removes the old one.

Example

lifecycle {
create_before_destroy = true
}

This is useful when downtime is not acceptable, such as application servers or production systems.

terraform apply 

Made changes to bucket name and ran terraform apply. Terraform creates the new resource first and only then removes the old one, ensuring no gap in availability.



prevent destroy

This setting prevents Terraform from deleting a resource.

Example

lifecycle {
prevent_destroy = true
}

If Terraform tries to destroy this resource, it will fail with an error.

This is useful for:

  • Production databases
  • State storage buckets
  • Important data resources

In this screenshot, Terraform blocks the destroy operation because prevent_destroy is enabled. This protects critical resources from accidental deletion.



ignore changes

Sometimes resources are modified by other systems such as auto scaling or monitoring tools.

In such cases, Terraform can ignore specific changes.

Example

lifecycle {
ignore_changes = [tags["Owner"]]
}

This prevents Terraform from trying to revert those changes.

In this screenshot, I manually changed a tag in AWS, but Terraform did not detect any changes when i ran terraform plan because ignore_changes is configured for that attribute.





replace triggered by

This forces Terraform to replace a resource when another resource changes.

Example

lifecycle {
replace_triggered_by = [random_pet.deployment_version]
}

Even if the resource itself is not modified, Terraform will recreate it when the dependent resource changes.

This is useful for maintaining consistency.

In this screenshot, the S3 bucket is replaced even though I did not modify it directly. This happens because replace_triggered_by forces recreation when the dependent resource changes.




Changes in AWS Console



precondition

Precondition is used to validate conditions before Terraform creates or updates a resource.

Example

precondition {
condition = contains(var.allowed_regions, data.aws_region.current.name)
error_message = "Invalid region"
}

If the condition is not met, Terraform stops before creating the resource.

In this screenshot, Terraform stops the deployment before creating the resource because the region is not in the allowed list. This helps prevent invalid configurations.

Made below changes to terraform.tfvars file


Ran terraform apply



postcondition

Postcondition validates a resource after it is created.

Example

postcondition {
condition = contains(keys(self.tags), "Environment")
error_message = "Missing Environment tag"
}

Even if the resource is created successfully, Terraform will fail if the condition is not satisfied.

Made below changes to main.tf file


Ran terraform apply. In this screenshot, Terraform successfully creates the resource but then fails because the required Environment tag is missing. This shows how postcondition validates the resource after creation.


Key learnings

  • Terraform lifecycle controls how resources change over time
  • create before destroy helps avoid downtime
  • prevent destroy protects critical resources
  • ignore changes allows external systems to manage attributes
  • replace triggered by helps maintain consistency
  • precondition and postcondition help validate configurations

Conclusion

Today I learned that Terraform is not just about creating infrastructure.
It is about managing change safely.

Lifecycle meta arguments give more control and help build reliable and production ready environments.


Video reference


Jay

Comments

Popular posts from this blog

ASM Integrity check failed with PRCT-1225 and PRCT-1011 errors while creating database using DBCA on Exadata 3 node RAC

Lock Tables in MariaDB

Life is beautiful