Search code examples
amazon-cloudwatchamazon-cloudwatch-metrics

Getting a "singleValue" image from aws CLI using "aws cloudwatch get-metric-widget-image"


In the AWS Console I have this metric visible:

enter image description here

I try to get the same image using the AWS CLI (using the json in the Source tab of Cloudwatch):

aws cloudwatch get-metric-widget-image --metric-widget '{
    "metrics": [
        [ "AWS/ApiGateway", "Count", "ApiName", "MikeGw", "Resource", "/x/y", "Method", "POST", "Stage", "prod", { "stat": "Sum" } ]
    ],
    "view": "singleValue",
    "period": 300,
    "title": "Number of api calls",
    "setPeriodToTimeRange": true,
    "width": 1430,
    "height": 250,
    "start": "-PT3H",
    "end": "P0D"
}' | jq -r '.MetricWidgetImage' | base64 --decode >| image.png

and the result looks like this:

enter image description here

It seems to be ignoring "view": "singleValue".

How do I get images that are just counts using this approach?


Solution

  • The single value widgets are not supported.

    Since this is not really a graph, it's just a number, you can use the GetMetricData API to get this number and display it in some other way.

    Alternatively, if you don't mind getting the graph, you can use the GetMetricWidgetImage API to get the full graph like you did and use dynamic labels on the graph to put the total number of calls in the graph legend. See here: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/graph-dynamic-labels.html

    This will give you the same graph you have in your question, but the label will be something like [Sum: 11] Count instead of just Count.