terraform: create list based on resource count

偶尔善良 提交于 2020-05-17 05:25:33

问题


We have a bunch of instances (I know... cattle, not pets, but in this case, these are really pets)

resource "aws_instance" "read_00" {
  count = "${var.read_00_count}"

resource "aws_instance" "read_01" {
  count = "${var.read_01_count}"

And we have an ELB where we want to dynamically add the instances based on their count variable, like so:

resource "aws_elb" "read_slaves" {
  instances = ["${aws_instance.read_.*.id}"]

But that doesn't work, of course.

Is it possible to dynamically create a list of instance ids ONLY if their count is not zero?

I know this goes against the grain, but if this is possible, that would be awesome.


回答1:


With Terraform 0.12 that will be much easier, but for now something like this would do:

[...]
resource "aws_instance" "read_01" {
  [...]
  count = "${var.read_01_count}"
  tags {
    Role = "read_slave"
  }
}

data "aws_instances" "read-slaves" {
  instance_tags {
    Role = "read_slave"
  }
  // optional filters
}

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

  listener {
    ...
  }
}

Thus:

  • tagging each instance which acts as a read slave
  • collect the list of aws_intances
  • create the aws_elb based on the collected data


来源:https://stackoverflow.com/questions/53280855/terraform-create-list-based-on-resource-count

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!