设置cloudwatch仪表板的小部件。收到错误“仪表板主体无效,存在1个验证错误”

时间:2019-06-16 04:35:08

标签: json amazon-web-services amazon-cloudformation amazon-cloudwatch amazon-cloudwatch-metrics

我无法在小部件中引用服务名称。

使用给定的代码获取以下错误: 仪表板主体无效,存在1个验证错误:[{“ dataPath”:“ / widgets / 0 / properties / metrics / 0”,“ message”:“最多不得超过3个项目”}](服务:AmazonCloudWatch ;状态代码:400;错误代码:InvalidParameterInput

"CloudwatchDashboard": {
      "Type": "AWS::CloudWatch::Dashboard",
      "Properties": {

"{ \"widgets\":
[{ \"type\":\"metric\", 
\"x\":0, 
\"y\":0, 
\"width\":12, 
\"height\":6, 
\"properties\":
{ \"metrics\":
[[ \"AWS/ECS\", \"CPUUtilization\", \"ServiceName\",
{ \"Fn::Sub\": [ \"${Service}\", { \"Service\": {\"Ref\" : \"AWS::StackName\" }} ]}]], 
\"region\": \"us-east-1\", 
\"stat\":\"Average\",
\"period\": 300, 
\"view\": \"timeSeries\", 
\"title\":\"CPUUtilization\", 
\"stacked\": false } }]}"

      }
    }

2 个答案:

答案 0 :(得分:0)

仪表板主体是一个字符串,因此将Sub语法放入该字符串中使其成为仪表板定义的一部分,从而使其无效。

我建议切换到yaml语法。这样可以使仪表板定义保持整洁,并可以像这样使用Sub

ExampleDashboard:
    Type: AWS::CloudWatch::Dashboard
    Properties: 
        DashboardName: 'SomeDashboard'
        DashboardBody: !Sub |
          {
              "widgets": [
                  {
                      "type": "metric",
                      "x": 0,
                      "y": 0,
                      "width": 12,
                      "height": 6,
                      "properties": {
                          "metrics": [
                              [ "AWS/ECS", "CPUUtilization", "ServiceName", "${AWS::StackName}"]
                          ],
                          "region": "us-east-1",
                          "stat": "Average",
                          "period": 300,
                          "view": "timeSeries",
                          "title": "CPUUtilization",
                          "stacked": false
                      }
                  }
              ]
          }

这是json中的同一件事:

"ExampleDashboard": {
            "Type": "AWS::CloudWatch::Dashboard",
            "Properties": {
                "DashboardName": "SomeDashboard",
                "DashboardBody": {
                    "Fn::Sub": "{\n    \"widgets\": [\n        {\n            \"type\": \"metric\",\n            \"x\": 0,\n            \"y\": 0,\n            \"width\": 12,\n            \"height\": 6,\n            \"properties\": {\n                \"metrics\": [\n                    [ \"AWS/ECS\", \"CPUUtilization\", \"ServiceName\", \"${AWS::StackName}\"]\n                ],\n                \"region\": \"us-east-1\",\n                \"stat\": \"Average\",\n                \"period\": 300,\n                \"view\": \"timeSeries\",\n                \"title\": \"CPUUtilization\",\n                \"stacked\": false\n            }\n        }\n    ]\n}\n"
                }
            }
        }

答案 1 :(得分:0)

您可以在yaml模板中以JSON定义CloudWatch仪表板,如下所示:

  ExampleDashboard:
    Type: AWS::CloudWatch::Dashboard
    Properties: 
      DashboardName: 'SomeDashboard'
      DashboardBody: !Sub '
{
    "widgets": [
        {
            "type": "metric",
            "x": 0,
            "y": 0,
            "width": 12,
            "height": 6,
            "properties": {
                "metrics": [
                    [ "AWS/ECS", "CPUUtilization", "ServiceName", "${AWS::StackName}"]
                ],
                "region": "us-east-1",
                "stat": "Average",
                "period": 300,
                "view": "timeSeries",
                "title": "CPUUtilization",
                "stacked": false
            }
        }
    ]
}
'