lifecycle
What is lifecycle?
lifecycle: Tunes how Terraform creates, updates and deletes a resource — create_before_destroy, prevent_destroy, ignore_changes. The lifecycle block in Terraform changes how a resource is created, updated and deleted. create_before_destroy builds the replacement before removing the old resource, which avoids downtime for things like certificates or servers behind a load balancer. prevent_destroy makes Terraform refuse any plan that would delete the resource, a valuable safety net for production databases. ignore_changes tells Terraform to ignore certain attributes that are changed outside it, such as tags set by another tool or a scaling count managed by an autoscaler.
Frequently asked questions about lifecycle
What should you keep in mind with lifecycle?
Use these settings deliberately and add a comment explaining why. ignore_changes in particular can hide real drift, so list specific attributes rather than ignoring everything. A prevent_destroy on your main database costs nothing and prevents one of the worst possible mistakes.
What does an example of lifecycle look like?
lifecycle { create_before_destroy = true prevent_destroy = true ignore_changes = [tags] } At a glance
| Category | Terraform |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |