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
Create a Directory for Your Terraform Files
1 |
cd ~/Labs && mkdir lifecycle-lab && cd lifecycle-lab |
Create a main.tf File
1 |
vi main.tf |
Define the Azure Provider in main.tf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# Azure Provider for East US provider "azurerm" { features {} resource_provider_registrations = "none" subscription_id = "b70f2b66-b08e-4775-8273-89d81847a0c2" # Replace with your subscription id } resource "azurerm_resource_group" "lifecycle-group" { name = "lifecycle-group" location = "East US" } resource "azurerm_storage_account" "lifecyclegroupstorage" { name = "lifecyclegroupstorage" resource_group_name = azurerm_resource_group.lifecycle-group.name location = azurerm_resource_group.lifecycle-group.location account_tier = "Standard" account_replication_type = "LRS" # Uncomment one at a time to see the effect of each lifecycle argument lifecycle { # create_before_destroy = true # prevent_destroy = true # ignore_changes = [name] replace_triggered_by = [azurerm_storage_account.triggeringresource.name] } } resource "azurerm_storage_account" "triggeringresource" { name = "triggeringresource" resource_group_name = azurerm_resource_group.lifecycle-group.name location = azurerm_resource_group.lifecycle-group.location account_tier = "Standard" account_replication_type = "LRS" } |
Initialize Terraform
1 |
terraform init |
Plan Terraform Deployment
1 |
terraform plan |
Apply Terraform Configuration
1 |
terraform apply -auto-approve |
Task 1: Create Before Destroy
- Edit the main.tf file and change the name of the Storage Account to lifecyclegroupstorage1.
- Apply the changes:
1 |
terraform apply -auto-approve |
- Notice that first, the destroy is triggered, and then the creation.
- Uncomment the lifecycle rule
1 |
create_before_destroy and change the name back to lifecyclegroupstorage. |
- Apply again:
1 |
terraform apply -auto-approve |
- 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:
1 |
terraform destroy -auto-approve |
- 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:
1 |
terraform apply -auto-approve |
- 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:
1 |
terraform apply -auto-approve |
- Notice that although we have made no changes to lifecyclegroupstorage, it is also being destroyed and recreated.
Cleanup
1 2 3 |
terraform destroy -auto-approve cd ~/Labs && rm -rf lifecycle-lab |
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
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 Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner, Amazon CloudFront, Amazon OpenSearch, AWS DMS and many more.
To get started, go through our Consultancy page and Managed Services Package, CloudThat’s offerings.

WRITTEN BY Mehar Nafis
Comments