DevOps

2 Mins Read

Terraform Lifecycle Meta-Argument

Voiced by Amazon Polly

Terraform’s lifecycle meta-argument helps control how resources are created, updated, and destroyed. It includes:

  • create_before_destroy – Ensures a new resource is created before deleting the old one, preventing downtime.
  • prevent_destroy – Blocks accidental deletions of critical resources.
  • ignore_changes – Ignores changes to specific attributes that might be modified outside Terraform.
  • triggered_by – Ensures that a resource is recreated (destroyed and recreated) when specific referenced resources or variables change.

Enhance Your Productivity with Microsoft Copilot

  • Effortless Integration
  • AI-Powered Assistance
Get Started Now

Create a Directory for Your Terraform Files

Create a main.tf File

Define the Azure Provider in main.tf

Initialize Terraform

Plan Terraform Deployment

Apply Terraform Configuration

Task 1: Create Before Destroy

  • Edit the main.tf file and change the name of the Storage Account to lifecyclegroupstorage1.
  • Apply the changes:
  • Notice that first, the destroy is triggered, and then the creation.
  • Uncomment the lifecycle rule
  • Apply again:
  • Notice that first, the create is triggered, and then the destroy.

Task 2: Prevent Destroy

  • Comment out create_before_destroy and uncomment prevent_destroy.
  • Attempt to destroy the resources:
  • Terraform will not destroy this resource, even when you run terraform destroy. Any attempt to destroy it will result in an error.

Task 3: Ignore Changes

  • Edit the main.tf file:
    • Change the name of the Storage Account to lifecyclegroupstorage2.
    • Uncomment ignore_changes.
    • Comment prevent_destroy.
  • Apply the changes:
  • Notice that no change will be done.

Task 4: Replace Triggered By

  • Edit the main.tf file:
    • Change the name of the Storage Account from triggeringresource to triggeringresource1.
    • Comment ignore_changes.
    • Uncomment replace_triggered_by.
  • Apply the changes:
  • Notice that although we have made no changes to lifecyclegroupstorage, it is also being destroyed and recreated.

Cleanup

Start your career on Azure without leaving your job! Get Certified in less than a Month

  • Experienced Authorized Instructor led Training
  • Live Hands-on Labs
Subscribe now

About CloudThat

CloudThat is an award-winning company and the first in India to offer cloud training and consulting services worldwide. As a Microsoft Solutions Partner, AWS Advanced Tier Training Partner, and Google Cloud Platform Partner, CloudThat has empowered over 850,000 professionals through 600+ cloud certifications winning global recognition for its training excellence including 20 MCT Trainers in Microsoft’s Global Top 100 and an impressive 12 awards in the last 8 years. CloudThat specializes in Cloud Migration, Data Platforms, DevOps, IoT, and cutting-edge technologies like Gen AI & AI/ML. It has delivered over 500 consulting projects for 250+ organizations in 30+ countries as it continues to empower professionals and enterprises to thrive in the digital-first world.

WRITTEN BY Mehar Nafis

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!