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

AWS Cloud

AWS CLI CloudWatch Commands: Metrics, Alarms and Logs

We watch our systems: read metrics, push our own numbers, raise alarms, keep logs and search them with Logs Insights.

In this chapter

  • List and read metrics
  • Put a custom metric
  • Create an alarm
  • Manage log groups and retention
  • Tail and query logs

1. The big picture: a car dashboard

A car dashboard shows speed and fuel, and a warning light turns on when something is wrong. CloudWatch metrics are the gauges, alarms are the warning lights and logs are the trip diary.

Metrics and alarms are in aws cloudwatch. Logs are in aws logs.

Monitoring workflowlist-metrics to find namesget-metric-statisticsput-metric-alarmSNS topic gets the alertlogs tail / start-query
An alarm is only useful if it tells someone.

2. Find and read metrics

A CloudWatch metric is a set of measurements over time. The namespace groups related metrics, the metric name describes the measurement, and dimensions identify the series. In AWS/EC2, two instance IDs can publish two different CPUUtilization series. Requesting the wrong dimension can produce no matching points even when another instance has data.

Pick one instance and one time window for the read below. A period combines measurements into one reported interval. If one interval has samples of 20% and 80%, the average is 50% but the maximum is 80%. Read the timestamps, statistic and period together before deciding whether CPU was continuously high. Logs record discrete events and can help explain what the application did around the observed metric change.

You want to know how busy a server was in the last hour.

Each metric has a namespace (AWS/EC2), a name (CPUUtilization) and dimensions (InstanceId). get-metric-statistics returns numbers per period.

  1. List EC2 CPU metrics.
aws cloudwatch list-metrics --namespace AWS/EC2 --metric-name CPUUtilization --query "Metrics[].Dimensions[0].Value" --output text

Expected output:

i-0abc123def4567890
  1. Average CPU for one hour.
aws cloudwatch get-metric-statistics --namespace AWS/EC2 --metric-name CPUUtilization --dimensions Name=InstanceId,Value=i-0abc123def4567890 --start-time $(date -u -d "-1 hour" +%FT%TZ) --end-time $(date -u +%FT%TZ) --period 300 --statistics Average --query "Datapoints[].Average" --output text

Expected output:

2.1  1.9  3.4  2.2

3. Custom metrics

AWS does not know your business numbers, like orders per minute.

put-metric-data sends your own value to your own namespace. Then it works like any metric.

  1. Send a value.
aws cloudwatch put-metric-data --namespace MyApp --metric-name Orders --value 42 --unit Count

Expected output:

(no output means success)
  1. Verify it exists.
aws cloudwatch list-metrics --namespace MyApp --query "Metrics[].MetricName" --output text

Expected output:

Orders

4. Alarms

You cannot watch graphs all day. You want a message when CPU stays high.

put-metric-alarm sets a threshold, periods and an action like an SNS topic. States are OK, ALARM and INSUFFICIENT_DATA.

Set three one-minute evaluation periods and two datapoints to alarm. With all three datapoints present, CPU readings of 82%, 60% and 85% breach an 80% threshold in two periods, which meets the alarm rule even though the breaches are separated. When a reading is missing, the alarm’s missing-data setting and the other available samples affect the result. Missing telemetry should not be treated as a measured 0% CPU.

  1. Create an alarm.
aws cloudwatch put-metric-alarm --alarm-name cli-high-cpu --namespace AWS/EC2 --metric-name CPUUtilization --dimensions Name=InstanceId,Value=i-0abc123def4567890 --statistic Average --period 300 --evaluation-periods 2 --threshold 80 --comparison-operator GreaterThanThreshold --alarm-actions arn:aws:sns:ap-south-1:111122223333:cli-alerts

Expected output:

(no output means success)
  1. Test it by forcing the state.
aws cloudwatch set-alarm-state --alarm-name cli-high-cpu --state-value ALARM --state-reason "test"
aws cloudwatch describe-alarms --alarm-names cli-high-cpu --query "MetricAlarms[0].StateValue" --output text

