terraform:根据资源计数创建列表 [英] terraform: create list based on resource count
本文介绍了terraform:根据资源计数创建列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们有一堆实例(我知道......牛,不是宠物,但在这种情况下,这些真的是宠物)
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}"
我们有一个 ELB,我们希望根据它们的计数变量动态添加实例,如下所示:
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}"]
但这当然行不通.
只有当它们的计数不为零时,是否可以动态创建实例 ID 列表?
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.
推荐答案
使用 Terraform 0.12 会容易得多,但现在这样的事情会做:
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 {
...
}
}
因此:
- 标记每个充当读取从属设备的实例
- 收集
aws_intances
列表 - 根据收集的数据创建
aws_elb
这篇关于terraform:根据资源计数创建列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文