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 a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.

CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training PartnerAWS Migration PartnerAWS Data and Analytics PartnerAWS DevOps Competency PartnerAWS GenAI Competency PartnerAmazon QuickSight Service Delivery PartnerAmazon EKS Service Delivery Partner AWS Microsoft Workload PartnersAmazon EC2 Service Delivery PartnerAmazon ECS Service Delivery PartnerAWS Glue Service Delivery PartnerAmazon Redshift Service Delivery PartnerAWS Control Tower Service Delivery PartnerAWS WAF Service Delivery PartnerAmazon CloudFrontAmazon OpenSearchAWS DMS and many more.

To get started, go through our Consultancy page and Managed Services PackageCloudThat’s offerings.

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!