Ranika Nisal
Ranika Nisal

Reputation: 980

Adding exceptions to ignore_changes

I currently have a module which contains a block with many configurable properties

resource "example_resource" "instance1" {
    
     block1 {
          property1 = var.variable1      # Should generate a diff if changed
          property2 = var.variable2      # Ignore
          property3 = var.variable3      # Ignore
          ....
          ....
          propertyN = var.variableN      # Ignore
     }
    
     lifecycle {
        ignore_changes = [
            block1[0].property2, block1[0].property3, ... ,block1[0].propertyN
        ]
     }
}

Once the resource has been generated many of the properties within block1 are likely to change due to interaction with the user. I want to ignore such changes when running a terraform plan apart from a few exceptions which should generate a difference if changed in the future. (For example in the above resource if property1 is changed it should generate a diff but not for the others)

Ignoring such changes can be done using the ignore_changes within lifecycle block. But it seems like to do the above. Adding the entire block1 argument to this will cause all changes within to be ignored, or we have to add all ignored properties within the block one by one to the ignore_changes block as I have mentioned in the example.

Manually doing as such makes things a bit harder to maintain, as you will have to keep adding/removing new properties as a new property is added/removed to the block. So is it possible to configure the ignore_changes block to ignore all changes and specifically add the required exceptions?

P.S.

I do not believe this question is specific to a certain resource, But the resource I am trying to implement this concept to is the Azure App Service Resource, specifically to the site_config block within it.

Upvotes: 4

Views: 4075

Answers (1)

pkatsourakis
pkatsourakis

Reputation: 1082

The simplest approach I can think of is to just list out all the individual properties possible in that block, except the ones you don't want to ignore changes on. This would still be tedious and ugly.

Here's a more clever (untested) approach to try

#get the existing resource
data "example_resource" "instance1" {
}

resource "example_resource" "instance1" {

  block1 {
      ...
  }

  lifecycle {
    #transform the list of properties so the values all start with block[0].
    ignore_changes = [for prop in local.ignore_change_props : "block[0].${prop}"] 
  }
}

locals {
  #these properties we want to exclude from ignore_changes
  change_exceptions = ["property1", "property10"],

  #get all the properties from the data block in a map, then remove properties to be excluded
  ignore_change_props = setsubtract(keys(data.example_resource.instance1.block1), local.change_exceptions)
}

Upvotes: 3

Related Questions