variablesterraformhcl

Overwrite attribute in Terraform variable object


I need to pass a variable that contains an object with multiple variables. Is there any way to override one of these attributes where parsing it?

For example:

global = {
  region     = "eu-central-1"
  account_id = "9555555"
  app_port   = 8080
  domain     = "my.domain"
  stage      = "production"
  docker_tag = "production"
}

I want to pass this global var but with docker_tag set to "latest". Is there any way to do it without the need to put all attributes and replace the one I need? (I have like 25 attributes)

What I don't want:

global = {
  region     = var.region
  account_id = var.account_id
  app_port   = var.app_port
  domain     = var.domain
  stage      = var.stage
  docker_tag = "latest"
}

Solution

  • I found myself the way to do it with merge (https://www.terraform.io/docs/language/functions/merge.html): "If more than one given map or object defines the same key or attribute, then the one that is later in the argument sequence takes precedence."

    merge ( var.global, { docker_tag = "latest"})

    It just replaced the key I want to be replaced