如何在 terraform 中构建地图列表 [英] how do i build a list of maps in terraform
问题描述
我想在 terraform 中构建地图列表.有没有可以让我这样做的手术.
i would like to build a list of maps in terraform. Is there an operation that would let me do that.
例如:我想构建结构(来自 aws_instance.test.*.private_ip)
eg: i would like to build the structure (from aws_instance.test.*.private_ip)
addresses = [
{
address = private_ip-1
},
{
address = private_ip-2
}
...
]
推荐答案
抱歉,如果这个问题已经解决了,我知道这是一个老问题.
Apologies if this has already been resolved, I see that it's an older question.
我不确定这是否是最佳做法,但这是我尝试达到所需状态的方式.
I'm not sure if this is best practice, but this is the way I would attempt to reach the desired state.
使用 null_resource 您可以执行以下操作:
Using the null_resource you'd be able to do the following:
variable "addresses" {
type = "list"
default = [
"private_ip-1",
"private_ip-2",
"private_ip-3"
]
}
resource "null_resource" "test" {
count = "${length(var.addresses)}"
triggers {
address = "${element(var.addresses, count.index)}"
}
}
output "addresses" {
value = "${null_resource.test.*.triggers}"
}
并且有这个输出:
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
addresses = [
{
address = private_ip-1
},
{
address = private_ip-2
},
{
address = private_ip-3
}
]
我目前使用的是 terraform 0.11.5,null_resource 似乎已添加到 0.6.7
I'm currently on terraform 0.11.5, null_resource appears to have been added in 0.6.7
但 null_resource 触发器有一些限制.插值变量只能产生字符串.因此,不幸的是,您将无法插入会导致列表或地图的值;例如:
There are limitations to the null_resource triggers though. interpolated variables can only result in strings. So, unfortunately you won't be able to interpolate a value that would result in a list or a map; for example:
resource "null_resource" "test" {
triggers {
mylist = "${var.addresses}"
}
}
会导致错误
错误:null_resource.test:触发器(地址):''预期类型'string',得到不可转换类型'[]interface {}'
Error: null_resource.test: triggers (address): '' expected type 'string', got unconvertible type '[]interface {}'
这篇关于如何在 terraform 中构建地图列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!