Terraform: Module - output a list directly with the splat ('*') operator

Created on 30 Jun 2016  ยท  4Comments  ยท  Source: hashicorp/terraform

I'm creating a list of subnets in my module:

resource "aws_subnet" "public" {
  vpc_id            = "${var.vpc_id}"
  cidr_block        = "${element( var.cidrs, count.index)}"
  availability_zone = "${element(var.azs, count.index)}"
  count             = "${length(var.cidrs)}"

  tags      { Name = "${var.name}.${element(var.azs, count.index)}" }
  lifecycle { create_before_destroy = true }

  map_public_ip_on_launch = true
}

Setting the output as a list of the created subnet ids:

output "subnet_ids" { value = "${aws_subnet.public.*.id}" }
The error is:

output 'subnet_ids': use of the splat ('*') operator must be wrapped in a list declaration

Workaround:

output "subnet_ids" { value = "${split(",",join(",", aws_subnet.public.*.id))}" }

Terraform Version

Terraform v0.7.0-rc2 (46a0709bba004d8b6e0eedad411270b3ae135a9e)

Affected Resource(s)

  • splat ('*') operator
  • lists
  • outputs
  • core?

    Expected Behavior

The use of splat should be directly supported without hacking. ( It is already an array.)

Actual Behavior

I believe the splat operator is not aware of the fact, an output can be an array.

core question

Most helpful comment

Hi @ocsi01! Thanks for reporting this - it's actually a case of documentation lagging code. I assume from this that you are using a release candidate of Terraform 0.7 or a build from master. The correct output definition here is:

output "subnet_ids" {
    value = ["${aws_subnet.public.*.id}"]
} 

I do think it might be possible to expand these automatically without the [] wrapper - I'll open a separate issue for that for discussion - it would bring more consistency.

I'll go ahead and close this issue - if the fix proposed doesn't work for you please feel free to re-open!

All 4 comments

Hi @ocsi01! Thanks for reporting this - it's actually a case of documentation lagging code. I assume from this that you are using a release candidate of Terraform 0.7 or a build from master. The correct output definition here is:

output "subnet_ids" {
    value = ["${aws_subnet.public.*.id}"]
} 

I do think it might be possible to expand these automatically without the [] wrapper - I'll open a separate issue for that for discussion - it would bring more consistency.

I'll go ahead and close this issue - if the fix proposed doesn't work for you please feel free to re-open!

Hi, I've tried exactly that and doesn't work:
Terraform v0.10.7
output "private_subnets" {
value = ["${aws_subnet.private.*.id}"]
}

$ terraform plan
module 'cluster': aws_subnet.private.*.id is not a valid output for module network
please advise

Resolved with updateding to Terraform v0.10.8

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

ronnix picture ronnix  ยท  3Comments

sprokopiak picture sprokopiak  ยท  3Comments

rnowosielski picture rnowosielski  ยท  3Comments

c4milo picture c4milo  ยท  3Comments

franklinwise picture franklinwise  ยท  3Comments