best way to create multiple aws_instances...

109 views
Skip to first unread message

Britt Treece

unread,
Feb 4, 2016, 6:10:46 PM2/4/16
to Terraform
I'm doing this...

resource "aws_instance" "web" {
  count = "${length(split(",", lookup(var.subnet_ids, var.application)))}"
  subnet_id = "${element(split(",", lookup(var.subnet_ids, var.application)), count.index)}"
  ...
}

which works swimmingly. I end up with aws_instance.web.0 and aws_instance.web.1 in the correct subnets, etc.

Now i'm trying to extend it and create an aws_ebs_volume called data for each ec2 instance. However, I'm having trouble figuring out how to get the aws_instance id's to correlate with the correct ec2 instance. I was trying to get this to work... 

resource "aws_volume_attachment" "data" {
  count = "${length(split(",", aws_instance.web.*.id))}"
  device_name = "/dev/xvdb"
  volume_id = "${element(aws_ebs_volume.data.*.id, count.index)}"
  instance_id = "${element(aws_instance.web.*.id, count.index)}"
}

but I get this error...

* Error reading aws_instance.web count: strconv.ParseInt: parsing "${length(split(\",\", lookup(var.subnet_ids, var.application)))}": invalid syntax


Anyone have any good examples of this?

TIA

Igor Cicimov

unread,
Feb 4, 2016, 11:03:11 PM2/4/16
to Terraform
I think the problem is that "aws_instance.web.*.id" is a list and split is string operation. Can you just try:

count =
"${length(aws_instance.web.*.id)}"

or converting the list into comma separated string first and then split:

count = "${length(split(",", join(",", aws_instance.web.*.id)))}"
Reply all
Reply to author
Forward
0 new messages