Expected output:

ALARM

5. Logs: groups, retention, tail and Insights

Use /cli/app as the log group and s1 as a stream inside it. The group is the container that shares retention settings. The stream is a sequence of events from one producer. The sample event writes ERROR payment failed. A retention policy controls how long events are kept; it does not route events between streams.

First use the tail example with a recent time window and confirm that the marker appears. Then read the Logs Insights query in order. It displays @message, filters to lines containing ERROR, and returns at most five matching records. start-query creates a background operation, so the returned value in Q is a query ID. It is the value to pass to get-query-results until the status becomes Complete.

Read the final response without the optional status-only --query status if you want to examine matching rows. A query can be complete with no matching rows. If you expected the practice error, compare the log group, timestamps and filter text before changing the permissions or alarm. A metric alarm and a log search can both relate to errors, but the alarm evaluates measured values and the search reads event records.

Logs kept forever cost money, and you need to find one error among millions of lines.

A log group holds streams. put-retention-policy deletes old data. tail follows live. start-query runs a Logs Insights query.

  1. Create a group and set 14-day retention.
aws logs create-log-group --log-group-name /cli/app
aws logs put-retention-policy --log-group-name /cli/app --retention-in-days 14
aws logs describe-log-groups --log-group-name-prefix /cli --query "logGroups[].[logGroupName,retentionInDays]" --output text

Expected output:

/cli/app  14
  1. Write a log line.
aws logs create-log-stream --log-group-name /cli/app --log-stream-name s1
aws logs put-log-events --log-group-name /cli/app --log-stream-name s1 --log-events timestamp=$(date +%s000),message="ERROR payment failed"

Expected output:

{"nextSequenceToken": "..."}
  1. Follow and filter.
aws logs tail /cli/app --since 1h --filter-pattern ERROR

Expected output:

2026-10-24T06:00:01 s1 ERROR payment failed
  1. Run an Insights query.
Q=$(aws logs start-query --log-group-name /cli/app --start-time $(date -d "-1 hour" +%s) --end-time $(date +%s) --query-string "fields @message | filter @message like /ERROR/ | limit 5" --query queryId --output text)
sleep 3; aws logs get-query-results --query-id $Q --query status

Expected output:

"Complete"

6. Same action in Python, Node.js, Java, Go, .NET and PHP

Scripts start in the CLI. Apps call the same API through an SDK. Knowing both helps you move a tested command into real code.

Low-level CLI commands expose service operations. A high-level command or an automatically paginated list can make multiple requests. For example, aws cloudwatch put-metric-data --namespace MyApp --metric-name Orders --value 42 maps to the Put metric API.

Same action (Put metric) from code. Set the intended profile and Region for the SDK. Provider support and precedence can differ by SDK, and an explicit client setting can override defaults.

StackCode
Python (boto3)boto3.client("cloudwatch").put_metric_data(Namespace="MyApp",MetricData=[{"MetricName":"Orders","Value":42}])
Node.js (v3)await cw.send(new PutMetricDataCommand({Namespace:"MyApp",MetricData:[{MetricName:"Orders",Value:42}]}));
Java (v2)cw.putMetricData(r -> r.namespace("MyApp").metricData(MetricDatum.builder().metricName("Orders").value(42.0).build()));
Go (v2)c.PutMetricData(ctx, &cloudwatch.PutMetricDataInput{Namespace: aws.String("MyApp"), MetricData: []types.MetricDatum{{MetricName: aws.String("Orders"), Value: aws.Float64(42)}}})
.NETawait cw.PutMetricDataAsync(new PutMetricDataRequest{Namespace="MyApp",MetricData=new(){new MetricDatum{MetricName="Orders",Value=42}}});
PHP$cw->putMetricData(["Namespace"=>"MyApp","MetricData"=>[["MetricName"=>"Orders","Value"=>42]]]);

