Terraform: Support maps in output resources

Created on 29 Oct 2015  ·  14Comments  ·  Source: hashicorp/terraform

Please add support for map types in output resources. Like:

output "addresses" {
    value = {
        dc0 = "0.0.0.1"
        dc1 = "0.0.0.2"
    }
}

Outputs only support string values right now, and it makes it really ugly when you want to export arbitrary key/value pairs from a module.

I just had to write this code (which does work):

cidrsubnet(
    element(
        split(",", module.subnets.subnet_vals),
        index(
            split(",", module.subnets.subnet_ids),
            format("%s_aws_gen_%s_priv", var.environment, var.region)
        )
    ),
    module.subnets.bits,
    0
)

It depends on two outputs in the subnet module, subnet_vals and subnet_ids, both ,-joined lists.

An improvement with maps would be:

cidrsubnet(
    lookup(
        module.subnets.subnets,
        format("%s_aws_gen_%s_priv", var.environment, var.region)
    ),
    module.subnets.bits,
    0
)
core proposal

Most helpful comment

Outputting maps directly is not supported in 0.7.1, but there's a workaround by using interpolation -

output "az_public_subnets" {
  value = "${
    map(
      "a", "${aws_subnet.public_a.id}",
      "b", "${aws_subnet.public_b.id}",
      "c", "${aws_subnet.public_c.id}"
    )
  }"
}

Related:

8384 #8153

https://groups.google.com/forum/#!topic/terraform-tool/P_oH4WAo4l0

All 14 comments

@catsby Is this an approved proposal? Because I could probably work on it

Hi @sthulb... something along these lines is actually being worked on as part of the 0.7 development by @jen20, so probably best to hold on that and see how things look after he's finished his work. I've not been following closely the details, but I gather that something very like the motivating example in this issue should work once that set of changes is complete.

Hey @sthulb – glad to hear you're interested in contributing!

There is no formal approval process. We do have a contributing guide if you're interested, but we aren't too stringent about it:

Regarding this issue in particular, @apparentlymart is correct in that work has already been done, in fact it's already been merged into our v0.7.0 dev branch. If you're curious, the pull request is here:

I can't say when v0.7.0 will be released – I don't believe there is a set date – but I do know we'd like it to be sooner than later 😄

I'm going to close this now, thanks for bringing it up! Let me know if you have any other questions.

@catsby I'm not sure this issue is closed by @jen20's PR. I think there's more to cover, i.e. maps

Hi @sthulb! Map outputs are supported in 0.7 - just not via that PR. #5841 is the one you are looking for I think!

Outputting maps directly is not supported in 0.7.1, but there's a workaround by using interpolation -

output "az_public_subnets" {
  value = "${
    map(
      "a", "${aws_subnet.public_a.id}",
      "b", "${aws_subnet.public_b.id}",
      "c", "${aws_subnet.public_c.id}"
    )
  }"
}

Related:

8384 #8153

https://groups.google.com/forum/#!topic/terraform-tool/P_oH4WAo4l0

I think I found a slightly less gnarly workaround since maps are valid as variable defaults and variables can be piped to outputs. Tested in 0.7.7:

variable "networks" {
  type = "map"
  default = {
    myorg.ap-northeast-1.dev.vpc_cidr = "10.10.0.0/17"
    myorg.ap-northeast-1.ops.vpc_cidr = "10.10.128.0/17"
  }
}
output "networks" { value = "${var.networks}" }

That's a thing of beauty right there, @matschaffer. Thanks!

Is there a workaround that works for folks here? @matschaffer's doesn't work when you need to interpolate values and @eugene1g's silently fails to output anything when using v0.8.6. 😢

@LegNeato maybe try 0.8.7? Simple test of @eugene1g's approach seems to work here.

❯ cat mod/main.tf
variable "interpolated" {
  default = "an_interpolated_value"
}

output "az_public_subnets" {
  value = "${
    map(
      "a", "${var.interpolated}"
    )
  }"
}

❯ cat main.tf
module "tester" {
  source = "mod"
}

output "from_mod" {
  value = "${module.tester.az_public_subnets}"
}

❯ terraform apply

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

Outputs:

from_mod = {
  a = an_interpolated_value
}

❯ terraform -version
Terraform v0.8.7

@matschaffer just tried with v0.8.8, no go. I must be hitting another bug, I'll get a repro and file another issue.

Edit: My issue is https://github.com/hashicorp/terraform/issues/12403

+1 on that, need to output maps as well, for example a vpc module which creates subnets which needs to be used in other sibling modules:

variable "subnets" {
  type = "map"
  default = {
    16 = "${aws_vpc.16.id}"
    17 = "${aws_vpc.17.id}"
  ....
  }
}

Zipmap might be an option for that. We use it for our counted dynamic VPC resource outputs.

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