build a dynamic ignore_changes in terraform

Viewed 104

I want to avoid resource creation when hidden-link tag is being added by azure on a azurerm_application_insights_web_test :

resource "azurerm_application_insights_web_test" "webtest" {
  for_each                = local.webtest_urls
  name                    = each.key
  location                = var.location
  resource_group_name     = azurerm_resource_group.main.name
  application_insights_id = azurerm_application_insights.main.id
  kind                    = "ping"
  frequency               = 300 # 5 minutes  
  enabled                 = true
  geo_locations           = var.ping_locations
  configuration           = templatefile("webtest.xml.tmpl", {
  url                     = each.value.url 
  expected_http_code      = 200,
  timeout                 = 60
  })
    lifecycle {
    ignore_changes = [
        tags["hidden-link:/subscriptions/${var.subscription_id}/resourceGroups/${azurerm_resource_group.main.name}/providers/Microsoft.Insights/components/${azurerm_application_insights.main.name}"]
    ]
  }
}

Unfortunatly this does not work :

Error: Invalid expression on appinsights.tf line 52, in resource "azurerm_application_insights_web_test" "webtest": 52: tags["hidden-link:/subscriptions/${var.subscription_id}/resourceGroups/${azurerm_resource_group.main.name}/providers/Microsoft.Insights/components/${azurerm_application_insights.main.name}"] A single static variable reference is required: only attribute access and indexing with constant keys. No calculations, function calls, template expressions, etc are allowed here.

When I hardcode the value to ignore this works well.

How am I supposed to deal with this issue?

1 Answers

You cannot use that, per Terraform documentation [1]:

The lifecycle settings all affect how Terraform constructs and traverses the dependency graph. As a result, only literal values can be used because the processing happens too early for arbitrary expression evaluation.


[1] https://www.terraform.io/language/meta-arguments/lifecycle

Related