7. Clean up so nothing keeps costing

Lab resources left running cost money and can be a security risk.

Delete in reverse order: children first, then parents.

  1. Delete the alarm.
aws cloudwatch delete-alarms --alarm-names cli-high-cpu

Expected output:

(no output means success)
  1. Delete the log group.
aws logs delete-log-group --log-group-name /cli/app

Expected output:

(no output means success)
  1. Verify.
aws cloudwatch describe-alarms --alarm-names cli-high-cpu --query "MetricAlarms" --output text

Expected output:

(no output means success)
  1. Custom metrics cannot be deleted; they expire after 15 months with no data. Check what is left.
aws cloudwatch list-metrics --namespace MyApp --query "length(Metrics)"

Expected output:

1

Ravindra Bagale's Tip

get-metric-statistics needs UTC times in ISO format. Use date -u +%FT%TZ, or you get empty Datapoints.

Common errors: cause and safe next check

The alarm stays in INSUFFICIENT_DATA and someone assumes the resource is broken

INSUFFICIENT_DATA can mean the metric has not published enough points yet, or that the resource is inactive and sends no data. Inspect the metric namespace, dimensions, period, and how the alarm treats missing data before you raise thresholds.

An action was attached, but nothing happens while the alarm stays in ALARM

CloudWatch invokes most alarm actions when the alarm changes state, not on every evaluation while it remains in the new state. Auto Scaling actions are a documented exception that can continue while the alarm stays in the new state. Inspect the alarm history and the target SNS topic or action before you recreate the alarm.

Logs were sent, but the reader looks in the wrong place

Log events belong to a log stream inside a log group. Confirm the log group name and the stream name with describe commands before you change retention or create a second group.

Practical questions and full answers

What states can a CloudWatch alarm show?

Dashboards and docs describe OK when the threshold is not breached in the way the alarm defines, ALARM when it is, and INSUFFICIENT_DATA when there is not enough metric data for a firm evaluation. Read the alarm configuration and recent history to see why a state changed.

What does a metric alarm actually compare?

A metric alarm watches a single CloudWatch metric or the result of a math expression based on CloudWatch metrics. It performs one or more actions based on the value of that metric or expression relative to a threshold over a number of time periods.

How are log groups and log streams related?

A log stream is a sequence of log events that share the same source. A log group is a group of log streams that share the same retention, monitoring, and access control settings. Put events into the correct group and stream names that your app or agent already uses.

8. Interview questions and answers

Q. Namespace, metric, dimension?

Namespace groups metrics (AWS/EC2), metric is the measure, dimension filters it (InstanceId).

Q. Alarm states?

OK, ALARM and INSUFFICIENT_DATA.

Q. How to test an alarm action?

set-alarm-state to force ALARM.

Q. Why retention?

Default is never expire; retention cuts cost.

Q. What is Logs Insights?

A query language over log groups to filter, stats and sort logs.

Try at home

  1. Push a custom metric every minute for 5 minutes and draw its average.
  2. Create an alarm on it and force it into ALARM.

In short

  1. cloudwatch = metrics, alarms.
  2. logs = log groups.
  3. UTC times.
  4. set-alarm-state to test.
  5. Set retention.

Use one time window and one metric identity

Pick a fixed five-minute lab window and specify the namespace and dimension exactly. Compare observations before and after sending one known sample. Inspect alarm history when a state changes, including what happens for missing data.

Interview check Why might the same metric name look empty?

Namespace, dimensions, Region and time period identify different time series. An alarm can also enter INSUFFICIENT_DATA while it waits for usable datapoints.

Reference documentation

Practise this chapter

Interview questions and answers

Why can two dashboards disagree about how busy the same instance was?

I would compare their Region, namespace, dimensions, time window, period and statistic. A maximum can capture a brief peak while an average smooths it across the interval. Missing data and measurements of another instance should not be treated as the same series.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. Two EC2 instances publish CPUUtilization. Which field distinguishes their series?

