Search code examples
variablesterraformhcl

Overwrite attribute in terraform variable object


I need to pass a variable that contains an object with multiple variables. Is it 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" }

But 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) Example of want i don't want to do:

global = {

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

Thanks to all


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