Which metrics are essential to evaluate the state of your cloud infrastructure? Probably a lot. A dashboard allows you to keep an eye on all these metrics. For example, I like to monitor the following metrics for a typical 3-tier web application with the help of a CloudWatch dashboard:
load balancer: client-side and server-side error rates
load balancer: target response latency and number of requests
But how to create a customized CloudWatch Dashboard with CloudFormation? Doing so is possible with a simple CloudFormation template. However, I choose to use a custom resource to be more flexible when generating the dashboard. The following steps are needed to create a CloudWatch dashboard with a custom resource.
Create a CloudFormation template and add a Lambda-backed custom resource.
Write the code creating, updating, and deleting a CloudWatch dashboard.
Generate the JSON code describing the customized dashboard.
You will learn how to do so in more detail next.
Create a template and add a Lambda-backed custom resource
First of all, create a CloudFormation template. Add the identifiers of the resource you want to monitor to the parameters section as shown in the following code snippet.
--- Parameters: DashboardName: Description:'The name for the dashboard.' Type:String AlbFullName: Description:'The full-name of the ALB. (optional)' Type:String Default:'' RdsClusterName: Description:'The RDS Aurora Cluster name. (optional)' Type:String Default:'' # ...
Next, you need to add three resources to your template.
A custom resource Dashboard to manage the CloudWatch dashboard.
A Lambda function CustomResourceFunction executing your source code for the custom resource.
An IAM role CustomResourceRole assumed by the Lambda function to write logs as well as creating, updating and deleting CloudWatch dashboards.
Implement creating, updating, and deleting a CloudWatch dashboard
The following code snippet shows parts of dashboard.js including the handler which creates, updates, or deletes a CloudWatch dashboard. The built-in modules aws-sdk and cfn-response are used.
There is only one step missing: you need to add widgets to the dashboard.
Generate the JSON code describing the customized dashboard
To do so, you need to generate a JSON file describing your dashboard in Dashboard Body Structure and Syntax . I recommend to create your dashboard by clicking through the AWS Management Console first. Generating the dashboard’s JSON with the help of the View/edit source action gives you a good starting point.
The following code snippet shows the generateDashboard function, which generates the JSON defining a CloudWatch dashboard with two widgets:
Widget ALB: total number of incoming requests as well as the target latency in 99 and 95 percentile
Widget RDS: maximum read and write IOPS as well as the serverless capacity of the database
let dashboard = {widgets: []}; for (let w in widgets) { let widget = widgets[w]; let x = (w % 2) * 12; let y = Math.floor(w/2); widget.x = x; widget.y = y; widget.width = 12; widget.height = 6; dashboard.widgets.push(widget); }