在小部件部分循环

Loop on Widget part

如何仅迭代 Terraform 脚本的小部件部分并在单个仪表板中获取所有小部件?

locals {
  instances = csvdecode(file("${path.module}/sample.csv"))
}

// 如果我们使用计数,它将循环这部分

resource "aws_cloudwatch_dashboard" "main" {
  dashboard_name = "my-dashboard" 

  dashboard_body = <<EOF
 { 
   "widgets": [
       {
          "type":"metric",
          "x":0,
          "y":0,
          "width":12,
          "height":6,  

          "properties":{
             "metrics":[ 
              for itr in local.instances.id:

                [
                   "AWS/EC2",
                   "CPUUtilization",
                   "InstanceId",
                   itr   // want this section to fetch the value form excel 
                ]

             ],
             "period":300,
             "stat":"Average",
             "region":"ap-south-1",
             "title":"EC2 Instance CPU ",
             "annotations": {
                "horizontal": [
                      {
                         "label": "Untitled annotation",
                         "value": 2
                     }]}

          }},]}EOF}

我尝试使用两个 template_file 资源。

sample.csv 测试

instance_id
i-00001
i-00002
i-00003
  1. 创建一个 template_file 循环资源,
data "template_file" "ec2_metric" {
  count    = length(local.instances)
  template = jsonencode([ "AWS/EC2", "CPUUtilization", "InstanceId", element(local.instances.*.instance_id, count.index)])
}
  1. 为整个 json
  2. 创建一个 template_file
data "template_file" "widgets" {
  template = <<JSON
  { 
   "widgets": [
       {
          "type":"metric",
          "x":0,
          "y":0,
          "width":12,
          "height":6,  

          "properties":{
             "metrics":[ 
               ${join(", \n               ", data.template_file.ec2_metric.*.rendered)}

             ],
             "period":300,
             "stat":"Average",
             "region":"ap-south-1",
             "title":"EC2 Instance CPU ",
             "annotations": {
                "horizontal": [
                      {
                         "label": "Untitled annotation",
                         "value": 2
                     }]}

          }},]}
JSON
}
  1. 使用 template_file,
resource "aws_cloudwatch_dashboard" "main" {
  dashboard_name = "my-dashboard" 

  dashboard_body = data.template_file.widgets.rendered
...
}
  1. 测试template_file.widgets
output "test" {
  value = data.template_file.widgets.rendered
}

结果

Outputs:

test =   { 
   "widgets": [
       {
          "type":"metric",
          "x":0,
          "y":0,
          "width":12,
          "height":6,  

          "properties":{
             "metrics":[ 
               ["AWS/EC2","CPUUtilization","InstanceId","i-00001"], 
               ["AWS/EC2","CPUUtilization","InstanceId","i-00002"], 
               ["AWS/EC2","CPUUtilization","InstanceId","i-00003"]
             ],
             "period":300,
             "stat":"Average",
             "region":"ap-south-1",
             "title":"EC2 Instance CPU ",
             "annotations": {
                "horizontal": [
                      {
                         "label": "Untitled annotation",
                         "value": 2
                     }]}

          }},]}

如果您的目标是生成 JSON,通常使用 jsonencodetemplate_file 更好,因为它可以自动处理 JSON 语法细节,因此避免需要调整文本模板的烦人细节以获得 JSON 正确。

例如:

dashboard_body = jsonencode({
  "widgets": [
    "type": "metric",
    "x": 0,
    "y": 0,
    "width": 12,
    "height": 6,

    "properties": {
      "metrics": [
        for inst in local.instances : [
          "AWS/EC2",
          "CPUUtilization",
          "InstanceId",
          inst.id,
        ]
      ],
      "period": 300,
      # etc, etc
    },
  ],
})

通过使用 jsonencode,您可以使用 Terraform 的任何常规语言特性和函数来生成您的数据结构,并让 jsonencode 函数将其转换为有效的 JSON 语法结束。