So we've created a dashboard in CloudWatch and we want it initialized by CDK every startup across all our environments. We noticed there's a view/edit source that you can copy paste a json in and we wondered is there a way to convert the View/Edit Source to CDK objects or widgets so it would be easier to maintain?
You can do this using the low-level L1 CfnDashboard
construct. L1 constructs map 1 to 1 to CloudFormation resources, and since CloudFormation supports creating a dashboard from JSON, this can be done in CDK.
Simply provide your JSON string to the dashboardBody
prop of CfnDashboard
.
Keep in mind, though, that all the metric names and regions will be hardcoded, so if you need them to change based on the environment, you'll need to do that yourself.
If your goal is ease of maintainability, I would strongly suggest converting your dashboard to CDK code. This should be straightforward to do and will give you readability and ease of modification.