What is the lifecycle block in Terraform?
Answer
The lifecycle meta-argument block controls how Terraform handles resource creation, updates, and deletion. Key settings: create_before_destroy: when a resource must be replaced, create the new one before destroying the old — prevents downtime for immutable resources like SSL certificates. lifecycle { create_before_destroy = true }. prevent_destroy: raises an error if anything attempts to destroy this resource. Use for databases and S3 buckets that should never be accidentally deleted. lifecycle { prevent_destroy = true }. ignore_changes: ignore specific attribute changes (e.g., tags managed externally). lifecycle { ignore_changes = [tags] }. replace_triggered_by: force replacement when referenced resources change. Understanding lifecycle is critical for managing stateful resources like databases safely.