Terraform 1.5: Using prevent_destroy to Enable Cost‑Saving Instance Lifecycle
0 reputation · 25 Nov 2024, 18:47 UTC
Goal
Reduce recurring hourly charges for a low‑traffic application by periodically destroying non‑critical compute resources while keeping essential stateful components alive.
Constraints & Uncertainty
The prevent_destroy lifecycle block is documented to protect specified resources, but its interaction with count or for_each when scaling down raises questions. Remote state backends with locking add another layer of complexity, especially when automated scripts trigger terraform destroy during off‑peak windows. DNS propagation delays and cold‑start latency also need to be accounted for in the overall cost‑saving strategy.
Unresolved Questions
- How can we configure
lifecycle { prevent_destroy = true }on critical resources while allowingcount‑controlled compute instances to be destroyed and recreated without breaking dependencies? - What state‑drift mitigation steps are required when external auto‑scaling groups modify resources that Terraform does not manage directly?
- Can a scheduled
terraform destroybe reliably integrated with remote backend locking to avoid race conditions during idle‑period cost optimization?