-
Notifications
You must be signed in to change notification settings - Fork 9.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using count index to interpolate other variables? #408
Comments
This was also requested in #398. Wrote up my notes in that thread. |
Hmm, I'm not sure if it is the same yet. For static things (like a hostname) you can use |
I think we need to document how to do this. You should use map structures for this (map variable types) with the |
@andyshinn yep you're right and I was unaware of the count.index until this thread. thanks! |
@mitchellh Using the lookup function works if the values are static. But if they're dynamic (like the subnet ids in this example), you can't reference them in a map since variable values are required to be static. Is there another way to accomplish this now? |
I just submitted #554 which helps solve this problem in a slightly different way. There's a new "element" interpolation function which you can use to get a specific index from a splat. So for the example above, if you use the count = 3 to generate your subnets as a multi-value variable like:
You then get a aws_subnet.coreospub.*.id splat to play around with. To use that when creating instances with the new element function, it'd look like:
Note that I made the element function wrap if the index is greater than the splat length, so if you do a count = 6, for example, you'll end up with 2 instances per AZ/subnet pair. |
Curious when will we be able to using count.index to reference a resource already created just as @andyshinn pointed. I ran into the same problem resource "aws_subnet" "public" { vpc_id = "${aws_vpc.default.id}" cidr_block = "${concat(var.vpc_cidr_block_base, ".", count.index ,".0/24")}" availability_zone = "${concat(var.region, lookup(var.vpc_availability_zone, concat("zone_", count.index)))}" count = "${var.zone_count}" depends_on = ["aws_vpc.default", "aws_internet_gateway.default"] } # Routing table for public subnets resource "aws_route_table" "public" { vpc_id = "${aws_vpc.default.id}" route { cidr_block = "0.0.0.0/0" gateway_id = "${aws_internet_gateway.default.id}" } depends_on = ["aws_vpc.default", "aws_internet_gateway.default"] } resource "aws_route_table_association" "public" { subnet_id = "${aws_subnet.public.${count.index}.id}" route_table_id = "${aws_route_table.public.id}" count = "${var.zone_count}" depends_on = ["aws_vpc.default", "aws_internet_gateway.default"] } Output module.vpc.aws_route_table_association.public.1: Creating... route_table_id: "" => "rtb-0668bf63" subnet_id: "" => "${aws_subnet.public.1.id}" module.vpc.aws_route_table_association.public.0: Creating... route_table_id: "" => "rtb-0668bf63" subnet_id: "" => "${aws_subnet.public.0.id}" module.vpc.aws_route_table_association.public.0: Error: The subnet ID '${aws_subnet.public.0.id}' does not exist (InvalidSubnetID.NotFound) |
Using the way @rcostanzo suggested above (i.e. using the element function) did the job for me. However I believe a way that provides more flexibility needs to be found and the only way I can imagine is eventually allowing for maps to use interpolated variables. |
I just tried to use the new
works fine. However, when I try and use that as a module i.e.
I run into Am I not referring to the |
I fixed my issue with #611 |
Just in case someone lands here trying to achieve this with 0.7 or later, the way to do this without relying on
Or, in my case:
|
@rafaelmagu that was the final touch in getting terraform to name instances based on their region variable and count. thank you ever so much! |
Anyway to reference data.aws_availability_zones.available.names[0] into this? I think I'm furthering the double interpolation pattern witnessed here |
I tried similar code above to create 3 instances but public IP is not getting attached to all three instances. It attaches it to only 1st instance: terraform code:
Above code creates 3 subnets and 3 different instances are attached to these 3 subnets. But only 1st instance is getting public ip. 2nd and 3rd are not getting public ip's. |
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. |
I'm attempting to create 3 subnets, each in different availability zones:
I was then trying to simplify the
aws_instance
resource duplication by usingcount
and interpolatingsubnet_id
:But I end up with a plan that tries to reference the string and not the actual
subnet_id
:Is there any way to accomplish this yet?
The text was updated successfully, but these errors were encountered: