Ravindra BagaleCourses & study guides मराठी Track your progress

How to Create a CloudWatch Dashboard for Multiple EC2 Instances

You now have metrics (Amazon CloudWatch Explained), health alarms (EC2 and EBS Health Check Alarms) and logs (How to Send Logs from Multiple EC2 Instances to CloudWatch Logs). A dashboard puts all of them on one screen, so anyone can see the health of the whole app in five seconds.

What you'll learn in this chapter

  • What a dashboard and a widget are
  • Line, number, gauge, alarm status, text and logs widgets
  • CPU of many instances on one graph, and metric math
  • Build the same dashboard with the CLI (JSON)
  • Share a dashboard safely, and what it costs

1. A story: the war room on sale night

On sale night the team sits in one room with a big TV. The manager asks every few minutes: "Are we fine?" Nobody wants to open 10 console pages. The TV shows one dashboard: CPU of all web servers, requests per minute, 5xx errors, the alarm lights and the latest error logs. One look answers the question.

What feeds a dashboardEC2, EBS, ALB metricsAgent metricsmemory, diskAlarmsOK / ALARMLogs Insights queriesOne dashboardall on one screen
Every source flows down into one screen.

2. Dashboards and widgets

Why do we need this?

Graphs in different places are slow to compare. When CPU jumps, you want the request count and the error logs right next to it.

What is it?

A dashboard is a page you design. A widget is one box on it.

WidgetShowsGood for
LineValues over timeCPU of 3 servers
Stacked areaParts that add upRequests per target
NumberLatest single valueHealthy hosts = 3
GaugeValue against a rangeDisk used 0-100%
Alarm statusGreen/red lights for chosen alarmsAll health alarms
Logs tableA Logs Insights query resultLatest 5xx lines
TextMarkdown notes and links"Who to call", runbook link

3. Lab: build an app dashboard in the console

You need 2-3 running instances (tag Role=web), the alarms from the earlier chapters and the /app/nginx/access log group. With fewer pieces, build only the widgets you can.

Part A: create the dashboard

  1. Open CloudWatch → Dashboards → Create dashboard.
  2. Name: shop-prod. Why a clear name: people open it under stress; the name must say which app and which environment.
  3. Click Create dashboard. The "Add widget" window opens.

Part B: CPU of all web servers on one graph

  1. Choose Line → Metrics → Next.
  2. Click EC2 → Per-Instance Metrics. In the search box type CPUUtilization.
  3. Tick CPUUtilization for each web instance. Each becomes one coloured line.
  4. Open Graphed metrics. Set Statistic Average, Period 1 minute if detailed monitoring is on, else 5 minutes.
  5. Change each Label from the ID to web1, web2, web3. Why: IDs mean nothing to people at 2 AM.
  6. Options tab: add a horizontal annotation at 80 labelled alarm line. Why: you see how close you are to the alarm.
  7. Click Create widget. Title it Web CPU % (click the title to edit).

Faster for many servers: instead of ticking each one, use a search expression in the Source tab. New Auto Scaling servers appear by themselves:

SEARCH('{AWS/EC2,InstanceId} MetricName="CPUUtilization"', 'Average', 300)

Part C: number, metric math and memory

  1. + Add widget → Number → Metrics. Pick the ALB metric RequestCount (ApplicationELB → Per AppELB Metrics), statistic Sum, 1 minute. Title Requests / min.
  2. Add a line widget with HTTPCode_Target_5XX_Count (m1, Sum) and RequestCount (m2, Sum). Click Add math → Start with empty expression: 100*m1/m2, label 5xx %. Untick m1 and m2. Why: "12 errors" means little; "0.4% of requests failed" is clear.
  3. Add a Gauge on CWAgent → InstanceId → mem_used_percent for web1, range 0-100. Title web1 memory %.

