How to use aws cloudwatch get-metric-widget-image?

I would like to get Cloudwatch screenshot automatically since I have many instances.

But when I try to run get-metric-widget-image by aws cli command tool, I always get error.

An error occurred (ValidationError) when calling the GetMetricWidgetImage operation: MetricWidget property 'metricWidget' has a bad JSON content.

Is there anyone who could help me out? Thanks.

I could not find an example from aws doc. No exact example in below link. https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/CloudWatch-Metric-Widget-Structure.html

My command is like this.

aws cloudwatch get-metric-widget-image  --metric-widget "{ "width":600,"height":395,"metrics":[["AWS/EC2","CPUUtilization","InstanceId","i-01234567890123456",{"stat":"Average"}]],"period":300,"start":"-P30D","end":"PT0H","stacked":false,"yAxis":{"left":{"min":0.1,"max":1},"right":{"min":0}},"title":"CPU","annotations":{"horizontal":[{"color":"#ff6961","label":"Troublethresholdstart","fill":"above","value":0.5}], "vertical":[{"visible":true, "color":"#9467bd","label":"Bugfixdeployed","value":"2018-11-19T07:25:26Z","fill":"after"}]}}}" --output-format "png" 

Answers 1

  • The best way to get the correct json for your request is to use CloudWatch Console to construct the graph, then click on the Source tab, select Image API view and click Copy Source to copy the json generated there. You also need to wrap the json in single quotes, like this:

    aws cloudwatch get-metric-widget-image --metric-widget \
    '{
        "width": 600,
        "height": 395,
        "metrics": [
            [ "AWS/EC2", "CPUUtilization", "InstanceId", "i-01234567890123456", { "stat": "Average" } ]
        ],
        "period": 300,
        "stacked": false,
        "yAxis": {
            "left": {
                "min": 0.1,
                "max": 1
            },
            "right": {
                "min": 0
            }
        },
        "title": "CPU",
        "annotations": {
            "horizontal": [
                {
                    "color": "#ff6961",
                    "label": "Troublethresholdstart",
                    "fill": "above",
                    "value": 0.5
                }
            ],
            "vertical": [
                {
                    "visible": true,
                    "color": "#9467bd",
                    "label": "Bugfixdeployed",
                    "value": "2018-11-19T07:25:26Z",
                    "fill": "after"
                }
            ]
        },
        "view": "timeSeries"
    }'
    

    Response to this will be a base64 encoded image, like this:

    {
        "MetricWidgetImage": "iVBORw0KGgoAAAANSUhEUgAAAlgAAAGLEAYA..."
    }
    

    If you need the raw png image, you'll need to decode the response by doing something like this:

    aws cloudwatch get-metric-widget-image --metric-widget 'JSON_GOES_HERE' | grep MetricWidgetImage | awk '{split($0,a,"\""); print a[4]}' | base64 --decode > graph.png
    

Related Articles