尝试多次循环小部件字段时出现 400 Terraform 代码错误


I have the following code which I am trying to implement for multiple widgets in a single cloud watch dashboard
`    locals {
      instances = csvdecode(file("${path.module}/sample.csv"))
    }
    resource "aws_cloudwatch_dashboard" "main" {
      dashboard_name = "my-dashboard"
      dashboard_body = jsonencode(
     {
       "widgets": [
    for inst in range(length(local.instances)):[  
// i want to repeat the below section as the length of instances variable but getting an error  
           {   
              "type":"metric",
              "x":0,
              "y":0,
              "width":12,
              "height":6, 
              "properties":{
                 "metrics":[ // trying to implement multiple widget in a single dashboard 
    enter code here
                    [
                       "AWS/EC2",
                       "CPUUtilization",
                       "InstanceId",
                       "${local.instances[inst].instance_id}"
                    ]
                 ],
                 "period":300,
                 "stat":"Average",
                 "region":"ap-south-1",
                 "title":"EC2 Instance CPU",
                 "annotations": {
                    "horizontal": [
                          {
                             "label": "Untitled annotation",
                             "value": 1.01
                         }]
                       }          }
           }
      ]]
     })
    }   `

我在错误下面收到此错误:

错误:放置仪表板失败:无效

参数输入:仪表板正文无效,有 4 个验证错误:[ { "dataPath": "/widgets/0", "消息":"应该是对象" }, { "dataPath": "/widgets/1", "消息":"应该是对象" }, { "dataPath": "/widgets/2", "消息":"应该是对象" }, { "dataPath": "/widgets/3", "消息":"应该是对象" }] 状态代码:400,请求 ID:706AC87C-A796-11E9-8983-65D87C7656B4

代码生成如下,

{
  "widgets": [
    [ // <--- It seems to be wrong.
      {
        "height": 6,
...

小部件在列表中具有列表。所以修改如下,

jsonencode(
    {
      "widgets" : [ //removed [ 
        for inst in range(length(local.instances)) :
        {
...
            "annotations" : {
              "horizontal" : [
                {
                  "label" : "Untitled annotation",
                  "value" : 1.01
              }]
            }
          }
        }
      ] // and removed ] 
  })

删除嵌套列表。

最新更新