Part D: alarms and logs on the same screen

  1. + Add widget → Alarm status. Tick web1-cpu-high, web1-system-recover, web1-instance-reboot, web1-disk-queue. Create.
  2. + Add widget → Logs table. Log group /app/nginx/access. Query:
fields @timestamp, @logStream, @message
| filter @message like / 50[0-9] /
| sort @timestamp desc
| limit 20
  1. Click Create widget. Title Latest 5xx lines.
  2. + Add widget → Text: **On call:** Ops team · Runbook: link to your wiki. Why: the person who sees red knows what to do next.
  3. Drag widgets so the most important ones (alarm status, 5xx %) are at the top. Click Save. Why save: unsaved changes are lost when you leave the page.
  4. Top right: time range 3h, refresh 1 minute, and turn on Auto refresh for the TV.
  5. Verify: run stress-ng --cpu 0 --timeout 300s on web1; its line climbs while the others stay flat.

4. The same dashboard with the CLI

Why do we need this?

A dashboard made by clicking exists in one account only. As JSON you can keep it in Git, review it and create it again in a new account or region in seconds.

How to do it (step by step)

  1. Open your dashboard → Actions → View/edit source to see its JSON. Save a shorter version as dash.json:
{
  "widgets": [
    { "type": "metric", "x": 0, "y": 0, "width": 12, "height": 6,
      "properties": { "title": "Web CPU %", "region": "ap-south-1", "view": "timeSeries",
        "stat": "Average", "period": 300,
        "metrics": [
          ["AWS/EC2", "CPUUtilization", "InstanceId", "i-0aaa1111111111111", {"label": "web1"}],
          ["...", "i-0bbb2222222222222", {"label": "web2"}]
        ],
        "annotations": { "horizontal": [ { "value": 80, "label": "alarm line" } ] } } },
    { "type": "log", "x": 12, "y": 0, "width": 12, "height": 6,
      "properties": { "title": "Latest 5xx lines", "region": "ap-south-1", "view": "table",
        "query": "SOURCE '/app/nginx/access' | fields @timestamp, @logStream, @message | filter @message like / 50[0-9] / | sort @timestamp desc | limit 20" } },
    { "type": "text", "x": 0, "y": 6, "width": 24, "height": 2,
      "properties": { "markdown": "**On call:** Ops team" } }
  ]
}
  1. Create or update it:
aws cloudwatch put-dashboard --dashboard-name shop-prod-cli --dashboard-body file://dash.json
# expected: {"DashboardValidationMessages": []}
aws cloudwatch list-dashboards --query 'DashboardEntries[].DashboardName'
# expected: ["shop-prod", "shop-prod-cli"]

"..." in the metrics array means "same as the line above", so you only write what changes (the instance ID).

5. Sharing a dashboard

Why do we need this?

A manager or a client wants to see the dashboard but should not get AWS console access. Giving console access to see graphs is too much power.

What is it?

WayWho can open itUse when
IAM users/roles in your accountPeople with CloudWatch read permissionYour own team
Share with specific email addressesOnly those people, after they set a passwordManagers, clients
Share publiclyAnyone with the linkOnly non-sensitive data, for example a status TV
Cross-account observabilityA central monitoring accountMany AWS accounts in one company

How to do it (step by step)

  1. Open the dashboard → Actions → Share dashboard.
  2. Choose Share your dashboard and require a username and password. Enter the email addresses.
  3. Click Prepare for sharing and confirm. AWS creates the needed role and an Amazon Cognito user pool behind the scenes.
  4. Verify: each person gets an email with a link and sets a password; they see the dashboard read-only, with no console menu.
  5. To stop sharing: Actions → Share dashboard → Stop sharing.

Ravindra Bagale's Tip

A shared dashboard shows logs widgets too. Do not put log lines with customer emails, phone numbers or tokens on a dashboard you share outside the team.

Cost: the free tier covers 3 dashboards with up to 50 metrics each per month. After that each dashboard is billed per month (about USD 3 at the time of writing; check the CloudWatch pricing page). Logs widgets also bill for the data each query scans.

