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 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
Comments