The lifecycle Meta-Argument
lifecycle
is a block that has arguments which allow you to control how Terraform manages resources throughout their lifecycle.
create_before_destroy
main.tf
resource "akamai_edgeworker" "my_edgeworker" {
# ... other configuration ...
lifecycle {
create_before_destroy = true
}
}
- When set to
true
, Terraform creates the new resource before destroying the old one - Useful for zero-downtime deployments
prevent_destroy
main.tf
resource "akamai_datastream" "my_datastream" {
# ... other configuration ...
lifecycle {
prevent_destroy = true
}
}
- Prevents the resource from being destroyed
- Will cause an error if you try to destroy the resource
replace_triggered_by
main.tf
resource "akamai_cloudaccess_key" "my_access_key" {
# ... other configuration ...
lifecycle {
replace_triggered_by = [
akamai_property.my_property.latest_version
]
}
}
- Forces replacement of the resource when specified attributes of other resources change
- Useful for dependencies that require recreation of resources
ignore_changes
The most common use case for ignore_changes
on Akamai is for arguments that may change frequently.
For example, in a GitOps operation the note
argument in the akamai_property_activation
might contain things like commit IDs, timestamps, or other values that change frequently.
main.tf
resource "akamai_property_activation" "my_activation" {
property_id = "prp_12345"
network = "staging"
contact = ["jsmith@example.com"]
note = var.activation_notes
version = "1"
auto_acknowledge_rule_warnings = true
lifecycle {
ignore_changes = [
note
]
}
}
- Without ignore_changes, every time the note changes, Terraform would try to create a new activation
- By ignoring changes to note, Terraform won't trigger a new activation just because the note changed
- This prevents unnecessary activations while still maintaining the desired state of other important configuration
Exercise
Configure
Add lifecyle ignore_changes
to both of the 2 akamai_property_activation
resources in your property.tf to prevent changes to the notes to trigger activations.
Plan/Apply
Run terraform
Update
Make a change to the note
argument.
Plan/Apply
Run Terraform again. What happens?
Commit
Commit your change and push it to GitHub.