6. Dashboard ideas per stack

StackWidgets to put on top
nginx / Apache + ALBRequestCount, 5xx %, TargetResponseTime p90, HealthyHostCount
Java Spring / .NETCPU, mem_used_percent per instance, GC or error log query
Node.js (PM2) / Python / GoCPU per instance, 5xx log query, response time
MySQL / RDSCPUUtilization, DatabaseConnections, FreeStorageSpace, ReadLatency
PHP / WordPressCPU, php-fpm error log table, disk used gauge

7. Cleanup

  1. Stop sharing if you shared it.
  2. CloudWatch → Dashboards → tick shop-prod and shop-prod-cli → Delete.
  3. CLI: aws cloudwatch delete-dashboards --dashboard-names shop-prod shop-prod-cli.
  4. Delete the alarms, log groups and instances from the earlier labs if you no longer need them.

8. Interview questions

Q. How do you show the CPU of all Auto Scaling instances on one graph without editing it each time?

Use a SEARCH expression on AWS/EC2 CPUUtilization (or the AutoScalingGroupName dimension), so new instances appear automatically.

Q. How do you share a dashboard with someone who has no AWS login?

Share dashboard with specific email addresses (username and password via Cognito) or publicly for non-sensitive data.

Q. Why keep dashboards as JSON?

To store them in Git, review changes and recreate them with put-dashboard in any account or region.

Try at home

  1. Build shop-prod with at least 5 widgets: CPU of all servers, requests, 5xx %, alarm status and latest 5xx log lines.
  2. Export it with View/edit source, delete it, and recreate it with put-dashboard.

In short

  1. A dashboard is one screen of widgets: metrics, alarms, logs and notes.
  2. Put many instances on one line graph; use SEARCH for Auto Scaling.
  3. Metric math turns raw counts into clear numbers like 5xx %.
  4. Keep the JSON in Git; create with put-dashboard.
  5. Share by email for managers and clients; public only for non-sensitive data.

Practice Tasks

About this scenario

Aurangabad Pharma is a pharmaceuticals business in Chhatrapati Sambhajinagar. Dr. Farhan Shaikh looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 10

Easy List metrics in a namespace

Why: See which metrics AWS already collects for a service.

What to do: List the metrics in the AWS/EC2 namespace.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy Reboot an instance

Why: A reboot keeps the same disks and IPs, unlike stop/start which may change the public IP.

What to do: Reboot the instance i-01a40525502111a2b.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy Stop an instance

Why: A stopped instance does not charge for compute, only for its disks.

What to do: Stop the instance i-01a40525502111a2b.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy How many addresses in a /16?

Why: Picking a VPC size needs this count.

What to do: Aurangabad Pharma plans the VPC 10.138.0.0/16. How many IP addresses does it hold in total?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy Port for SSH

Why: Security group rules need the right port.

What to do: Which TCP port must the security group open for SSH?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Publish a custom metric

Why: Custom metrics let you graph and alarm on business numbers, like orders per minute.

What to do: Publish metric OrdersPlaced with value 6 in namespace AurangabadPharma/Shop.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Check instance status checks

Why: System and instance status checks tell you if AWS hardware or your OS has a problem.

What to do: Show the status checks of instance i-01a40525502111a2b.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Check target health

Why: If targets are unhealthy the site shows 502/503 errors.

What to do: Show the health of the targets in arn:aws:elasticloadbalancing:ap-south-1:112055250461:targetgroup/abadpharma-tg/6d0ecf831eec9f09.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Create a dashboard from a file

Why: A dashboard JSON file can be kept in Git and re-created any time.

What to do: Create or update dashboard abadpharma-ops from dashboard.json.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Create a CPU alarm

Why: An alarm watches a metric and notifies you before users notice a problem.

