40

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"

szymon
  • 830
  • 1
  • 7
  • 11
  • 1
    Worth noting there are a couple bugs around this for 0.12: https://github.com/hashicorp/terraform/issues/21433 and https://github.com/hashicorp/terraform/issues/21421 (supposed to be fixed in https://github.com/hashicorp/terraform/pull/21788). – Andy Shinn Jul 11 '19 at 18:04

3 Answers3

43

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

dain
  • 6,475
  • 1
  • 38
  • 47
Jamie
  • 3,094
  • 1
  • 18
  • 28
  • 1
    FYI: This format is currently making terraform ignore the whole site_config block for me when using v.1.1.5 – M_dk Apr 04 '22 at 08:17
15

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.

szymon
  • 830
  • 1
  • 7
  • 11
  • 5
    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_key` – mltsy Aug 10 '20 at 19:39
  • [This is the correct syntax](https://stackoverflow.com/a/57040569/542251), not the above – Liam Sep 09 '20 at 07:17
  • 2
    The syntax in the above linked comment did not work for me, but the one in the comment from @mltsy did. Here's a [GitHub issue](https://github.com/hashicorp/terraform/issues/24719#issuecomment-628264278) that also shows this syntax being used. – Chris McKeown Jun 17 '21 at 19:06
  • 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. – M_dk Apr 04 '22 at 08:20
  • @M_dk 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? – Roger Chen Aug 04 '22 at 21:49
  • @RogerChen Yes. We found that site_config[0].scm_type did the trick. So a block like this – M_dk Aug 05 '22 at 20:02
  • lifecycle { ignore_changes = [ site_config[0].scm_type, ] } – M_dk Aug 05 '22 at 20:13
  • Can anyone comment on this issue? https://stackoverflow.com/questions/76286528/how-to-prevent-azure-apim-primary-key-not-change/76288125#76288125 – Sudhir May 22 '23 at 08:32
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

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103