Answer and explanation

A. The InstanceId dimension

The metric name is shared, but dimensions distinguish the two time series.

2. Samples were 20% and 80% in one interval. What is their average?

Answer and explanation

A. 50%

Average is the sum divided by the number of samples. Maximum would be 80%.

3. Two out of three present periods cross a threshold that requires 2 of 3. How should you read that condition?

Answer and explanation

B. The two breaches satisfy the rule even when they are separated

DatapointsToAlarm specifies how many evaluated periods must breach. They need not be adjacent within that evaluation window.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

About this scenario

Kolhapuri Chappal Co is a footwear business in Kolhapur. Sachin Mane 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 / 12

Easy List metrics in a namespace

See which metrics AWS already collects for a service.

List the metrics in the AWS/EC2 namespace.

  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 Create an SNS topic

A topic is a channel: publish once, every subscriber gets it.

Create topic kolhapurichappal-alerts.

  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 List alarms that are firing

In an incident, first see which alarms are in ALARM state.

Describe only the alarms whose state is ALARM.

  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

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

Reboot the instance i-0da02400dac111a2b.

  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 Publish to one topic

An alerting app only needs to publish.

Allow sns:Publish on arn:aws:sns:ap-south-1:118690042069:kolhapurichappal-alerts only.

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium Publish a custom metric

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

Publish metric OrdersPlaced with value 9 in namespace KolhapuriChappalCo/Shop.

  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 Subscribe an email

Email subscribers must click the confirm link before they receive messages.

Subscribe ops@kolhapurichappal.in by email to arn:aws:sns:ap-south-1:118690042069:kolhapurichappal-alerts.

  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

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

Show the health of the targets in arn:aws:elasticloadbalancing:ap-south-1:118690042069:targetgroup/kolhapurichappal-tg/6d0ecf831eec9f09.

  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 Let a function write logs

Without these actions, Lambda logs never appear in CloudWatch.

Allow logs:CreateLogGroup, logs:CreateLogStream, logs:PutLogEvents on arn:aws:logs:*:*:* only.

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium Kubernetes LoadBalancer Service

A Service of type LoadBalancer gets an AWS load balancer.

Write a Service of type LoadBalancer selecting app: web, port 80 to targetPort 8080.

  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.

Hard Create a CPU alarm

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

Create alarm kolhapurichappal-cpu-high: AWS/EC2 CPUUtilization, Average over 300 seconds, 1 evaluation period, GreaterThanThreshold 80, for InstanceId=i-0da02400dac111a2b, notify arn:aws:sns:ap-south-1:118690042069:kolhapurichappal-alerts.

  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 Auto-recover an instance with an alarm

A recover action moves the instance to healthy hardware when the system check fails.

Create alarm kolhapurichappal-recover on AWS/EC2 StatusCheckFailed_System, Maximum over 60 s, 2 periods, GreaterThanOrEqualToThreshold 1, InstanceId=i-0da02400dac111a2b, action arn:aws:automate:ap-south-1:ec2:recover.

  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.

CloudWatch alarms. You cannot watch graphs all day. Alarm = threshold + periods + SNS action. States: OK, ALARM, INSUFFICIENT_DATA. Test with set-alarm-state.
Revision card: CloudWatch alarms · Download PNG · Open card page
Logs tail and query. One error hides in millions of lines. Set retention; default never expires. Follow live with a filter. Logs Insights: start-query then get-query-results.
Revision card: Logs tail and query · Download PNG · Open card page
Metrics vs Logs. Numbers over time. / Text lines with time.
Comparison: Metrics vs Logs · Download PNG · Open card page
Why empty Datapoints?. Times must be UTC ISO 8601. Use date -u +%FT%TZ. Dimensions must match exactly.
Interview card: Why empty Datapoints? · Download PNG · Open card page

More cards: Ravi Sir's Cards.