0
votes

I'm attempting to use an expression: Search in my CloudFormation in making a Dashboard.

{ "expression": "SEARCH('{AWS/ApiGateway,ApiName} MetricName=\"Count\"', 'Sum', 300)", "id": "e1" }

The line above is taken directly from the Cloudwatch source when creating a widget. This has to be modified for CloudFormation since Dashboard in CloudFormation is type string.

What I've altered it to looks like::

{ \"expression\": \"SEARCH('{AWS/ApiGateway,ApiName} MetricName=\"Count\"', 'Sum', 300)\", \"id\": \"e1\" }

The problem is that when I attempt to update CloudFormation with this change, it fails.

I've tried using documentation but AWS doesn't have anything for this type of problem and I can't seem to find proper resources to show how it should be done.

Any help would be great, Thanks.

-edit-

Here is a snippet of my template:

As for template I can give you snippets:

\"properties\": 
{
\"metrics\":
[
{ \"expression\": \"SEARCH('{AWS/ApiGateway,ApiName}   MetricName=\"Count\"', 'Sum', 300)\", \"id\": \"e1\" }
],
\"period\": 300,
\"stat\": \"Average\",
\"region\": \"$${AWS::Region}\"
}

I can tell everyone that the expression itself is the problem. If I were to remove that expression all together and save/update my template CloudFormation will update and push but the expression itself causes an error, probably because of the formatting.

-edit #2-

So I've been working on this and got some new info and a new error but I think it's a step forward.

{ \"expression\": \"SEARCH('{AWS/ApiGateway,ApiName} MetricName=\\\"Count\\\"', 'Sum', 300)\", \"id\": \"e1\" }

One of the devs at my work gave me this change to the code, thought that it would work but got this error with it.

The dashboard body is invalid, there are 2 validation errors:
[
{ "dataPath": "/widgets/4/properties/metrics/0", "message": "Should be array" }, 
{ "dataPath": "/widgets/4/properties/metrics/0", "message": "Field \"metrics\" has to be an array of array of strings, with an optional metricRenderer object as last element" } 
]

(Service: AmazonCloudWatch; Status Code: 400; Error Code:

1
Could you add your CloudFormation template and the error message you received?Pat Myron
The Error I'm getting is: The field DashboardBody must be a valid JSON object (Service: AmazonCloudWatch; Status Code: 400; Error Code: InvalidParameterInput; As for template I can give you snippets and I put it into the above.FubarP

1 Answers

0
votes

Few things here:

  1. metrics is an array of arrays, so your expression should be wrapped with [].
  2. Backslash in MetricName=\"Count\"' should also be escaped, change it to this: MetricName=\\\"Count\\\"'
  3. You need only one dollar sign in the region, change it to \"${AWS::Region}\". You'll also need to wrap the dashboard body into a Sub block for this to work.

Here is a full example similar to what you need:

{
    "Resources": {
        "ExampleDashboard": {
            "Type": "AWS::CloudWatch::Dashboard",
            "Properties": {
                "DashboardName": "SomeDashboard",
                "DashboardBody": {
                     "Fn::Sub": "{ \"widgets\": [{\"type\": \"metric\", \"x\": 0, \"y\": 0, \"width\": 12, \"height\": 6, \"properties\": { \"metrics\": [ [ { \"expression\": \"SEARCH('{AWS/ApiGateway,ApiName}   MetricName=\\\"Count\\\"', 'Sum', 300)\", \"id\": \"e1\" } ] ], \"region\": \"${AWS::Region}\", \"stat\": \"Average\", \"period\": 300, \"view\": \"timeSeries\", \"title\": \"CPUUtilization\", \"stacked\": false } } ] } "
                 }
            }
        }
    }
}