Terraform for 循环
Terraform for loop
我一直在学习 Terraform,并且一直在玩仪表板。
我有以下生成仪表板的文件。
resource "aws_cloudwatch_dashboard" "main" {
dashboard_name = "sample_dashboard"
dashboard_body = <<EOF
{
"widgets": [
${templatefile("${path.module}/cpu.tmpl", { ids = aws_instance.web[*].id })},
${templatefile("${path.module}/network.tmpl", { ids = aws_instance.web[*].id })}
]
}
EOF
}
这是 cpu 模板文件。
{
"type": "metric",
"x": 0,
"y": 0,
"width": 12,
"height": 6,
"properties": {
"metrics": ${jsonencode([for id in ids : ["AWS/EC2","CPUUtilization","InstanceId", "${id}"]])},
"period": 300,
"stat": "Average",
"region": "us-east-1",
"title": "EC2 Instance CPU"
}
}
这里有网络模板文件。
{
"type": "metric",
"x": 12,
"y": 0,
"width": 12,
"height": 6,
"properties": {
"metrics": ${jsonencode([for id in ids :
["AWS/EC2", "NetworkIn", "InstanceId", "${id}"]
])},
"period": 300,
"stat": "Average",
"region": "us-east-1",
"title": "EC2 Instance Network"
}
}
一切都按预期进行,我得到以下仪表板。
我遇到的问题是在 for 循环中尝试添加另一个指标时出现错误。
{
"type": "metric",
"x": 12,
"y": 0,
"width": 12,
"height": 6,
"properties": {
"metrics": ${jsonencode([for id in ids :
["AWS/EC2", "NetworkIn", "InstanceId", "${id}"],
["AWS/EC2", "NetworkOut", "InstanceId", "${id}"]
])},
"period": 300,
"stat": "Average",
"region": "us-east-1",
"title": "EC2 Instance Network"
}
}
我收到以下错误。
Call to function "templatefile" failed: ./network.tmpl:9,70-71:
Invalid 'for' expression; Extra characters after the end of the 'for'
expression..
一如既往地感谢您的帮助。
解决此问题的一种方法是concat您的指标:
{
"type": "metric",
"x": 12,
"y": 0,
"width": 12,
"height": 6,
"properties": {
"metrics": ${jsonencode(concat([for id in ids :
["AWS/EC2", "NetworkIn", "InstanceId", "${id}"]
], [for id in ids :
["AWS/EC2", "NetworkOut", "InstanceId", "${id}"]
]))},
"period": 300,
"stat": "Average",
"region": "us-east-1",
"title": "EC2 Instance Network"
}
}
我一直在学习 Terraform,并且一直在玩仪表板。
我有以下生成仪表板的文件。
resource "aws_cloudwatch_dashboard" "main" {
dashboard_name = "sample_dashboard"
dashboard_body = <<EOF
{
"widgets": [
${templatefile("${path.module}/cpu.tmpl", { ids = aws_instance.web[*].id })},
${templatefile("${path.module}/network.tmpl", { ids = aws_instance.web[*].id })}
]
}
EOF
}
这是 cpu 模板文件。
{
"type": "metric",
"x": 0,
"y": 0,
"width": 12,
"height": 6,
"properties": {
"metrics": ${jsonencode([for id in ids : ["AWS/EC2","CPUUtilization","InstanceId", "${id}"]])},
"period": 300,
"stat": "Average",
"region": "us-east-1",
"title": "EC2 Instance CPU"
}
}
这里有网络模板文件。
{
"type": "metric",
"x": 12,
"y": 0,
"width": 12,
"height": 6,
"properties": {
"metrics": ${jsonencode([for id in ids :
["AWS/EC2", "NetworkIn", "InstanceId", "${id}"]
])},
"period": 300,
"stat": "Average",
"region": "us-east-1",
"title": "EC2 Instance Network"
}
}
一切都按预期进行,我得到以下仪表板。
我遇到的问题是在 for 循环中尝试添加另一个指标时出现错误。
{
"type": "metric",
"x": 12,
"y": 0,
"width": 12,
"height": 6,
"properties": {
"metrics": ${jsonencode([for id in ids :
["AWS/EC2", "NetworkIn", "InstanceId", "${id}"],
["AWS/EC2", "NetworkOut", "InstanceId", "${id}"]
])},
"period": 300,
"stat": "Average",
"region": "us-east-1",
"title": "EC2 Instance Network"
}
}
我收到以下错误。
Call to function "templatefile" failed: ./network.tmpl:9,70-71: Invalid 'for' expression; Extra characters after the end of the 'for' expression..
一如既往地感谢您的帮助。
解决此问题的一种方法是concat您的指标:
{
"type": "metric",
"x": 12,
"y": 0,
"width": 12,
"height": 6,
"properties": {
"metrics": ${jsonencode(concat([for id in ids :
["AWS/EC2", "NetworkIn", "InstanceId", "${id}"]
], [for id in ids :
["AWS/EC2", "NetworkOut", "InstanceId", "${id}"]
]))},
"period": 300,
"stat": "Average",
"region": "us-east-1",
"title": "EC2 Instance Network"
}
}