Variable Interpolation

130 views
Skip to first unread message

Matthew Ceroni

unread,
Oct 11, 2017, 11:11:54 PM10/11/17
to Terraform
Have a unique question, that I can't solve (maybe I can't do what I want).

Designing a VPC module, I have a variable availability_zones which defines which AZ to create subnets in. I want the default to be all availability zones in the region the VPC is being created in. Otherwise the user can supply the list of AZ to deploy into.

However I can't do variable interpolation within a variable.  I was trying to do something like

data "availabiilty_zones" "all" {}

variable "availability_zones" {
  type = "list"
  default = "${data.availability_zones.all.names}"
}

That fails since you can't do interpolation here.

Within the actual resource creation I use the variable availablity_zones in many places, as an example

resource "aws_subnet" "private" {

  count  = "${length(var.availability_zones)}"
  vpc_id = "${aws_vpc.environment.id}"

  cidr_block = "${cidrsubnet(aws_vpc.environment.cidr_block, var.cidr_block_bits, length(var.availability_zones) + count.index)}"

  /* load balance over all availability zones */
  availability_zone = "${element(var.availability_zones, count.index)}"

  /* private subnet, no public IPs */
  map_public_ip_on_launch = false

  /* merge all the tags together */
  tags = "${merge(var.tags, var.private_subnet_tags, map("Name", format("private-%d.%s", count.index, var.name)), map("builtWith", "terraform"))}"

}

I could replace all var.availablity_zones with a conditional CONDITIONAL ? TRUE : FALSE but that will get kind of ugly repeating that over and over. 

What I am basically looking for is a way to set that one place, and then just use it later on. 

Anshu Prateek

unread,
Oct 12, 2017, 12:24:16 AM10/12/17
to Terraform
Some of the ways I can think of:

1) Always use data.aws_availability_zones.available.names instead of var.availability_zones

2) Use locals to convert data.aws_availability_zones.available.names to local.availability_zone

3) Use output to get availability_zone into you state file location and then use that with data_get (similar to 1 but you query your state store instead of querying AWS everytime)

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/1314478f-95ad-4869-9d5c-e8d3195455cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
regards
Anshu Prateek
+91.991.610.2967
Reply all
Reply to author
Forward
0 new messages