Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default map values

Does the default value of the variable map merge with data I provide to the terraform?

Sample variables.tf:

variable "foo" {
  type = map
  default = {
    lorem = "ipsum"
    dolor = "sit"
  }
}

And foo.tfvars provided:

foo = {
  dolor = "changed"
  amet = "consectetur"
}

Will ${foo.lorem} still exist?

Will ${foo.dolor} be "changed"?

Will ${foo.amet} be available?

like image 285
n0zz Avatar asked Mar 31 '26 23:03

n0zz


1 Answers

No, there is no merging behavior. If you set an explicit value for a variable then the default is not used at all.

If you need to merge with other values then you can use the merge function to write that explicitly:

variable "foo" {
  type    = map(string)
  default = {}
}

locals {
  foo = merge(
    tomap({
      lorem = "ipsum"
      dolor = "sit"
    }),
    var.foo,
  )
}

With the above configuration, elsewhere in the module you can refer either to var.foo to get the exact value the caller provided or to local.foo to get the result of merging the caller's map with your map of default values.

like image 100
Martin Atkins Avatar answered Apr 02 '26 15:04

Martin Atkins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!