我正在為此模板尋找解決方案以獲取正確的 JSON 檔案。正如您在各節中看到的那樣,aws:RequestTag/***末尾缺少逗號。如果我在模板中使用逗號,那么最后一個字串的末尾會有一個不必要的逗號。
我想知道${jsonencode()}應該有幫助,但我仍然沒有意識到它是如何%{ for key in key_tag ~}一起使用的。
我將不勝感激任何幫助。
地形:
resource "local_file" "enforcetags" {
content = templatefile("${path.module}/enforcetags.tpl",
{
key_tag = ["development_prod", "production_prod", "rnd_prod"]
}
)
filename = "./enforce_tags.json"
}
模板:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "ec2:*",
"Resource": "*",
"Condition": {
"ForAllValues:StringEquals": {
"aws:TagKeys": ${jsonencode([for key in key_tag : "${key}"])}
},
"StringEqualsIfExists": {
%{ for key in key_tag ~}
"aws:RequestTag/${key}": ${jsonencode([for key in key_tag : "${key}"])}
%{ endfor ~}
}
}
}
]
}
輸出:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "ec2:*",
"Resource": "*",
"Condition": {
"ForAllValues:StringEquals": {
"aws:TagKeys": ["development_prod","production_prod","rnd_prod"]
},
"StringEqualsIfExists": {
"aws:RequestTag/development_prod": ["development_prod","production_prod","rnd_prod"]
"aws:RequestTag/production_prod": ["development_prod","production_prod","rnd_prod"]
"aws:RequestTag/rnd_prod": ["development_prod","production_prod","rnd_prod"]
}
}
}
]
}
uj5u.com熱心網友回復:
最后一個逗號,除了最后一個字串,它應該是:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "ec2:*",
"Resource": "*",
"Condition": {
"ForAllValues:StringEquals": {
"aws:TagKeys": ${jsonencode([for key in key_tag : "${key}"])}
},
"StringEqualsIfExists": ${jsonencode(
{for key in key_tag: "aws:RequestTag/${key}" => key_tag})}
}
}
]
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/367216.html
上一篇:如何獲取SNS主題中的最新訊息?
