Manage the lifecycle of resources in Azure with Terraform

Manage the lifecycle of resources in Azure with Terraform

In terraform we have something called a a lifecycle block. It is very useful for many reasons. In this post we will focus primarily on two which are prevent_destroy and ignore_changes

I have a small configuration where I will create an SQL server and a database in Azure. You can find the code from my repository HERE

The key code lies in my database.tf file with the following snippet at the end

lifecycle {
    prevent_destroy = true
    ignore_changes = [
      geo_backup_enabled,
      tags
    ]
  }

I have set prevent_destroy to true which means I cannot delete this resource with Terraform unless I intentionally remove this code configuration.

Why would I want this?

This could seem like an annoying extra step, Terraform already asks me to confirm once if I run terraform destroy (Unless I append the -auto-approve switch). The reason is simple. It could be valid that the resources should be spun down and removed but databases usually contain data and information, are you sure you want to delete this? I see it as its purpose is to prevent accidental deletion of data and at least make you think an extra time:

"Do I care about the data in the DB or is it okay for me to just remove it?"

Finally, as you can see I also have an ignore_changes block in my configuration as well. This is actually because of a problem I have run into which I have not solved yet and that is I wanted to test the serverless compute option for SQL database but I run into an issue when trying to apply any changes via Terraform. It is the same issue they discuss in this thread but I havent found a fix and their workaround did not work for me.

This is placing me in a position where I cannot keep working with my codebase so by ignore some changes to properties for the database Terraform thinks the resource has not changed at all, and I can apply and change properties of other resources in my configuration.

Maybe you have a solution to my issue discussed in that Github Issue thread?

References

Manage resource lifecycle | Terraform | HashiCorp Developer
Create an AWS instance and security group. Update your configuration with lifecycle management blocks to prevent resource deletion, create resources before destroying them, and ignore changes to specific resource attributes.
GitHub - carlzxc71/terraform-prevent-destroy: Demonstrating lifecycle blocks using Terraform and Azure
Demonstrating lifecycle blocks using Terraform and Azure - carlzxc71/terraform-prevent-destroy

About me

About me
If you have landed on my page you will have already understood my passion for tech, but obviously there is more to life than that. Here I will try and outline a few of my other hobbies. Strength training I am a person who loves to move around and challenge