从一个json文件中读取terraform变量的键值



我有以下示例json文件:.json文件

[
{
"ParameterKey": "key1",
"ParameterValue": "valueofthekey1" 
},
{
"ParameterKey": "key2",
"ParameterValue": "valueofthekey2"
}
]

资源tf文件:


locals {
local_data = jsondecode(file("./modules/path/file.json"))
}
resource "aws_ssm_parameter" "testing1" { 
type = "String" 
name = "test_name1" 
value = local.local_data.valueofthekey1
}
resource "aws_ssm_parameter" "testing2" { 
type = "String" 
name = "test_name2" 
value = local.local_data.valueofthekey2
}

有什么线索吗?我如何读取json文件并在第一个资源中传递key1的值,然后在第二个资源中发送key2??

我尝试使用local,但他们显示了以下错误:12:value=local.local_data.testing1|----------------|local_data是具有2个元素的元组

如果您希望ParameterKey是参数的名称,您可以执行:

resource "aws_ssm_parameter" "testing" { 
count = length(local.local_data)
type = "String" 
name = local.local_data[count.index].ParameterKey
value = local.local_data[count.index].ParameterValue
}

但是,如果您希望整个json元素都是有值的,那么您可以执行以下操作:

resource "aws_ssm_parameter" "testing" { 
count = length(local.local_data)
type = "String" 
name = "test_name${count.index}" 
value = jsonencode(local.local_data[count.index])
}

最新更新