Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore change of an attribute in block

I'm deploying web apps in Azure and I'd like to ignore changes to scm_type attribute within site_config block.

During deployment the scm_type attribute set to None and later we are changing it to something different in Azure Portal.

My current TF code looks like this:

resource "azurerm_app_service" "web_app" {
  count               = length(var.app_names)
  name                = var.app_names[count.index]
  location            = data.azurerm_resource_group.app_resource_group.location
  resource_group_name = data.azurerm_resource_group.app_resource_group.name
  app_service_plan_id = azurerm_app_service_plan.app_plan.id
  tags                = var.tags
  app_settings        = var.app_settings[count.index]

  site_config {
    always_on                 = true
    websockets_enabled        = var.websockets_enabled[count.index]
    use_32_bit_worker_process = var.use_32_bit_worker_process
    scm_type                  = "None"
  }

  lifecycle {
    ignore_changes = [
      site_config.0.scm_type
    ]
  }
}

I expect terraform plan to ignore changes in scm_type during infrastructure updates, but it's trying to revert it back to None. Line from terraform plan output:

~ scm_type = "BitbucketGit" -> "None"

like image 483
szymon Avatar asked Jul 11 '19 14:07

szymon


People also ask

How do you ignore changes in Terraform?

When you want Terraform to ignore changes between subsequent apply commands you can use the lifecycle ignore_changes meta-argument. The ignore_changes argument means that Terraform will set the value when the resource is first deployed and then forever ignore any changes to it.

How do you stop Terraform destroy?

To prevent destroy operations for specific resources, you can add the prevent_destroy attribute to your resource definition. This lifecycle option prevents Terraform from accidentally removing critical resources. Add prevent_destroy to your EC2 instance. Run terraform destroy to observe the behavior.

Does Terraform destroy before creation?

No need for terraform destroy, as it will just destroy all the resources created. You just need to provide the backend configuration in your tf file.

What is life cycle in Terraform?

lifecycle is a nested block that can appear within a resource block. The lifecycle block and its contents are meta-arguments, available for all resource blocks regardless of type.


3 Answers

I think you need to fix your syntax in the ignore changes. It should look like this, or at least from what I have been able to get to work.

lifecycle {
    ignore_changes = [
        site_config["scm_type"],
    ]
}

Here are the docs that have the syntax.

https://www.terraform.io/docs/language/meta-arguments/lifecycle.html#ignore_changes

like image 59
Jamie Avatar answered Oct 23 '22 22:10

Jamie


It was a terraform bug: https://github.com/hashicorp/terraform/issues/21433 My syntax is correct, in version 0.12.4 it's working again.

like image 15
szymon Avatar answered Oct 23 '22 22:10

szymon


lifecycle {
    ignore_changes = [
        site_config["scm_type"]
    ]
}

here site_config["scm_type"]

without comma(,) also it will work

Here are the docs that have the syntax.

https://spacelift.io/blog/terraform-resource-lifecycle

like image 1
Venkata Subbaiah Avatar answered Oct 23 '22 23:10

Venkata Subbaiah