You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Provisioners let you execute scripts on resources after creation, while lifecycle rules control how Terraform creates, updates, and destroys resources. This lesson covers both concepts, including when (and when not) to use provisioners.
Lifecycle rules modify the default behaviour of resource creation, update, and destruction:
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = var.instance_type
lifecycle {
create_before_destroy = true
}
}
| Argument | Purpose |
|---|---|
create_before_destroy | Create the replacement before destroying the original |
prevent_destroy | Prevent accidental destruction of critical resources |
ignore_changes | Ignore changes to specific attributes |
replace_triggered_by | Force replacement when a referenced value changes |
precondition | Validate assumptions before applying |
postcondition | Validate outcomes after applying |
By default, Terraform destroys the old resource before creating the new one. This argument reverses the order:
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = "t3.micro"
lifecycle {
create_before_destroy = true
}
}
Use case: Zero-downtime deployments — the new instance is ready before the old one is removed.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.