0
0
Terraformcloud~3 mins

Why Prevent_destroy lifecycle rule in Terraform? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if one wrong click could erase your entire cloud setup forever?

The Scenario

Imagine you manage a cloud server that runs your website. One day, you accidentally delete it while cleaning up resources because you forgot which one was critical.

The Problem

Manually tracking which resources are important is hard. Mistakes happen easily, causing downtime and lost data. Fixing these mistakes takes time and can be costly.

The Solution

The prevent_destroy rule in Terraform stops important resources from being deleted by mistake. It acts like a safety lock, so you must consciously remove the lock before deleting.

Before vs After
Before
resource "aws_instance" "web" {
  # no protection, can be deleted anytime
}
After
resource "aws_instance" "web" {
  lifecycle {
    prevent_destroy = true
  }
}
What It Enables

This rule lets you protect critical cloud resources from accidental deletion, keeping your services safe and reliable.

Real Life Example

A company uses prevent_destroy on their database server resource to avoid accidental removal during updates, preventing data loss and downtime.

Key Takeaways

Manual deletion risks breaking important cloud resources.

prevent_destroy adds a safety lock to stop accidental deletes.

It helps keep your cloud infrastructure stable and secure.