← Back to list

Terraform for GCP Lifecycle Rules

Hi, this is Paul, and welcome to the #10 part of my Terraform guide. Today we will discuss how to use the Terraform Lifecycle Rules.

Paul Ravvich in Terraform for the Google Cloud Platform · 2024-04-24 15:40 · 4 claps · 2.0 min read
#terraform #terraform-lifecycle #devops #infrastructure-as-code #infrastructure-automation
Open on Medium ↗
Wiki topics: ☁️ · DevOps & Cloud

Terraform for GCP Lifecycle Rules

Terraform for GCP Lifecycle Rules

Terraform for GCP Lifecycle Rules

Hi, this is Paul, and welcome to the #10 part of my Terraform guide. Today we will discuss how to use the Terraform Lifecycle Rules.

Terraform Lifecycle Rules

You can define various lifecycle rules as meta-arguments within an individual resource. Think of a lifecycle block as one of the resource’s attributes. As previously mentioned, this is a meta-argument.

Create Before Destroy

This rule dictates that when we attempt to create a new resource based on an existing one, the default action is to destroy the existing resource before creating the new one. However, if we want to ensure that the new resource is created before the old one is destroyed, we must enforce this order.

resource "random_integer" "name" {
    min = 20
    max = 100
    lifecycle {
        create_before_destroy = true
    }
}

init, plan and apply commands and change max param from 100 to 200

resource "random_integer" "name" {
    min = 20
    max = 200 # prev value 100 and we change it to 200
    lifecycle {
        create_before_destroy = true
    }
}

And run plan and apply one more time we have the log:

random_integer.name: Creating...
random_integer.name: Creation complete after 0s [id=169]
random_integer.name (deposed object es753497): Destroying...
random_integer.name: Destruction complete after 0s

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.

As you can see creating happens before destroying.

Prevent Destroy

This rule allows you to prevent the destruction of a particular resource, even if a command like terraform destroy is executed. By applying this rule, you can ensure the resource remains intact.

resource "random_integer" "name" {
    min = 20
    max = 100
    lifecycle {
        prevent_destroy = true
    }
}

And try to make terraform destoywe’ve got an error:

Error: Instance cannot be destroyed

on main.tf line 1:
1: resource random_integer name {

Resource random_integer.name has lifecycle.prevent_destroy set, 
but the plan calls for this resource to be destroyed. 
To avoid this error and continue with the plan, 
either disable lifecycle.prevent_destroy or reduce the scope 
of the plan using the -target flag.

Ignore Changes

This rule is useful when you want to make certain changes to a resource, such as updating a tag or other attribute, without those changes taking effect. For example, you might have a database configuration that you do not want anyone in your organization to alter. Even if changes are made, they will not impact the existing configuration.

resource "random_integer" "name" {
    min = 50 # 20 -> 50
    max = 200 # 100 -> 200
    lifecycle {
        ignore_changes = [min]
    }
}

Now terraform plan the command doesn't detect any changes in min param because we are exclude min from change detection.

Thank you for reading until the end. Before you go:

Paul Ravvich


메타데이터
post_id
5968898b1333
slug
terraform-for-gcp-lifecycle-rules-5968898b1333
url
https://medium.com/terraform-using-google-cloud-platform/terraform-for-gcp-lifecycle-rules-5968898b1333
canonical_url
https://medium.com/terraform-using-google-cloud-platform/terraform-for-gcp-lifecycle-rules-5968898b1333
author_url
https://medium.com/@pravvich
status
ok
fetched_at
2026-06-29 01:02:39