Terraform: Variables of type map are requiring defaults when they shouldn't

Created on 11 Feb 2016  ยท  6Comments  ยท  Source: hashicorp/terraform

When I put this in variables.tf:

variable "foo" {
    type = "map"
}

and this in terraform.tfvars:

foo.bar = "baz"

Executing terraform plan errors out:

Errors:

  * 1 error(s) occurred:

* Required variable not set: foo

I can fix the error by adding a default value to my variable declaration:

variable "foo" {
    type = "map"
    default = {
        bar = "blah"
    }
}

Then an output declaration is displaying foo = baz in the plan/refresh execution as expected:

output "foo" {
    value = "${var.foo.bar}"
}

However, the documentation leads me to believe that a default value shouldn't be required. In the "VARIABLE CONFIGURATION" section of the documentation, it says this behind the "type" parameter:

this allows variables of type map to be set in the terraform.tfvars file without requiring a default value to be set.

then below that, it says

Default values can be either strings or maps, and if specified must match the declared type of the variable. If no value is supplied for a variable of type map, the values must be supplied in a terraform.tfvars file - they cannot be input via the console.

This issue makes it unusable for my intended use case, which is defining an "environment" variable map which I would define different environment variable keys/values (so staging and production would have different maps). There could be different map keys between environments, so I can't predefine them in a "default" block.

This is happening on Terraform v0.6.11.

bug core

Most helpful comment

With the new 0.7 map features, this is sort of flipped on its head but works now.

All 6 comments

I just ran into this myself. I'm glad to hear there's a workaround.

Noting you should also be able to work around this w/ an empty map:

variable "foo" {
  type    = "map"
  default = {}
}

Paging @jen20 who was in this area of the code recently.

using
variable "foo" {
type = "map"
default = {}
}
gives this kind of error:

Error running plan: 1 error(s) occurred:

  • 1:3: unknown variable accessed: var.foo.bar in:

${var.foo.bar}

I am seeing this same issue, should be able to pull default for tfvars file or environment variable.

With the new 0.7 map features, this is sort of flipped on its head but works now.

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

Related issues

kforsthoevel picture kforsthoevel  ยท  86Comments

phinze picture phinze  ยท  86Comments

shubhambhartiya picture shubhambhartiya  ยท  72Comments

dupuy picture dupuy  ยท  61Comments

radeksimko picture radeksimko  ยท  80Comments