Terraform: Can't create an output map

Created on 12 Aug 2016  ·  6Comments  ·  Source: hashicorp/terraform

I'm pretty sure I have the correct syntax, but maybe not. There aren't any specific examples in the docs. Incidentally, the docs (https://www.terraform.io/docs/configuration/syntax.html) make mention of using colon to separate the key/value pairs in a map. That results in a syntax error. I used = instead since that is shown in most examples.

Terraform Version

Terraform v0.7.0

Affected Resource(s)

  • core

    Terraform Configuration Files

output "output-string" {
  value = "test-string"
}

output "output-list" {
  value = ["value1", "value2"]
}

variable "testmapvar" {
  type = "map"
  default = {
    key1 = "value1"
    key2 = "value2"
  }
}

output "output-mapfromvar" {
  value = "${var.testmapvar}"
}

output "output-mapdirect" {
  value = {
    key1 = "value1"
    key2 = "value2"
  }
}

Expected Behavior

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

Outputs:

output-list = [
    value1,
    value2
]
output-mapfromvar = {
  key1 = value1
  key2 = value2
}
output-mapdirect = {
  key1 = value1
  key2 = value2
}
output-string = test-string

Actual Behavior

Error configuring: 1 error(s) occurred:

* output output-directmap is not a valid type ([]map[string]interface {})

Steps to Reproduce

  1. terraform apply
bug core

Most helpful comment

Execellent! And you can do a map of lists the same way:

variable "teststring" {
  default = "testvarvalue"
}

output "output-test-map-of-strings" {
  value = "${
    map(
      "key1", "value1",
      "key2", "${var.teststring}value2"
    )
  }"
}

output "output-test-map-of-lists" {
  value = "${
    map(
      "key1", list("value11", "value12"),
      "key2", list("value21", "${var.teststring}value22")
    )
  }"
}

My immediate problems are solved, thanks!!

All 6 comments

Related mailing list post on this subject, including a workaround:
https://groups.google.com/forum/#!topic/terraform-tool/P_oH4WAo4l0

Execellent! And you can do a map of lists the same way:

variable "teststring" {
  default = "testvarvalue"
}

output "output-test-map-of-strings" {
  value = "${
    map(
      "key1", "value1",
      "key2", "${var.teststring}value2"
    )
  }"
}

output "output-test-map-of-lists" {
  value = "${
    map(
      "key1", list("value11", "value12"),
      "key2", list("value21", "${var.teststring}value22")
    )
  }"
}

My immediate problems are solved, thanks!!

Okay, I realize I'm probably mis-using outputs, but I've just stumbled onto this:

Using this workaround with nested maps which themselves have values which are lists, causes the output to be dropped out of existence entirely!

# this works fine
output "nested_maps" {
  value = "${map(
            "nested_map", "${map(
              "nested_reference", "${module.vpc_0.vpc_id}"
            )}"
          )}"
}

# this doesn't:
output "nested_nested_list" {
  value = "${map(
            "nested_map", "${map(
              "nested_reference", "${module.vpc_0.vpc_id}",
              "nested_list", "${list(1,2,3)}"
            )}"
          )}"
}

The only output I'm getting is test. The vpcs output is entirely missing from the output of terraform apply and the state file:

{
  "version": 3,
  "terraform_version": "0.7.3",
  "serial": 10,
  "lineage": "f5b594b8-8989-4590-8815-b3d9c12d5141",
  "remote": {
    "type": "s3",
    "config": {
      ...
    }
  },
  "modules": [
    {
      "path": [
        "root"
      ],
      "outputs": {
        "nested_maps": {
          "sensitive": false,
          "type": "map",
          "value": {
            "nested_map": {
              "nested_reference": "some_value"
            }
          }
        }
      },
...

Regardless whether I use list() or reference an output of a resource/module which is a list, I see the same result.

¯\_(ツ)_/¯

Ah, I just learned that maps must have consistently typed values. When I tried this again with TF_LOG=1 I see a warning about it dropping the output. Still, might be nice to surface stuff like that to the default output.

Hi, this should work in the current version:

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

Outputs:

output-list = [
    value1,
    value2
]
output-mapdirect = {
  key1 = value1
  key2 = value2
}
output-mapfromvar = {
  key1 = value1
  key2 = value2
}
output-string = test-string

Closing as fixed.

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