Sub topic 12
Building dashboards
This chapter assumes you can run AWS CLI commands and know how metrics, namespaces, dimensions, and alarms work from the earlier chapters. It also assumes you have an EC2 instance that runs the CloudWatch agent and writes to the /app/web log group. The names below come from those chapters. Substitute your own.
What a dashboard is
A CloudWatch dashboard is a named page of widgets. Each widget draws one kind of data: metrics, log query results, alarm states, or text. A dashboard stores no data of its own. Every widget runs its query when you open the page, so a dashboard is only a saved layout of queries.
Dashboards belong to your account and are not tied to one region. Each widget carries its own region property, so one page can show a metric from us-east-1 next to a metric from eu-west-1.
Widget types
| Widget type | Shows | Typical use |
|---|---|---|
| `metric` | Lines, stacked areas, bars, numbers, gauges, or pie charts of metrics | CPU, memory, error counts |
| `log` | Results of a Logs Insights query as a table or time series | Recent errors, top requesters |
| `alarm` | The current state of one or more alarms | A health summary at the top of the page |
| `text` | Markdown text | Titles, runbook links, explanations |
How a dashboard pulls data together
The dashboard body
You create a dashboard with put-dashboard. The body is a JSON document with a widgets array. Each widget has a type, a position, and properties. The grid is 24 columns wide. x runs from 0 to 23, y is the row, and width and height are in grid units. Widgets that overlap are rejected.
Save this as dashboard.json. It combines four resources: the EC2 instance, the agent's memory metric, the metric filter's ErrorCount, and the log group.
{
"widgets": [
{
"type": "text", "x": 0, "y": 0, "width": 24, "height": 2,
"properties": { "markdown": "# Web tier\nRunbook: https://example.com/runbooks/web" }
},
{
"type": "metric", "x": 0, "y": 2, "width": 12, "height": 6,
"properties": {
"title": "CPU utilization",
"region": "us-east-1",
"view": "timeSeries",
"stacked": false,
"period": 300,
"stat": "Average",
"metrics": [
["AWS/EC2", "CPUUtilization", "InstanceId", "i-0123456789abcdef0"],
["...", { "stat": "Maximum", "label": "Max CPU" }]
],
"yAxis": { "left": { "min": 0, "max": 100 } },
"annotations": { "horizontal": [ { "label": "Alarm threshold", "value": 80 } ] }
}
},
{
"type": "metric", "x": 12, "y": 2, "width": 12, "height": 6,
"properties": {
"title": "Memory used (%)",
"region": "us-east-1",
"view": "timeSeries",
"period": 60,
"stat": "Average",
"metrics": [
["CWAgent", "mem_used_percent", "InstanceId", "i-0123456789abcdef0"]
]
}
},
{
"type": "metric", "x": 0, "y": 8, "width": 12, "height": 6,
"properties": {
"title": "Application errors",
"region": "us-east-1",
"view": "bar",
"period": 300,
"stat": "Sum",
"metrics": [ ["MyApp", "ErrorCount"] ]
}
},
{
"type": "alarm", "x": 12, "y": 8, "width": 12, "height": 6,
"properties": {
"title": "Alarm status",
"alarms": ["arn:aws:cloudwatch:us-east-1:111122223333:alarm:high-cpu-web"]
}
},
{
"type": "log", "x": 0, "y": 14, "width": 24, "height": 6,
"properties": {
"title": "Busiest error periods",
"region": "us-east-1",
"view": "table",
"query": "SOURCE '/app/web' | fields @timestamp, @message | filter @message like /ERROR/ | stats count() as errors by bin(5m) | sort errors desc | limit 20"
}
}
]
}Create the dashboard and check it:
aws cloudwatch put-dashboard \
--dashboard-name web-overview \
--dashboard-body file://dashboard.json
aws cloudwatch get-dashboard --dashboard-name web-overviewFour details in the JSON matter:
"..."in a metric row means "reuse the values from the previous row". The second CPU line therefore draws the maximum of the same metric without repeating its namespace, name, and dimensions.- The
annotationsblock draws a horizontal line at 80, which matches the alarm threshold from the alarm chapter. Viewers can see how close the metric is to alarming. - The
ErrorCountmetric has no dimensions, because the metric filter you created publishes it without any. A metric row must list the same dimensions the metric was published with, or the widget stays empty. - The log widget's
SOURCEclause names the log group. Without it the widget has nothing to query.
Updating and managing dashboards
put-dashboard creates the dashboard if the name is new and replaces it entirely if the name exists. There is no partial update. To edit, run get-dashboard, change the JSON in DashboardBody, and put it back. If the body is invalid, the response contains DashboardValidationMessages that name the failing widget. A valid body returns an empty list.
Names may contain letters, numbers, dashes, and underscores. Other management commands:
aws cloudwatch list-dashboards
aws cloudwatch delete-dashboards --dashboard-names web-overviewKeep dashboard.json in version control and deploy it with put-dashboard, or define it in CloudFormation as an AWS::CloudWatch::Dashboard resource. Edits made in the console are lost the next time the stored file is deployed. Choose one source of truth.
Metric math
A metric widget can compute new series from existing ones. Give each source metric an id, then add an expression row. This row calculates an error percentage from two metrics:
"metrics": [
["MyApp", "ErrorCount", { "id": "errs", "visible": false }],
["MyApp", "RequestCount", { "id": "reqs", "visible": false }],
[{ "expression": "100 * errs / reqs", "label": "Error rate (%)", "id": "rate" }]
]Setting visible to false hides the inputs so only the result is drawn. The RequestCount metric here is hypothetical, so you would need to publish it first.
Design guidelines
- Put alarm and summary widgets at the top, and put detail below. Someone who opens the page during an incident should see whether anything is wrong before they scroll.
- Give every widget a
title, and setyAxislimits for percentages so a flat line is not stretched to look like a spike. - Keep one dashboard per audience or service. A page with fifty widgets is slow to load and hard to read.
- Match the
periodto the metric's resolution. A 60-second period over a metric published every 5 minutes produces gaps.
Time range and refresh
Widgets use the dashboard's time range, which the viewer sets at the top of the page: relative ranges such as the last 3 hours, or absolute ones. The put-dashboard body can also set start (for example "-PT3H") and periodOverride at the top level, next to widgets. A widget can override the range with its own start and end. Auto-refresh intervals (10 seconds up to 15 minutes) are chosen in the console. More frequent refresh means more queries, which matters for cost and for log widgets.
Exercise
You deployed a dashboard whose "Application errors" widget is empty, although the ErrorCount metric appears in the Metrics console. The widget row is ["MyApp", "ErrorCount", "Service", "checkout"]. The metric filter from the logs chapter created the metric. What is the most likely cause, and how do you fix it?
Show answer
The metric filter publishes ErrorCount with no dimensions, but the widget asks for a metric with the dimension Service=checkout. CloudWatch treats these as two different metrics, so the second one has no data. Change the row to ["MyApp", "ErrorCount"], then run put-dashboard again with the corrected body.
Exercise
A teammate edits the dashboard in the console and a day later a deployment pipeline runs put-dashboard with the file from version control. What happens to the teammate's edit, and how do you prevent this?
Show answer
The edit is lost, because put-dashboard replaces the whole dashboard body. To prevent it, treat the stored JSON as the only source of truth: make changes in the file (or the CloudFormation template) and deploy them, or copy the console's updated body back into version control before the next deployment.