本文介绍了设置cloudwatch仪表板的小部件.得到错误“仪表板主体无效,存在1个验证错误".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

使用给定的代码获取以下错误:仪表板主体无效,存在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 } }]}"

      }
    }
解决方案

仪表板主体是一个字符串,因此将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"
                }
            }
        }

I am not able to ref servicename in the widget.

Getting the following error with the given code: The dashboard body is invalid, there are 1 validation errors: [ { "dataPath": "/widgets/0/properties/metrics/0", "message": "Should NOT have more than 3 items" } ] (Service: AmazonCloudWatch; Status Code: 400; Error Code: 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 } }]}"

      }
    }
解决方案

Dashboard body is a string, so putting the Sub syntax inside that string is making it part of the dashboard definition which in turn makes it invalid.

I'd suggest switching to yaml syntax. This will allow you to keep your dashboard definition cleaner and you can use the Sub like this:

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

Here is the same thing in 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"
                }
            }
        }

这篇关于设置cloudwatch仪表板的小部件.得到错误“仪表板主体无效,存在1个验证错误".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 07:04