1

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

Guel135
  • 750
  • 7
  • 26

2 Answers2

7

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

Guel135
  • 750
  • 7
  • 26
1

Since Terraform v1.3, you can use Optional Object Type Attributes!

Example:

variable "with_optional_attribute" {
  type = object({
    a = string                # a required attribute
    b = optional(string)      # an optional attribute
    c = optional(number, 127) # an optional attribute with default value
  })
}
matsn0w
  • 29
  • 3