AWS Terraform cloudwatch - 从列表中读取的仪表板正文

AWS Terraform cloudwatch - Dashboard body reading from a list

我在 terraform 上的 AWS_CloudWatch_Resource 有以下正文:

 dashboard_body = jsonencode(
 dashboard_body = jsonencode(
{ 
    "widgets": [
        {
            "type": "metric",
            "x": 0,
            "y": 0,
            "width": 9,
            "height": 6,
            "properties": {
                "view": "bar",
                "stacked": false,
                "metrics": [
                    [ "AWS/AutoScaling", "GroupDesiredCapacity", "AutoScalingGroupName", "Momo-Test-ASG1" ],
                    [ ".", "GroupMaxSize", ".", "." ],
                    [ ".", "GroupTotalCapacity", ".", "." ],
                    [ ".", "GroupTotalInstances", ".", "." ],
                    [ ".", "GroupInServiceInstances", ".", "." ]
                ],
                "region": "eu-central-1",
                "title": "ASG1 statistics"
            }
        },
        {
            "type": "metric",
            "x": 9,
            "y": 0,
            "width": 9,
            "height": 6,
            "properties": {
                "view": "bar",
                "stacked": false,
                "metrics": [
                    [ "AWS/AutoScaling", "GroupDesiredCapacity", "AutoScalingGroupName", "Momo-Test-ASG2" ],
                    [ ".", "GroupMaxSize", ".", "." ],
                    [ ".", "GroupTotalCapacity", ".", "." ],
                    [ ".", "GroupTotalInstances", ".", "." ],
                    [ ".", "GroupInServiceInstances", ".", "." ]
                ],
                "region": "eu-central-1",
                "period": 300,
                "title": "ASG2 statistics"
            }
        },
        {
            "type": "explorer",
            "x": 0,
            "y": 6,
            "width": 24,
            "height": 15,
            "properties": {
                "metrics": [
                    {
                        "metricName": "CPUUtilization",
                        "resourceType": "AWS::EC2::Instance",
                        "stat": "Average"
                    },
                    {
                        "metricName": "NetworkIn",
                        "resourceType": "AWS::EC2::Instance",
                        "stat": "Average"
                    },
                    {
                        "metricName": "DiskReadOps",
                        "resourceType": "AWS::EC2::Instance",
                        "stat": "Average"
                    },
                    {
                        "metricName": "DiskWriteOps",
                        "resourceType": "AWS::EC2::Instance",
                        "stat": "Average"
                    },
                    {
                        "metricName": "NetworkOut",
                        "resourceType": "AWS::EC2::Instance",
                        "stat": "Average"
                    }
                ],
                "aggregateBy": {
                    "key": "*",
                    "func": "AVG"
                },
                "labels": [
                    {
                        "key": "aws:autoscaling:groupName",
                        "value": "Momo-Test-ASG1"
                    },
                    {
                        "key": "aws:autoscaling:groupName",
                        "value": "Momo-Test-ASG2"
                    }
                ],
                "widgetOptions": {
                    "legend": {
                        "position": "bottom"
                    },
                    "view": "timeSeries",
                    "stacked": false,
                    "rowsPerPage": 40,
                    "widgetsPerRow": 3
                },
                "period": 300,
                "splitBy": "",
                "title": "Average ASG1 and ASG2"
            }
        },
        {
            "type": "metric",
            "x": 0,
            "y": 21,
            "width": 6,
            "height": 6,
            "properties": {
                "metrics": [
                    [ { "expression": "AVG(METRICS())", "label": "Average", "id": "e1" } ],
                    [ "CWAgent", "mem_used_percent", "InstanceId", "i-0f67225a5c04aebf9", "AutoScalingGroupName", "Momo-Test-ASG2", "ImageId", "ami-0502e817a62226e03", "InstanceType", "t2.micro", { "yAxis": "left", "id": "m1" } ],
                    [ "...", "i-00198c860886391f4", ".", "Momo-Test-ASG1", ".", ".", ".", ".", { "id": "m2" } ]
                ],
                "view": "timeSeries",
                "stacked": false,
                "region": "eu-central-1",
                "period": 300,
                "stat": "Average",
                "title": "mem_used_percent"
            }
        }
    ]
}
  )
}

如您所见,我的 Momo-Test-ASG1(第一个自动缩放组)和 Momo-Test-ASG2(第二个 Autenter 代码缩放组)使用相同的小部件。

如果我要测试很多 ASG,那么为很多组硬编码相同的东西就会有问题。 是否有任何解决方案可以使 terraform 从列表中读取 ASG?而不是必须复制相同的部分?

HashiCorp Terraform 0.12 Preview: For and For-Each

使用for

locals {
  asg_names = [
    "Momo-Test-ASG1",
    "Momo-Test-ASG2",
  ]
}

locals {
  body = [for asg_name in local.asg_names :
        {
            type: "metric",
            x: 0,
            y: 0,
            width: 9,
            height: 6,
            properties: {
                view: "bar",
                stacked: false,
                metrics: [
                    [ "AWS/AutoScaling", "GroupDesiredCapacity", "AutoScalingGroupName", asg_name ],
                    [ ".", "GroupMaxSize", ".", "." ],
                    [ ".", "GroupTotalCapacity", ".", "." ],
                    [ ".", "GroupTotalInstances", ".", "." ],
                    [ ".", "GroupInServiceInstances", ".", "." ]
                ]
                region: "eu-central-1",
                title: asg_name
            }
        }
        ]
}

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

  dashboard_body = jsonencode({ 
  widgets: concat(local.body, [{
      type: "text",
      x: 0,
      y: 7,
      width: 3,
      height: 3,
      properties: {
        markdown: "Hello world"
      }
    }])
  })
}