if we want to add graphs of cloud watch to dashboard,we choose Actions and then Add to dashboard. this is a manual task we do to add graphs to dashboard. Can we automate it using AWS CDK or some other way ? Is it possible, if yes then how? if no, how can I do it differently?
Since you already have dashboards it is best to start with the source. The cloudwatch dashboard JSON is pretty straight forward and easy to manipulate.
From the console on dashboard screen go to
"Actions -> View/edit source"
I'll give the overview approach that will vary slightly based on if you are using the CLI or programing language. I'll show the commands with the CLI but they map to the SDK.
Step 1: get a dashboard source
$ cloudwatch get-dashboard --dashboard-name CloudTrail-Search
{
"DashboardName": "CloudTrail-Search",
"DashboardArn": "arn:aws:cloudwatch::717475838310:dashboard/CloudTrail-Search",
"DashboardBody": "{ CONTENTS REMOVED}"
}
Step 2: Add widget to DashboardBody
Step 3: Put the dashboard back
$ aws put-dashboard --dashboard-name <value> --dashboard-body <value>
With Cloud Formations you will need to manage the whole dashboard but it makes adding a widget to multiple dashboards easy.
Getting started with Cloud Formations
Cloud Formations Cloudwatch Dashboard
This is similar approach to cloudformation in that you manage the whole dashboard configuration. There is a lot more to terraform but this is what one of the resources looks like.
source "aws_cloudwatch_dashboard" "dashboard" {
dashboard_name = "Your-Beautiful-Dashbaord"
dashboard_body = <<EOF
{
THE_JSON_YOU_GOT_FROM_THE_DASHBOARD_SOURCE
EOF
}