【问题标题】:Terraform AWS Dashboard - Widgets from nested listTerraform AWS 仪表板 - 嵌套列表中的小部件
【发布时间】:2021-09-30 21:24:27
【问题描述】:

Terraform 初学者在这里。我正在尝试从嵌套列表中创建一些小部件。组将是一个“标签”小部件,指示组,然后是与该组相关的金丝雀的度量小部件。所以仪表板应该如下所示:

第 1 组
小部件1、小部件2 等
第 2 组
小部件3、小部件4等

变量值:

dashboard = [
    {
        name = "Group-1",
        canaries  = ["canary1", "canary2", "canary3"]
    },
    {
        name = "Group-2",
        canaries  = ["canary4", "canary5"]
    }
]

尝试构建 json:

locals {
    body = [for group in var.dashboard :
                #Create text widget for Group name
                {
                    "height": 1,
                    "width": 24,
                    "y": 4,
                    "x": 0,
                    "type": "text",
                    "properties": {
                        "markdown": "\n#  > [${group.name}]\n"
                    }
                }
                #Attempt to create underlying widgets for group
                [for canary in group.canaries :
                    {
                        {
                            "height": 3,
                            "width": 6,
                            "y": 5,
                            "x": 0,
                            "type": "metric",
                            "properties": {
                                "metrics": [
                                    [ "CloudWatchSynthetics", "Failed", "CanaryName", "${canary}", { "label": "Canary failures count", "region": "us-west-2" } ]
                                ],
                                "title": "Failed canary runs",
                                "period": 60,
                                "region": "us-west-2",
                                "stat": "Sum",
                                "view": "singleValue",
                                "setPeriodToTimeRange": true
                            }
                        }
                    }
                ] #TF Doesn't like the inclusion of nested loop here or my syntax is incorrect.
            ]
}

资源创建:

resource "aws_cloudwatch_dashboard" "canary_dashboard" {
    dashboard_name = "Canary-Dashboard"
    dashboard_body = jsonencode({
        "widgets": concat(local.body)
    })
}

在我创建body 时,Terraform 抱怨Missing close bracket on index,但我已经三次检查我没有遗漏括号或花括号。如何从嵌套列表动态创建仪表板小部件?

编辑
按照 Jordan 的建议,在下面包括所需的 json 输出。最后,会有n 个组,每个组有n 个属于该组的金丝雀。

{
    "widgets": [
        {
            "height": 1,
            "width": 24,
            "y": 4,
            "x": 0,
            "type": "text",
            "properties": {
                "markdown": "\n# Group1\n"
            }
        },
        {
            "height": 3,
            "width": 6,
            "y": 5,
            "x": 6,
            "type": "metric",
            "properties": {
                "metrics": [
                    [ "CloudWatchSynthetics", "Failed", "CanaryName", "Group1-Canary", { "label": "Canary failures count", "region": "us-west-2" } ]
                ],
                "title": "Failed canary runs",
                "period": 60,
                "region": "us-west-2",
                "stat": "Sum",
                "view": "singleValue",
                "setPeriodToTimeRange": true
            }
        },
        {
            "height": 1,
            "width": 24,
            "y": 4,
            "x": 0,
            "type": "text",
            "properties": {
                "markdown": "\n# Group2\n"
            }
        },
        {
            "height": 3,
            "width": 6,
            "y": 5,
            "x": 6,
            "type": "metric",
            "properties": {
                "metrics": [
                    [ "CloudWatchSynthetics", "Failed", "CanaryName", "Group2-Canary", { "label": "Canary failures count", "region": "us-west-2" } ]
                ],
                "title": "Failed canary runs",
                "period": 60,
                "region": "us-west-2",
                "stat": "Sum",
                "view": "singleValue",
                "setPeriodToTimeRange": true
            }
        },
    ]
}

【问题讨论】:

    标签: terraform dashboard terraform-provider-aws


    【解决方案1】:

    您正在尝试使用 Terraform 不允许的列表理解做一些事情(请参阅我在哪里标记了“这里”):

    locals {
        body = [for group in var.dashboard :
                    #Create text widget for Group name
                    {
                        "height": 1,
                        "width": 24,
                        "y": 4,
                        "x": 0,
                        "type": "text",
                        "properties": {
                            "markdown": "\n#  > [${group.name}]\n"
                        }
                    }                                                       <===== HERE
                    #Attempt to create underlying widgets for group
                    [for canary in group.canaries :
                        {
                            {
                                "height": 3,
                                "width": 6,
                                "y": 5,
                                "x": 0,
                                "type": "metric",
                                "properties": {
                                    "metrics": [
                                        [ "CloudWatchSynthetics", "Failed", "CanaryName", "${canary}", { "label": "Canary failures count", "region": "us-west-2" } ]
                                    ],
                                    "title": "Failed canary runs",
                                    "period": 60,
                                    "region": "us-west-2",
                                    "stat": "Sum",
                                    "view": "singleValue",
                                    "setPeriodToTimeRange": true
                                }
                            }
                        }
                    ] #TF Doesn't like the inclusion of nested loop here or my syntax is incorrect.
                ]
    }
    

    如果 TF 允许你做你想做的事,你最终会得到类似的结果:

    body = [
      {
        "height": 1,
        "width": 24,
        "y": 4,
        "x": 0,
        "type": "text",
        "properties": {
          "markdown": "\n#  > [${group.name}]\n"
        }
      },
      [
        {
            {
                "height": 3,
                "width": 6,
                "y": 5,
                "x": 0,
                "type": "metric",
                "properties": {
                    "metrics": [
                        [ "CloudWatchSynthetics", "Failed", "CanaryName", "${canary}", { "label": "Canary failures count", "region": "us-west-2" } ]
                    ],
                    "title": "Failed canary runs",
                    "period": 60,
                    "region": "us-west-2",
                    "stat": "Sum",
                    "view": "singleValue",
                    "setPeriodToTimeRange": true
                }
            }
        },
        {
            {
                "height": 3,
                "width": 6,
                "y": 5,
                "x": 0,
                "type": "metric",
                "properties": {
                    "metrics": [
                        [ "CloudWatchSynthetics", "Failed", "CanaryName", "${canary}", { "label": "Canary failures count", "region": "us-west-2" } ]
                    ],
                    "title": "Failed canary runs",
                    "period": 60,
                    "region": "us-west-2",
                    "stat": "Sum",
                    "view": "singleValue",
                    "setPeriodToTimeRange": true
                }
            }
        }
      ]
    ]
    

    我怀疑这就是你想要做的。如果您可以提供您希望 JSON 的样子的示例,我们可以向您展示如何实现它。

    【讨论】:

      猜你喜欢
      • 2021-05-22
      • 2014-04-02
      • 2021-01-27
      • 2013-11-05
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 2011-11-24
      • 2011-02-18
      相关资源
      最近更新 更多