How to ignore change of an attribute in block
Asked Answered
N

4

50

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"

Nowak answered 11/7, 2019 at 14:53 Comment(1)
Worth noting there are a couple bugs around this for 0.12: github.com/hashicorp/terraform/issues/21433 and github.com/hashicorp/terraform/issues/21421 (supposed to be fixed in github.com/hashicorp/terraform/pull/21788).Decorum
E
50

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

Electroscope answered 15/7, 2019 at 13:15 Comment(1)
FYI: This format is currently making terraform ignore the whole site_config block for me when using v.1.1.5Hard
N
17

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.

Nowak answered 12/7, 2019 at 7:36 Comment(8)
I was surprised to find that your syntax was correct, as it doesn't appear to be documented anywhere! But FWIW to future answer-seekers - apparently blocks within a resource definition are referenced as lists (presumably to support the case where the same block appears more than once), and therefore do require this kind of numerically-indexed syntax in order to reference a nested key: block_name[0].nested_keyEse
This is the correct syntax, not the aboveTetherball
The syntax in the above linked comment did not work for me, but the one in the comment from @Ese did. Here's a GitHub issue that also shows this syntax being used.Maestas
The suggested syntax of site_config["scm_type"] causes terraform to ignore the whole site_config block for me with version 1.1.5 It does indeed ignore the settings, but you also end up ignoring settings like ip_restrictions so use with caution.Hard
@Hard we found the same, you try ignore a particular setting in site_config, but ignores the whole site_config, have you found out a way to work around it?Ohaus
@RogerChen Yes. We found that site_config[0].scm_type did the trick. So a block like thisHard
lifecycle { ignore_changes = [ site_config[0].scm_type, ] }Hard
Can anyone comment on this issue? #76287028Broderic
L
2
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

Loom answered 14/9, 2022 at 11:7 Comment(1)
This would cause ignoring the whole site_config. Check my answer here https://mcmap.net/q/348693/-how-to-ignore-change-of-an-attribute-in-blockStopwatch
S
2

It seems that syntax changed. M_dk confirmed that site_config["scm_type"] doesn't work in terraform 1.1.5. And it is the same in version 1.3.9. What is even worse it doesn't throw error.

It ignores whole site_config, what effectively blocks changing the site.

For version 1.1.5 and above proper syntax is:

lifecycle {
    ignore_changes = [
        site_config[0].scm_type
    ]
}
Stopwatch answered 27/10, 2023 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.