Search code examples
terraformterraform-provider-awshcl

Terraform Lifecycle Ignore changes


I am trying to apply lifecycle ignore_changes rule against parameter in resource resource "aws_servicecatalog_provisioned_product" as shown below.

resource "aws_servicecatalog_provisioned_product" "example" {
  name                       = "example"
  product_name               = "Example product"
  provisioning_artifact_name = "Example version"

  provisioning_parameters {
    key   = "foo"
    value = "bar"
  }

  provisioning_parameters {
    key   = "key2"
    value = lookup(var.parameter_group, "key2", "test2")
  }

  provisioning_parameters {
    key   = "key3"
    value = "test3"
  }

  tags = {
    foo = "bar"
  }

  lifecycle {
   ignore_changes = [
    tags["foo"],
    aws_servicecatalog_provisioned_product.provisioning_parameters.example["key2"]
   ]
 }
  
}
variable parameter_group {
  description = "Parameters map required for modules. 
  type        = map(any)
  default     = {}
}

when i am running the plan i am getting the error below

│ Error: Unsupported attribute
│ 
│   on modules/example_provision/main.tf line 28, in resource "aws_servicecatalog_provisioned_product" "example":
│  28:        aws_servicecatalog_provisioned_product.provisioning_parameters.example["key2"]
│ 
│ This object has no argument, nested block, or exported attribute named "aws_servicecatalog_provisioned_product".

I would like to ignore the changes made to this parameter value. The Ignore on tags is working fine but as soon as i add my second line which is

aws_servicecatalog_provisioned_product.provisioning_parameters.example["key2"] the error starts to come in.

looking for suggestion/help here :)

Regards


Solution

  • ignore_changes can only ignore changes to the configuration of the same resource where it's declared, and so you only need to name the argument you wish to ignore and not the resource type or resource name:

      lifecycle {
        ignore_changes = [
          tags["foo"],
          provisioning_parameters,
        ]
      }
    

    The provisioning_parameters block type doesn't seem to be represented as a mapping (the provisioning_parameter blocks don't have labels in their headers) so you won't be able to refer to a specific block by its name.

    However, the provider does declare it as being a list of objects and so I think you will be able to ignore a specific item by its index, where the indices are assigned in order of declaration. Therefore in your current example the one with key = "key2" will have index 1, due to being the second block where Terraform counts up from zero:

      lifecycle {
        ignore_changes = [
          tags["foo"],
          provisioning_parameters[1],
        ]
      }