【新機能】CloudWatch ダッシュボードAPI

2017.07.06

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

渡辺です。

みんな大好きCloudWatchに待望のアップデートです。 CloudWatch ダッシュボードがAWS CLIから登録できるようになりました。

put-dashboard

put-dashboardは、ダッシュボードを登録します。

--dashboard-nameでダッシュボード名を、--dashboard-bodyで表示するウィジェットを指定します。 dashboard-bodyはJSONファイルを指定するのが無難でしょう。

test.json

{
    "widgets": [
        {
            "type": "metric",
            "x": 0,
            "y": 0,
            "width": 6,
            "height": 6,
            "properties": {
                "view": "timeSeries",
                "stacked": false,
                "metrics": [
                    [ "Classmethod/EC2", "MemoryUtilization", "InstanceId", "i-xxxxxxxx" ]
                ],
                "region": "ap-northeast-1"
            }
        },
        {
            "type": "metric",
            "x": 6,
            "y": 0,
            "width": 6,
            "height": 6,
            "properties": {
                "view": "timeSeries",
                "stacked": false,
                "metrics": [
                    [ "Classmethod/EC2", "DiskUsage", "InstanceId", "i-xxxxxxxx" ]
                ],
                "region": "ap-northeast-1"
            }
        }
    ]
}
$ aws cloudwatch put-dashboard --dashboard-name test --dashboard-body file://test.json
{
    "DashboardValidationMessages": []
}

マネジメントコンソールから確認できました。

CloudWatch_Management_Console

list-dashboards

list-dashboardsは、ダッシュボードの一覧を取得します。

$ aws cloudwatch list-dashboards 
{
    "DashboardEntries": [
        {
            "DashboardName": "test", 
            "LastModified": "2017-07-05T23:46:32Z", 
            "Size": 908
        }
    ]
}

get-dashboard

get-dashboardは、ダッシュボードの情報を取得します。

$ aws cloudwatch get-dashboard --dashboard-name test 
{
    "DashboardArn": "arn:aws:cloudwatch::378384917084:dashboard/test", 
    "DashboardBody": "{\n    \"widgets\": [\n        {\n            \"type\": \"metric\",\n            \"x\": 0,\n            \"y\": 0,\n            \"width\": 6,\n            \"height\": 6,\n            \"properties\": {\n                \"view\": \"timeSeries\",\n                \"stacked\": false,\n                \"metrics\": [\n                    [ \"Classmethod/EC2\", \"MemoryUtilization\", \"InstanceId\", \"i-xxxxxxxx\" ]\n                ],\n                \"region\": \"ap-northeast-1\"\n            }\n        },\n        {\n            \"type\": \"metric\",\n            \"x\": 6,\n            \"y\": 0,\n            \"width\": 6,\n            \"height\": 6,\n            \"properties\": {\n                \"view\": \"timeSeries\",\n                \"stacked\": false,\n                \"metrics\": [\n                    [ \"Classmethod/EC2\", \"DiskUsage\", \"InstanceId\", \"i-xxxxxxxx\" ]\n                ],\n                \"region\": \"ap-northeast-1\"\n            }\n        }\n    ]\n}\n"
}

DashboardBodyがJSONの中にJSONとちょっと残念です。

まとめ

CloudWatchダッシュボードがAPIで登録できるようになりました。 EC2インスタンス起動時にダッシュボードを登録/更新する、起動中のEC2インスタンスの状態を表示するダッシュボードを作成するなど、応用範囲が広いAPIです!