🌐 Introduction
While metrics provide a snapshot of system health, logs
offer the full narrative. Logs are the breadcrumbs that help developers and
DevOps teams debug issues, trace user activity, monitor APIs, and detect
security threats.
In this chapter, we’ll walk through the complete
lifecycle of logs in AWS CloudWatch, from ingestion to analysis and
alerting using CloudWatch Logs and Logs Insights.
By the end of this chapter, you’ll be able to:
📦 What Are CloudWatch
Logs?
CloudWatch Logs is a fully managed log storage and analytics
service that lets you:
📂 Core Concepts
|
Concept |
Description |
|
Log Group |
Logical container for
related logs (e.g., /aws/lambda/apiFn) |
|
Log Stream |
Sequence of
events from a single source (e.g., instance or container) |
|
Event |
Individual log entry
with timestamp and message |
|
Retention |
Duration logs
are kept (1 day to indefinite) |
🛠️ Section 1: Collecting
Logs from AWS Services
✅ 1. Lambda Logs
✅ 2. EC2 Logs with CloudWatch
Agent
Install the CloudWatch Agent on your instance and
configure it to send logs.
Sample Agent Configuration
(amazon-cloudwatch-agent.json):
json
{
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path":
"/var/log/nginx/access.log",
"log_group_name":
"/app/nginx/access",
"log_stream_name":
"{instance_id}"
}
]
}
}
}
}
Start Agent:
bash
sudo
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \
-a start \
-m ec2 \
-c file:/path/to/amazon-cloudwatch-agent.json
\
-s
✅ 3. ECS, EKS, and Fargate
✅ 4. API Gateway Logs
📁 Section 2: Retention
and Cost Optimization
|
Retention Period |
Use Case |
Cost Implication |
|
1–7 days |
Short-term debugging |
Low |
|
30–90 days |
Audit trails,
historical analysis |
Medium |
|
Infinite |
Legal compliance, SIEM
storage |
Higher unless exported
to S3 |
✅ Setting Retention Policy
bash
aws
logs put-retention-policy \
--log-group-name
"/app/nginx/access" \
--retention-in-days 30
🧠 Section 3: Using
CloudWatch Logs Insights
CloudWatch Logs Insights provides a powerful SQL-like
interface to query, aggregate, and visualize logs in real-time.
🔍 Syntax Basics
|
Command |
Description |
|
fields |
Specify which fields
to return |
|
filter |
Filter log
lines by pattern or content |
|
sort |
Order results by field |
|
stats |
Perform
aggregations (avg, count, sum) |
|
parse |
Extract fields from
raw logs using patterns |
🛠️ Example Queries
1. Get Recent Errors from Lambda
sql
fields
@timestamp, @message
|
filter @message like /ERROR/
|
sort @timestamp desc
|
limit 20
2. Count 5XX API Gateway Errors by Path
sql
filter
status >= 500
|
stats count(*) by httpPath
3. Parse JSON Logs
sql
parse
@message "* requestId=* status=* latency=*" as reqId, status, latency
|
stats avg(latency) by status
📈 Section 4: Metric
Filters from Logs
You can convert specific log patterns into CloudWatch
metrics.
✅ Example: Track 404 Errors from
Nginx Logs
Step 1: Create Filter
bash
aws
logs put-metric-filter \
--log-group-name
"/app/nginx/access" \
--filter-name "NotFound404" \
--filter-pattern '" 404 "' \
--metric-transformations \
metricName=PageNotFound \
,metricNamespace=WebApp \
,metricValue=1
Step 2: Create Alarm on PageNotFound metric.
Now you can trigger alerts or visualize the metric like any
other.
📊 Section 5: Exporting
Logs for Analysis
If you need long-term analysis or integrations with SIEM
tools like Splunk or ElasticSearch, export logs to S3.
✅ Export Logs to S3
bash
aws
logs create-export-task \
--log-group-name
"/app/nginx/access" \
--from 1690000000000 \
--to 1690099999999 \
--destination "my-log-archive" \
--destination-prefix "nginx-logs/"
Time range values (--from, --to) are in epoch
milliseconds.
📌 Section 6: Best
Practices for CloudWatch Log Management
🧾 Summary
CloudWatch Logs and Logs Insights provide a robust framework
for end-to-end log observability in AWS.
With the ability to collect logs from any AWS service, VM,
container, or application — and analyze them instantly with interactive queries
— CloudWatch logs are a must-have tool in the modern DevOps stack.
Whether you’re debugging Lambda functions, monitoring API
errors, or tracking audit trails, CloudWatch Logs enables real-time and
historical insights with minimal setup.
Next, we’ll explore automating incident response and
observability workflows using EventBridge and Lambda.
Answer:
Amazon CloudWatch is AWS’s native monitoring and observability service. It
collects and tracks metrics, logs, events, and alarms from AWS resources,
applications, and on-premises servers. It’s used to detect anomalies, automate
responses, and provide visibility into system health.
Answer:
Yes. You can use CloudWatch Agent, CloudWatch Logs, and custom
metrics APIs to monitor on-prem servers or third-party cloud services by
pushing metrics manually or via integration tools.
Answer:
Answer:
CloudWatch uses Alarms to monitor metric thresholds. When thresholds are
breached, it can send notifications via Amazon SNS, trigger AWS
Lambda functions, or initiate Auto Scaling actions.
Answer:
CloudWatch Logs Insights is an interactive log analytics tool. It allows you to
run SQL-like queries on log data, visualize patterns, and troubleshoot
faster across Lambda, ECS, API Gateway, and more.
Answer:
Use CloudWatch cross-account observability. It allows a central
monitoring account to access logs and metrics from linked AWS accounts using
IAM roles and linked dashboards.
Answer:
Yes. CloudWatch Dashboards offer customizable graphs, metrics widgets,
single-value widgets, and time-based views to monitor infrastructure at a
glance.
Answer:
Anomaly Detection uses machine learning to automatically model your metric
patterns and highlight unusual behavior — without you needing to set static
thresholds.
Answer:
Absolutely. CloudWatch integrates with Datadog, Splunk, Grafana,
PagerDuty, and others via APIs, Kinesis Firehose, and AWS
Lambda for extended observability and incident management.
Answer:
CloudWatch pricing depends on usage:
Tutorials are for educational purposes only, with no guarantees of comprehensiveness or error-free content; TuteeHUB disclaims liability for outcomes from reliance on the materials, recommending verification with official sources for critical applications.
Kindly log in to use this feature. We’ll take you to the login page automatically.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Comments(0)