AWS CloudWatch Tutorial


1. Creating and Managing CloudWatch Logs

Example 1: Create a Log Group

aws logs create-log-group --log-group-name MyAppLogs

Explanation

  1. Creates a new CloudWatch log group to organize logs.

    • aws logs create-log-group
  2. The log group name must be unique within the AWS account.

    • --log-group-name MyAppLogs
  3. CloudWatch uses log groups to manage retention and access policies.

    • create-log-group
  4. A log group is required before adding log streams.

    • create-log-group

Example 2: Create a Log Stream

aws logs create-log-stream --log-group-name MyAppLogs --log-stream-name MyInstanceLogs

Explanation

  1. Creates a log stream inside a log group for specific logs.

    • aws logs create-log-stream
  2. Log streams store sequential log events from applications or services.

    • --log-stream-name MyInstanceLogs
  3. Used to separate logs from different instances or services.

    • create-log-stream
  4. A log stream must exist before sending logs to it.

    • create-log-stream

Example 3: Send Logs to CloudWatch

aws logs put-log-events --log-group-name MyAppLogs --log-stream-name MyInstanceLogs --log-events '[{"timestamp": 1700000000000, "message": "App started successfully"}]'

Explanation

  1. Writes log events to a CloudWatch log stream.

    • aws logs put-log-events
  2. Each log event requires a timestamp and message.

    • {"timestamp": 1700000000000, "message": "App started successfully"}
  3. Timestamps must be in Unix epoch milliseconds format.

    • "timestamp": 1700000000000
  4. Ensures logs are indexed and searchable in CloudWatch.

    • put-log-events

Example 4: Retrieve Logs from CloudWatch

aws logs get-log-events --log-group-name MyAppLogs --log-stream-name MyInstanceLogs --limit 10

Explanation

  1. Fetches log events from a specific log stream.

    • aws logs get-log-events
  2. Limits the number of returned logs to 10.

    • --limit 10
  3. Useful for debugging or monitoring application health.

    • get-log-events
  4. Can be integrated with automation tools for alerting.

    • get-log-events

2. Monitoring AWS Services with CloudWatch Metrics

Example 1: List Available Metrics

aws cloudwatch list-metrics --namespace AWS/EC2

Explanation

  1. Lists all CloudWatch metrics for an AWS service.

    • aws cloudwatch list-metrics
  2. The namespace AWS/EC2 returns EC2 instance metrics.

    • --namespace AWS/EC2
  3. Useful for identifying available monitoring parameters.

    • list-metrics
  4. Custom namespaces can be used for application-specific metrics.

    • list-metrics

Example 2: Get CPU Utilization for an EC2 Instance

aws cloudwatch get-metric-statistics --namespace AWS/EC2 --metric-name CPUUtilization --dimensions Name=InstanceId,Value=i-1234567890abcdef0 --start-time 2024-02-10T00:00:00Z --end-time 2024-02-10T12:00:00Z --period 3600 --statistics Average

Explanation

  1. Retrieves CPU utilization statistics for an EC2 instance.

    • aws cloudwatch get-metric-statistics
  2. Filters by instance ID (i-1234567890abcdef0).

    • --dimensions Name=InstanceId,Value=i-1234567890abcdef0
  3. Fetches data within a specific time range.

    • --start-time 2024-02-10T00:00:00Z --end-time 2024-02-10T12:00:00Z
  4. Returns averaged statistics over 1-hour periods.

    • --period 3600 --statistics Average

Example 3: Create a Custom Metric

aws cloudwatch put-metric-data --namespace "MyAppMetrics" --metric-name "RequestCount" --value 5 --unit Count

Explanation

  1. Sends custom application metrics to CloudWatch.

    • aws cloudwatch put-metric-data
  2. Uses a custom namespace (MyAppMetrics).

    • --namespace "MyAppMetrics"
  3. Tracks an application-specific metric (RequestCount).

    • --metric-name "RequestCount" --value 5
  4. Metric unit defines how data is measured (Count).

    • --unit Count

Example 4: Set Alarm on CPU Usage

aws cloudwatch put-metric-alarm --alarm-name HighCPUUsage --metric-name CPUUtilization --namespace AWS/EC2 --dimensions Name=InstanceId,Value=i-1234567890abcdef0 --statistic Average --period 300 --threshold 80 --comparison-operator GreaterThanThreshold --evaluation-periods 2 --alarm-actions arn:aws:sns:us-east-1:123456789012:MySNSTopic

Explanation

  1. Creates an alarm to monitor high CPU usage.

    • aws cloudwatch put-metric-alarm
  2. Triggers when CPU utilization exceeds 80%.

    • --threshold 80 --comparison-operator GreaterThanThreshold
  3. Evaluates metric over 5-minute intervals.

    • --period 300 --evaluation-periods 2
  4. Sends notifications to an SNS topic.

    • --alarm-actions arn:aws:sns:us-east-1:123456789012:MySNSTopic

3. CloudWatch Logs Insights (Log Analysis & Querying)

Example 1: Query Logs for Errors

aws logs start-query --log-group-name MyAppLogs --query-string 'fields @timestamp, @message | filter @message like "ERROR"' --start-time 1700000000000 --end-time 1700000500000

Explanation

  1. Executes a query in CloudWatch Logs Insights.

    • aws logs start-query
  2. Filters logs containing the word ERROR.

    • filter @message like "ERROR"
  3. Defines a time range for the query.

    • --start-time 1700000000000 --end-time 1700000500000
  4. Useful for debugging application failures.

    • start-query

Example 2: List Active Queries

aws logs describe-queries --log-group-name MyAppLogs

Explanation

  1. Shows running and completed log queries.

    • aws logs describe-queries
  2. Helps track long-running queries in CloudWatch Logs Insights.

    • describe-queries
  3. Provides query status and execution time.

    • describe-queries
  4. Useful for monitoring and optimizing log analysis.

    • describe-queries

Example 3: Cancel a Running Query

aws logs stop-query --query-id "query-12345678"

Explanation

  1. Stops an active CloudWatch Logs Insights query.

    • aws logs stop-query
  2. Requires the query ID to terminate execution.

    • --query-id "query-12345678"
  3. Prevents long-running queries from consuming resources.

    • stop-query
  4. Useful for troubleshooting and query optimization.

    • stop-query

Example 4: Get Query Results

aws logs get-query-results --query-id "query-12345678"

Explanation

  1. Retrieves results from a completed query.

    • aws logs get-query-results
  2. Requires a query ID for fetching data.

    • --query-id "query-12345678"
  3. Returns log data in a structured format.

    • get-query-results
  4. Useful for analyzing application errors and performance.

    • get-query-results