Terraform - create list based on resource count query returned no results

I’m trying to create dynamically create a list of instance ids, to assign it to the loadbalancer. Based in this [question][1] I found I can do it using data "aws_instances"
Unfortunately I’m getting this error

Error: Your query returned no results. Please change your search criteria and try again.

This is the way I create my EC2 resources

resource "aws_instance" "one" {
  instance_type = "${var.lc_instance_type}"
  ami           = "${var.dev_ami}"
  count         = "${var.instance_count}"
  tags = {
    Name = "${var.name_prefix}-id
  }
}

And this is the way I’m using the data aws_instance and how I plant to use it in the EB resource

data "aws_instances" "read-ec2" {
  instance_tags= {
    Name = "${var.name_prefix}-id"
  }
}

resource "aws_elb" "loadbalancer" {
  instances = ["${data.aws_instances.read-ec2.ids}"]

  listener {
    ...
  }
}

Not sure if I’m using properly the instance_tags option.

Hey @DiegoTc
You can run both EC2 and ELB together and you don’t need to use data block. If you want to use it separately, you can use output for storing instance IDs.

For using it together, you can change ELB code as follows:

resource "aws_elb" "loadbalancer" {
  instances = ["${aws_instance.one.*.id}"]

  listener {
    ...
  }
}

And if you want to use it separately, create outputs.tf file with EC2 and use the below code:

output "one_ec2_id" {
  value = "${aws_instance.one.*.id}"
}

And your ELB code will have data block as follows:

data "terraform_remote_state" "ec2_data" {
  backend = "local"

  config {
    path = "${path.module}/ec2/terraform.tfstate"
  }
}

resource "aws_elb" "loadbalancer" {
  instances = ["${data.terraform_remote_state.ec2_data.one_ec2_id}"]

  listener {
    ...
  }
}

Hope this helps and is what you’re looking for.

Thanks,
Arman

Thanks, I found the correct way, you have a typo in your suggestion.
instances = aws_instance.ec2-first[*].id