I'm starting with terraform and I'm building a small project, just some aws instances which one with an ebs volume attached.
I believe I'll start to work with terraform more, so I want to do the work by defining modules that I can then reuse in other projects.
For that, I created 3 modules (one for instances, another to create ebs volumes and the last one to attach the volumes).
On other place, I have a "main.tf" in which I call those modules. The problem I'm having is that to create and attach volumes I need some data like the instanceID.
To get the InstanceID I defined an output variable on the "instance" module (which has to be a list in case the instance count is more than one):
output "instance_id" {
value = ["${aws_instance.instance.*.id}"]
}
Then, on my main.tf file I call the variable on the module:
module "aws-instance" {
source = "../../Terraform/aws-instance"
instance_type = "t2.micro"
instance_count = "2"
}
(some other code...)
module "aws-volume-attachment" {
source = "../../Terraform/aws-volume-attachment"
device_name = "/dev/sdf"
instance_id = "${element("${module.aws-instance.instance_id}", count.index)}"
volume_id = "${element("${module.aws-ebs-volume.volume_id}", count.index)}"
}
But I get an error:
Error: module "aws-volume-attachment": count variables are only valid within resources
My question is, how can I loop over the variable so that I can attach each volume to one instance ?