What to do: Create alarm abadpharma-cpu-high: AWS/EC2 CPUUtilization, Average over 300 seconds, 1 evaluation period, GreaterThanThreshold 80, for InstanceId=i-01a40525502111a2b, notify arn:aws:sns:ap-south-1:112055250461:abadpharma-alerts.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Revision card: CloudWatch dashboard and widgets. On sale night nobody wants to open 10 console pages to ask "are we fine?". A dashboard is one page you design. A widget is one box: line graph, number, gauge, alarm status, logs table, text. CloudWatch › Dashboards › Create → Add widget → choose type → Save. shop-prod: web CPU line, Requests/min number, 5xx %, alarm lights, latest 5xx logs.
Revision card: CloudWatch dashboard and widgets · Download PNG
Revision card: Dashboard metric math. "12 errors" means little without knowing the traffic. Metric math makes a new line from other metrics with a formula. m1 = HTTPCode_Target_5XX_Count (Sum), m2 = RequestCount (Sum), Add math → expression. 12 errors in 3,000 requests → 0.4% of requests failed.
Revision card: Dashboard metric math · Download PNG
Revision card: SEARCH for many instances. Auto Scaling adds and removes servers. Ticking each one on a graph never keeps up. A SEARCH expression finds every metric that matches, each time the graph loads. In the graph Source tab, write the SEARCH with namespace, metric, statistic and period. 3 servers at 9 PM, 6 at 10 PM: all 6 lines appear by themselves.
Revision card: SEARCH for many instances · Download PNG
Revision card: How to share a CloudWatch dashboard. A manager or client should see graphs without getting AWS console access. Share by email (user sets a password, read-only) or publicly (anyone with the link). Dashboard › Actions › Share dashboard › username and password › add emails › Prepare for sharing. Client gets a link, sets a password and sees shop-prod with no console menu. Public only for non-sensitive data.
Revision card: How to share a CloudWatch dashboard · Download PNG
Comparison: Dashboard vs Alarm. Dashboard: A page of graphs and widgets you design. Alarm: A rule that watches one metric and acts.
Comparison: Dashboard vs Alarm · Download PNG
Comparison: Share by email vs Share publicly. Share by email: Dashboard shared with chosen email addresses. Share publicly: Dashboard open to anyone with the link.
Comparison: Share by email vs Share publicly · Download PNG
Comparison: Ticked metrics vs SEARCH expression. Ticked metrics: Each instance metric picked by hand on the graph. SEARCH expression: A query that finds all matching metrics each time.
Comparison: Ticked metrics vs SEARCH expression · Download PNG
Interview card: How do you show CPU of all Auto Scaling instances on one graph?. Use a SEARCH expression on AWS/EC2 CPUUtilization. Instances come and go; a fixed list goes out of date in an hour. Source tab: SEARCH with namespace, metric, statistic Average, period 300.
Interview card: How do you show CPU of all Auto Scaling instances on one graph? · Download PNG
Interview card: How do you share a CloudWatch dashboard with someone who has no AWS login?. Share with their email address. They set a password and get read-only access. Console access only to see graphs would give far too much power. Actions › Share dashboard › username and password. Public link only for non-sensitive data.
Interview card: How do you share a CloudWatch dashboard with someone who has no AWS login? · Download PNG
Interview card: Why keep CloudWatch dashboards as JSON?. The JSON describes every widget; put-dashboard creates the dashboard from it. Clicked dashboards live in one account and are easy to lose. Save dash.json in Git, then recreate in any account or Region in seconds.
Interview card: Why keep CloudWatch dashboards as JSON? · Download PNG
Interview card: How do you show the 5xx error rate as a percentage on a dashboard?. Metric math: 100 times 5xx count divided by request count. A raw count rises with traffic; a percentage shows real health. m1 = HTTPCode_Target_5XX_Count Sum, m2 = RequestCount Sum. 12 / 3,000 → 0.4%.
Interview card: How do you show the 5xx error rate as a percentage on a dashboard? · Download PNG

More cards: Ravi Sir's Cards.