terraform aws pass instance list from 1 module to another

amazon-web-services, module, terraform

Solution

First, define the output in your ec2 module:

output "instance_ids" {
  value = ["${aws_instance.web.*.id}"]
}

Note: the resource name `web` is an example. Please specify the actual resource name in the module.

Next, declare the list variable in your elb module:

variable "instances" {
  type = "list"
}

Finally, pass the output of the ec2 module to the elb module:

module "instances" {
  source = "../../../../modules/ec2"

  ami                         = "ami...."
  number_of_instances         = 2
  instance_type               = "t2.micro"
}

module "elb" {
  source = "../../../../modules/elb"

  name = "some elb"
  instances = ["${module.instances.instance_ids}"]
}

Problem

I'm creating 2 instances in 1 module and I now need to attach those 2 instances to an ELB which is created using another module (same file) - is this possible without manually specifying them? ``` module "instances" { source = "../../../../modules/ec2" ami = "ami...." number_of_instances = 2 instance_type = "t2.micro" } module "elb" { source = "../../../../modules//elb" name = "some elb" instances = ["???"] #something like ["${module.ec2.instances.id}"]? } ```

Original source