Back to course

Lifecycle Meta-Arguments

Infrastructure as Code (Terraform & OpenTofu Mastery)

Controlling Resource Behavior

What if you want to make sure a database is never accidentally deleted?

hcl resource "aws_db_instance" "database" {

...

lifecycle { prevent_destroy = true } }

If you run tofu destroy, it will fail.

Other useful lifecycle rules:

  • create_before_destroy: Create the new resource before deleting the old one (zero downtime).
  • ignore_changes: Ignore manual changes made in the AWS console.