我正在使用Terraform编写AWS构建脚本.我在多个可用区域中启动了多个实例,在本例中,2:
resource "aws_instance" "myinstance" {
count = 2
ami = "${var.myamiid}"
instance_type = "${var.instancetype}"
availability_zone = "${data.aws_availability_zones.all.names[count.index]}"
# other details omitted for brevity
}
Run Code Online (Sandbox Code Playgroud)
我现在需要为这些实例分配一个弹性IP,以便将来可以在不更改IP地址的情况下重建实例.下面的说明我有什么喜欢做的事:
resource "aws_eip" "elastic_ips" {
count = 2
instance = "${aws_instance.myinstance[count.index].id}"
vpc = true
}
Run Code Online (Sandbox Code Playgroud)
但是这个错误有:
预期"}"但发现"."
我也尝试过使用lookup:
instance = "${lookup(aws_instance.sbc, count.index).id}"
Run Code Online (Sandbox Code Playgroud)
但是失败了同样的错误.
如何将弹性IP附加到这些实例?
BMW*_*BMW 10
element(list,index) - 返回给定索引处的列表中的单个元素.如果索引大于元素数,则此函数将使用标准mod算法进行换行.此功能仅适用于平面列表.例子:
element(aws_subnet.foo.*.id, count.index)
Run Code Online (Sandbox Code Playgroud)
所以在你的情况下,代码将是:
instance = "${element(aws_instance.myinstance.*.id, count.index}"
Run Code Online (Sandbox Code Playgroud)
多玩一点,我找到了答案-您可以索引到“ splat”语法:
instance = "${aws_instance.myinstance.*.id[count.index]}"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3645 次 |
| 最近记录: |