【问题标题】:AWS Terraform cloudwatch - Dashboard body reading from a listAWS Terraform cloudwatch - 从列表中读取的仪表板正文
【发布时间】:2021-05-22 16:01:48
【问题描述】:

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

 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?而不必复制相同的部分?

【问题讨论】:

    标签: amazon-web-services amazon-cloudwatch terraform-provider-aws


    【解决方案1】:

    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"
          }
        }])
      })
    }
    

    【讨论】:

    • 非常感谢,但是当我尝试在写好的表达式之后添加一个新的小部件时,像这样 : , { "type": "explorer", "x": 0, "y": 6,“width”:24,“height”:15,“properties”:{“metrics”:[我得到错误:循环表达式后的额外字符,你知道我怎样才能添加新的小部件而不会出现这个错误吗?非常感谢您。
    • @Nidorino94 我在: 和循环之间有一个额外的空格。语法for index, asg_name in local.asg_names:。更新
    • 它没有解决问题。尝试添加新小部件时,通过将第一个小部件与 for loop 分开,例如 for XXXXX {type:metric.......}, {type:explorer .....} 然后我总是遇到同样的问题.在您最初的回复中,您编写了更多小部件……您如何实现它们?您能否添加一个示例,说明至少另一个与上述小部件一起使用的小部件?谢谢
    • @Nidorino94 使用仪表板快照更新了工作代码。
    • 我明白,但我的问题是:如何在使用 for 循环时分隔两个或多个小部件?我的意思是,您只使用一个小部件,但是使用两个小部件有点问题?您可以在上面的代码中添加另一个小部件吗?提前致谢 !你的帮助太宝贵了!
    猜你喜欢
    • 2021-09-30
    • 1970-01-01
    • 2022-01-13
    • 2021-07-07
    • 2021-10-21
    • 2023-03-03
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    相关资源
    最近更新 更多