问题
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 } }]}"
}
}
回答1:
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"
}
}
}
来源:https://stackoverflow.com/questions/56616110/setting-the-widget-for-cloudwatch-dashboard-getting-the-error-the-dashboard-bo