0
0

【AWS】CLIでCloudWatchのカスタムダッシュボードを作成する

Last updated at Posted at 2023-10-14

はじめに

CLIでCloudWatchのカスタムダッシュボードを作成する手順。CLI操作用のIAMユーザ作成とか認証情報の設定は割愛。
1はカスタムダッシュボードはどんな感じかっていう話なので本題は2。

1. CloudWatchカスタムダッシュボードはこんな感じ(GUIで作ってみる)

CloudWatch -> ダッシュボード -> ダッシュボードの作成で画面右上の+か、+ 最初のウィジェットを追加をクリックする。
image.png

好きな形式を選ぶ
image.png

Lambdaの実行時間のグラフ。他に確認したいメトリクスがあればさらに追加する。
image.png

2. CLIでダッシュボードを作る

put-dashboardコマンドを使う。必須パラメータは--dashboard-name(ダッシュボード名)と--dashboard-body。(どんなウィジェットを含めるかJSON形式で指定する)

> aws cloudwatch put-dashboard --dashboard-name <String> --dashboard-body <String>

実際にやるとこんな感じ。(--dashboard-bodyの内容は変数に入れた)
propertiesのendpointで指定しているのはLambda関数。
CloudWatchはカスタムウィジェットとしてLambda関数も組み込める。

> $body = ‘"{
                \"widgets\":[
                    {\"type\":\"custom\",
                    \"x\":0,\"y\":0,
                    \"width\":6,
                    \"height\":6,
                    \"properties\":{
                        \"endpoint\":\"arn:aws:lambda:us-east-1:xxxxx:function:GetMetrics\",
                        \"updateOn\":{\"refresh\":true,
                                        \"resize\":true,
                                        \"timeRange\":true},
                        \"title\":\"test\"}
                    }
                ]
            }"’

> aws cloudwatch put-dashboard --dashboard-name test1 --dashboard-body $body
{
    "DashboardValidationMessages": []
}

Lambdaを使えばアラートの設定内容を表示させることも可能。(使い道あるかは分からない……)
alert.png

コマンドのドキュメントは以下

3. ダッシュボードの情報をゲットする

--dashboard-bodyどうやって書くんだ…っていうときの参考にどうぞ。

> aws cloudwatch get-dashboard --dashboard-name test1
{
    "DashboardArn": "arn:aws:cloudwatch::xxxxx:dashboard/test1",
    "DashboardBody": "{\"widgets\":[{\"type\":\"custom\",\"x\":0,\"y\":0,\"width\":6,\"height\":6,\"properties\":{\"endpoint\":\"arn:aws:lambda:us-east-1:xxxxx:function:GetMetrics\",\"updateOn\":{\"refresh\":true,\"resize\":true,\"timeRange\":true},\"title\":\"test\"}}]}",
    "DashboardName": "test1"
}

おわりに

--dashboard-bodyの指定が若干手間だけど、実行したらサクッとダッシュボード作れます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0