Terraform: Map default merged rather than overridden

Created on 29 Aug 2016  ·  8Comments  ·  Source: hashicorp/terraform

Terraform Version

Terraform v0.7.2

Affected Resource(s)

Variable handling (core)

Terraform Configuration Files

#terraform.tfvars
mymap={ foo = "bar", baz = "biff" }
variable "mymap" {
  type = "map"
  default = { "zonk" = "pow" }
}

data "template_file" "test" {
  template = "${data}"

  vars {
    data = "${join(",", formatlist("key: %s, val: %s. ", keys(var.mymap), values(var.mymap)))}"
  }
}

output "test" {
  value = "${data.template_file.test.rendered}"
}

Output

data.template_file.test: Refreshing state...

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

test = key: baz, val: biff. ,key: foo, val: bar. ,key: zonk, val: pow.

Debug Output

https://gist.github.com/cbarbour/dfabe650fef1918e3f565f712036a677

Expected Behavior

Map defaults should be overridden when another map is explicitly declared.

Actual Behavior

Default values merged with override map.

Steps to Reproduce

  1. terraform apply
  2. Check outputs

    Important Factoids

References

bug core

Most helpful comment

Interestingly this is not the behavior when you use modules:

# terraform.tfvars
mymap={ foo = "bar", baz = "biff" }
variable "mymap" {
  type = "map"
  default = { "zonk" = "pow" }
}
# mymod/test.tf
data "template_file" "test" {
  template = "${data}"

  vars {
    data = "${join(",", formatlist("key: %s, val: %s. ", keys(var.mymap), values(var.mymap)))}"
  }
}

output "test" {
  value = "${data.template_file.test.rendered}"
}
# mymod.tf
module "mymod" {
  source = "./mymod"
  mymap = {
    "test" = "meh"
  }
}

output "mod_test" { 
  value = "${module.mymod.test}"
}

Outputs:

data.template_file.test: Refreshing state...

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

mod_test = key: test, val: meh.

I would actually like the default variables in a module to merge with the values I pass in for a map, but it doesn't ¯\_(ツ)_/¯

All 8 comments

is this a bug or a feature? I thought the documentation used to say explicitly that mappings declared via .tfvars orTF_VAR_name were supposed to be merge() with the default value.

Hi,

Yes, merging the maps is the behavior we expected. @mitchellh tagged this as a bug however, so I'll see if he had any ideas for enhancement in mind before closing this.

I was only quickly categorizing and not reading too deeply into the issue. If this is working as designed AND it is clearly documented, I'll close this.

That behavior is documented, but surprising to me. Going forward, I'll use length() and pick() where the map is actually referenced when I need an override rather than a merge.

Honestly, if I wanted a merge, it would have been a lot more straightforward to do it explicitly where the variable is consumed rather than implicitly where the variable is declared.

@cbarbour That's true, and I think its worth looking into the UX here, it seems sensible that single values should be overridable.

Interestingly this is not the behavior when you use modules:

# terraform.tfvars
mymap={ foo = "bar", baz = "biff" }
variable "mymap" {
  type = "map"
  default = { "zonk" = "pow" }
}
# mymod/test.tf
data "template_file" "test" {
  template = "${data}"

  vars {
    data = "${join(",", formatlist("key: %s, val: %s. ", keys(var.mymap), values(var.mymap)))}"
  }
}

output "test" {
  value = "${data.template_file.test.rendered}"
}
# mymod.tf
module "mymod" {
  source = "./mymod"
  mymap = {
    "test" = "meh"
  }
}

output "mod_test" { 
  value = "${module.mymod.test}"
}

Outputs:

data.template_file.test: Refreshing state...

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

mod_test = key: test, val: meh.

I would actually like the default variables in a module to merge with the values I pass in for a map, but it doesn't ¯\_(ツ)_/¯

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings