Apply "count" to instance subnet without creating a list var

809 views
Skip to first unread message

John Parfitt

unread,
Jun 30, 2017, 2:22:31 PM6/30/17
to Terraform
i have a module that launches a vpn server. i want to modify it to launch a second, identical one in a different az/subnet. here is my code:

resource "aws_instance" "vpn_server" {
    count = 2
    instance_type = "t2.medium"
    subnet_id = "${element(var.dmzs, count.index)}"
    source_dest_check = false
}

i can easily make this work with AZs, since i have those in my main.tf and can pass in var.azs and apply to the element to that, but my subnets are in the module itself, and i need to use those.

how can i modify by above code to have the counter iterate over two subnets? kind of like:

subnet_id = "${element(["aws_subnet.dmz.id","aws_subnet.dmz2.id"], count.index)}"

i dont even know if i would use "element" in this case.

Lowe Schmidt

unread,
Jun 30, 2017, 5:02:27 PM6/30/17
to terrafo...@googlegroups.com
You'll have to output the subnet IDs as a list from the module.

something like 

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

the use something like 

${element(module.my_subnet_module.subnet_ids, count.index)}

--
Lowe Schmidt | +46 723 867 157

--
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-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/f8ebc282-b2f5-4fdf-9359-868fbd878955%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Parfitt

unread,
Jun 30, 2017, 6:12:35 PM6/30/17
to Terraform
It's all contained in the same module so i would just be outputting and referencing itself.

A better way to ask would be... say no modules are involved. How can i create two identical instances, in different subnets, without creating a list var of the subnets and then calling them as an element.

resource "aws_instance" "vpn_server" {
    count = 2
    subnet_id = "${aws_subnet.dmz.id, aws_subnet.dmz-2.id}"
}


To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

Lowe Schmidt

unread,
Jul 1, 2017, 5:15:34 AM7/1/17
to terrafo...@googlegroups.com
Sorry, my bad, I misread it as different module.

You should be able to use ${aws_subnet.my_subnet.*.id} as the list to loop over then. 


--
Lowe Schmidt | +46 723 867 157

To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/71cb01ef-9f4f-485b-adf1-c5a2bcbcb929%40googlegroups.com.

John Parfitt

unread,
Jul 5, 2017, 12:59:59 PM7/5/17
to Terraform
that doesn't work since the subnet resources are separate. we have "aws_subnet.dmz-primary.id" and  "aws_subnet.dmz-secondary.id". unfortunately these subnets were not "count" created either, so i cannot loop over the splat (*).

am i not able to have count apply to them individually?

if the answer to my above question is no, is there any advice on how to bring both of those subnets under a single, aws_subnet.dmz.id resource, using "count = 2" ?

i think if i can do that.. then i can move forward with creating the instances

Thanks
Reply all
Reply to author
Forward
0 new messages