Amazon CloudWatch

Three products under one name: Metrics (numbers over time, with alarms on them), Logs (text lines, searchable, forwardable) and Synthetics (scripts that pretend to be a user). Almost every "how do we know this broke?" answer on the exam is one of those three.

Ba sản phẩm dưới một cái tên: Metrics (con số theo thời gian, kèm alarm), Logs (dòng text, tìm kiếm và chuyển tiếp được) và Synthetics (script giả làm người dùng). Gần như mọi câu "làm sao biết cái này hỏng?" trong đề đều rơi vào một trong ba.

Reach for itNên dùng

  • Any AWS-native metric, alarm or auto-scaling triggerMọi metric, alarm hay trigger auto-scaling gốc AWS
  • Central place for application logs from EC2, ECS, Lambda, VPC Flow Logs…Nơi tập trung log ứng dụng từ EC2, ECS, Lambda, VPC Flow Logs…
  • "Alert me when ERROR appears N times" → metric filter + alarm"Báo tôi khi ERROR xuất hiện N lần" → metric filter + alarm
  • Catch an outage before customers do → Synthetics canaryBắt sự cố trước khi khách hàng thấy → Synthetics canary

Not the right toolKhông hợp

  • Who called which API → CloudTrail (CloudWatch stores the metrics, not the audit trail)Ai gọi API nào → CloudTrail (CloudWatch giữ metric, không phải audit trail)
  • Distributed request tracing across services → X-RayTrace request xuyên nhiều service → X-Ray
  • Config drift / "is this resource compliant?" → AWS ConfigTrôi cấu hình / "resource này có tuân thủ không?" → AWS Config
  • Long-term log analytics at scale → forward to S3 + Athena or OpenSearchPhân tích log dài hạn quy mô lớn → đẩy sang S3 + Athena hoặc OpenSearch

1 · Metrics1 · Metrics

Metric resolution — EC2 standard monitoring publishes every 5 minutes, detailed monitoring every minute, and high-resolution custom metrics every second one data point every… EC2 standard 5 minutes · free, on by default EC2 detailed 1 minute · detailed monitoring, paid high resolution 1 second · custom metrics only (too dense to draw) same 15-minute window in all three rows

The resolution decides how fast an alarm can possibly react. A 5-minute metric cannot notice a 90-second outage.

Độ phân giải quyết định alarm phản ứng nhanh được tới đâu. Metric 5 phút không thể thấy một sự cố kéo dài 90 giây.

  • Metrics are provided by many AWS services out of the box — you don't install anything to get them.Nhiều dịch vụ AWS cung cấp metric sẵn — không phải cài gì để có.
  • EC2 standard: 5 minutes. Detailed monitoring: 1 minute (extra cost). Detailed monitoring is what makes fast Auto Scaling reactions possible.EC2 standard: 5 phút. Detailed monitoring: 1 phút (tính thêm tiền). Detailed monitoring là thứ cho phép Auto Scaling phản ứng nhanh.
  • EC2 RAM is not a built-in metric. The hypervisor can see CPU, network and disk from outside the instance; memory is only visible from inside, so it takes the CloudWatch agent. Same for disk-space-used and per-process metrics.RAM của EC2 không phải metric có sẵn. Hypervisor nhìn được CPU, network và disk từ bên ngoài; còn memory chỉ thấy được từ bên trong, nên phải có CloudWatch agent. Disk-space-used và metric theo process cũng vậy.
  • Custom metrics: standard resolution = 1 minute, high resolution = 1 second. High resolution costs more and alarms on it can fire every 10 or 30 seconds.Custom metric: standard resolution = 1 phút, high resolution = 1 giây. High resolution đắt hơn và alarm trên nó có thể bắn mỗi 10 hoặc 30 giây.

Why memory isn't in the listVì sao memory không có trong danh sách

A hypervisor is the software that carves one physical server into several virtual machines and runs them side by side. On current EC2 that's Nitro. It is the thing that performs vCPU scheduling, network packets and EBS I/O on your behalf — so it can count them without ever looking inside your instance. Memory is different: the hypervisor hands your instance a block of RAM at boot and never touches it again. Hypervisor là phần mềm chia một server vật lý thành nhiều máy ảo và chạy chúng song song. Trên EC2 hiện tại đó là Nitro. Nó chính là thứ thực hiện việc xếp lịch vCPU, chuyển gói mạng và gửi I/O xuống EBS thay cho bạn — nên nó đếm được những thứ đó mà không cần nhìn vào trong instance. Memory thì khác: hypervisor giao cho instance một khối RAM lúc khởi động rồi không đụng tới nữa.

The hypervisor performs CPU scheduling, networking and EBS I/O so those are measurable from outside the instance; memory and disk usage are guest-kernel bookkeeping and need an agent inside EC2 instance · guest OS app processes page cache free pages the guest kernel is the only thing that knows which box a given page belongs to CloudWatch agent virtualization boundary Nitro hypervisor + Nitro cards schedules vCPUs · moves packets · issues EBS I/O it does the work, so it can count the work only readable from inside mem_used_percent disk_used_percent swap_used_percent procstat (per process) published as custom metrics measurable from outside — built in CPUUtilization NetworkIn · NetworkOut EBS ops · status checks

The dividing line is who performs the operation. Cycles, packets and disk I/O pass through the hypervisor; a page of RAM being "used" or "free" is a label that exists only in the guest kernel's own tables.

Ranh giới nằm ở ai là người thực hiện thao tác. Chu kỳ CPU, gói tin và I/O đĩa đều đi qua hypervisor; còn một trang RAM là "đang dùng" hay "trống" chỉ là cái nhãn nằm trong bảng ghi của kernel bên trong máy ảo.

  • To the hypervisor, a page your application freed a second ago looks byte-for-byte identical to one still in use — nothing physical changes when memory is released. Only the guest kernel's own tables say otherwise.Với hypervisor, một trang mà ứng dụng vừa giải phóng trông giống hệt từng byte so với trang đang dùng — giải phóng bộ nhớ không làm thay đổi gì về mặt vật lý. Chỉ bảng ghi của kernel bên trong mới biết khác biệt.
  • "Used memory" isn't even one number. Page cache is technically in use but instantly reclaimable; shared pages get counted by several processes at once. Any honest answer has to come from the OS that owns those definitions."Memory đã dùng" thậm chí không phải một con số duy nhất. Page cache về kỹ thuật là đang dùng nhưng thu hồi được ngay; trang shared bị nhiều process cùng tính. Câu trả lời trung thực chỉ có thể đến từ OS — nơi định nghĩa những khái niệm đó.
  • Disk space used is the same story. EBS reports IOPS and throughput because the Nitro card handles every request, but "the filesystem is 80 % full" is metadata the guest wrote inside the volume and only the guest interprets.Dung lượng đĩa đã dùng cũng vậy. EBS báo được IOPS và throughput vì Nitro card xử lý mọi request, nhưng "filesystem đầy 80 %" là metadata do guest ghi bên trong volume và chỉ guest hiểu.
  • Detailed monitoring does not add memory. It only makes the existing metrics arrive every minute instead of every five — a different problem entirely.Detailed monitoring không thêm memory. Nó chỉ làm các metric sẵn có về mỗi phút thay vì mỗi 5 phút — hoàn toàn là chuyện khác.
The easiest paths AWS gives youCách dễ nhất mà AWS cho sẵn EC2: install the CloudWatch agent — the console wizard's basic config already includes mem_used_percent and disk_used_percent, and SSM Distributor + State Manager rolls it out fleet-wide with no SSH. ECS / EKS: turn on Container Insights and memory arrives without you writing an agent config at all. Lambda: nothing to install — every invocation already logs Max Memory Used, and Lambda Insights charts it. The gap is specific to an OS you own, on EC2. EC2: cài CloudWatch agent — cấu hình basic trong wizard của console đã có sẵn mem_used_percent và disk_used_percent, còn SSM Distributor + State Manager triển khai cho cả fleet mà không cần SSH. ECS / EKS: bật Container Insights là có memory, không phải viết file cấu hình agent nào. Lambda: khỏi cài gì — mỗi lần chạy đã ghi Max Memory Used vào log, Lambda Insights vẽ sẵn biểu đồ. Khoảng trống này chỉ tồn tại với một OS do bạn tự quản, trên EC2.

Which metric is measured by whom — and why a container reports memory while the instance under it doesn't: Where metrics come from → Metric nào do ai đo — và vì sao container báo được memory còn instance bên dưới thì không: Metric đến từ đâu →

2 · Alarms2 · Alarms

A CloudWatch alarm can act on EC2, Auto Scaling and SNS directly, and through EventBridge it can reach Kinesis, Step Functions and Lambda CloudWatch Alarm EventBridge EC2 action reboot stop terminate recover Auto Scaling SNS Kinesis Data Streams Step Functions Lambda SQS Lambda direct targets on the left · everything else via EventBridge or SNS

An alarm only knows three direct targets. EventBridge and SNS are the adapters that get you to everything else.

Alarm chỉ biết ba đích trực tiếp. EventBridge và SNS là hai bộ chuyển tiếp đưa bạn tới mọi thứ còn lại.

  • Alarms trigger actions: EC2 action (reboot, stop, terminate, recover), Auto Scaling, SNS.Alarm kích hoạt: EC2 action (reboot, stop, terminate, recover), Auto Scaling, SNS.
  • Alarm events can be intercepted by Amazon EventBridge — that's how one alarm ends up starting a Step Functions workflow or a Lambda.Event của alarm bị Amazon EventBridge bắt được — đó là cách một alarm khởi động được workflow Step Functions hay một Lambda.
  • EC2 recover is the one worth remembering: it moves the instance to new underlying hardware keeping the same instance ID, private IP and Elastic IP — the fix for a failed host, not a failed application.EC2 recover là action đáng nhớ nhất: chuyển instance sang phần cứng khác nhưng giữ nguyên instance ID, private IP và Elastic IP — cách xử lý khi host hỏng, không phải khi ứng dụng hỏng.
  • An alarm has three states: OK, ALARM, INSUFFICIENT_DATA. The third one is why a brand-new alarm looks broken for the first few periods.Alarm có ba trạng thái: OK, ALARM, INSUFFICIENT_DATA. Trạng thái thứ ba là lý do alarm mới tạo trông như bị lỗi trong vài chu kỳ đầu.
DashboardsDashboard Display metrics and alarms, and — unusually for an AWS console feature — a single dashboard can show metrics from multiple regions. That makes it the one place to watch a multi-region deployment. Hiển thị metric và alarm, và — hiếm gặp với tính năng console của AWS — một dashboard hiển thị được metric từ nhiều region. Nhờ vậy nó là chỗ duy nhất để theo dõi một hệ thống multi-region.

3 · Logs3 · Logs

A log group represents an application and contains one log stream per instance, file or container — here two ECS services writing into one group; retention and KMS encryption are set on the group EC2 · ECS · Lambda VPC Flow Logs API Gateway · Route 53 SDK · agent · CloudTrail log group — awslogs-repio-production-log-group log streams — one per container / task repio-production-app-service/repio-production-app/83a7c1 repio-production-worker-service/repio-production-worker/001c72 repio-production-app-service/repio-production-app/9f21e4 ECS stream name = prefix / container name / task id retention: never expire · 1 day … 30 days … 10 years optional KMS encryption — set on the group group = the application · stream = one producer inside it

Retention and encryption are properties of the group, not the stream — and retention defaults to "never expire", which is how log bills quietly grow.

Retention và mã hoá là thuộc tính của group, không phải stream — và mặc định là "never expire", đó là lý do hoá đơn log âm thầm phình to.

  • Log group: arbitrary name, usually representing an application. Log stream: instances within the application, log files, or containers.Log group: tên tuỳ ý, thường đại diện cho một ứng dụng. Log stream: các instance trong ứng dụng đó, file log, hoặc container.
  • You can define log expiration policies (never expire, 30 days, etc.) and turn on optional KMS encryption.Có thể đặt chính sách hết hạn log (never expire, 30 ngày…) và bật mã hoá KMS tuỳ chọn.
  • Sources: SDK · CloudWatch Logs Agent and CloudWatch Unified Agent · Elastic Beanstalk (application logs) · ECS (containers) · Lambda (function logs) · VPC Flow Logs · API Gateway · CloudTrail (based on a filter) · Route 53 (DNS queries).Nguồn: SDK · CloudWatch Logs Agent và CloudWatch Unified Agent · Elastic Beanstalk (log ứng dụng) · ECS (container) · Lambda (log function) · VPC Flow Logs · API Gateway · CloudTrail (theo filter) · Route 53 (truy vấn DNS).
  • Logs Agent vs Unified Agent: the older Logs Agent only ships log files. The Unified Agent ships logs and system-level metrics — RAM, disk space, per-process CPU, netstat — which is exactly the gap in the built-in EC2 metrics.Logs Agent vs Unified Agent: Logs Agent cũ chỉ đẩy file log. Unified Agent đẩy cả log lẫn metric mức hệ thống — RAM, dung lượng disk, CPU theo process, netstat — đúng vào chỗ trống của metric EC2 mặc định.

Metric filters & Logs InsightsMetric filter & Logs Insights

  • CloudWatch Logs can use filter expressions — for example, find a specific IP inside a log, or count occurrences of "ERROR".CloudWatch Logs dùng được filter expression — ví dụ tìm một IP cụ thể trong log, hoặc đếm số lần xuất hiện "ERROR".
  • Metric filters can be used to trigger alarms: the filter counts matches and publishes them as a metric, and the alarm watches that metric. This is the standard "alert on a log line" answer — filters are not retroactive, they only match data arriving after you create them.Metric filter dùng để kích hoạt alarm: filter đếm số lần khớp rồi publish thành metric, alarm theo dõi metric đó. Đây là đáp án chuẩn cho "báo động theo một dòng log" — filter không áp dụng ngược, chỉ khớp data tới sau khi tạo.
  • CloudWatch Logs Insights queries logs with its own query language and can add those queries to CloudWatch Dashboards. Interactive investigation, not alerting.CloudWatch Logs Insights query log bằng ngôn ngữ truy vấn riêng và ghim query lên CloudWatch Dashboard. Dùng để điều tra tương tác, không phải để cảnh báo.

4 · Getting logs out4 · Đưa log ra ngoài

Two completely different mechanisms, and the exam question is always which one. Export to S3 is a batch job; subscription filters are a live stream. If the wording contains "real time", the answer is never the export. Hai cơ chế hoàn toàn khác nhau, và đề luôn hỏi chọn cái nào. Export sang S3 là một job batch; subscription filter là luồng chảy liên tục. Nếu đề có chữ "real time" thì đáp án không bao giờ là export.

Exporting to S3 is a batch task that can lag up to 12 hours, while a subscription filter streams log events to Lambda, Firehose or Kinesis Data Streams in real time CloudWatch Logs CreateExportTask Amazon S3 SSE-S3 or SSE-KMS required batch · up to 12 h before data is exportable subscription filter Lambda real time → OpenSearch Firehose near real time → S3, OpenSearch Kinesis Data Streams → Firehose, Flink, EC2, Lambda… top path: batch archive · bottom path: live stream

Same source, two speeds. The export exists for archives and one-off dumps; everything operational goes through a subscription filter.

Cùng một nguồn, hai tốc độ. Export dùng để lưu trữ và trích xuất một lần; mọi thứ mang tính vận hành đều đi qua subscription filter.

  • S3 export: buckets must be encrypted with AES-256 (SSE-S3) or SSE-KMS; log data can take up to 12 hours to become available for export; the API call is CreateExportTask.Export S3: bucket phải mã hoá bằng AES-256 (SSE-S3) hoặc SSE-KMS; data log mất tới 12 giờ mới sẵn sàng để export; API là CreateExportTask.
  • The export is not near-real-time or real-time — use Logs Subscriptions instead. This sentence is the whole exam trap.Export không phải near-real-time hay real-time — hãy dùng Logs Subscription. Câu này chính là toàn bộ cái bẫy trong đề.
  • Subscription filter destinations: a Lambda (AWS-managed for OpenSearch, or your own custom function), Kinesis Data Firehose, or Kinesis Data Streams.Đích của subscription filter: một Lambda (bản do AWS quản cho OpenSearch, hoặc function tự viết), Kinesis Data Firehose, hoặc Kinesis Data Streams.
  • The filter part matters: a subscription filter can select which log events leave, so you don't pay to stream everything.Phần "filter" có ý nghĩa: subscription filter chọn những log event nào được gửi đi, khỏi phải trả tiền để stream tất cả.

Aggregating across accounts and regionsGom log từ nhiều account và region

Subscription filters in several accounts and regions all feed one Kinesis Data Stream, which fans into Firehose and lands in a single S3 bucket account · region Account A region 1 Account B region 2 Account B region 3 subscription filter subscription filter subscription filter Kinesis Data Streams Firehose S3 near real time one stream is the meeting point — cross-account access is granted on it

The pattern for "collect all our logs in one place": a subscription filter per account/region, one shared Kinesis stream, one bucket.

Mẫu cho bài toán "gom hết log về một chỗ": mỗi account/region một subscription filter, một Kinesis stream dùng chung, một bucket.

5 · The agent, installed with SSM5 · Agent, cài bằng SSM

You don't SSH into a hundred instances to install an agent. Systems Manager does it: Run Command for a one-off push, State Manager to keep it installed, and Parameter Store to hold the configuration the agent downloads. Không ai SSH vào cả trăm instance để cài agent. Systems Manager lo việc đó: Run Command để đẩy một lần, State Manager để giữ cho nó luôn được cài, và Parameter Store để chứa cấu hình cho agent tải về.

SSM Run Command Document AWS-ConfigureAWSPackage, name AmazonCloudWatchAgent — install now, onceDocument AWS-ConfigureAWSPackage, name AmazonCloudWatchAgent — cài ngay, một lần
SSM State Manager Same document, but enforced as desired state — new instances get it tooCùng document, nhưng áp như trạng thái mong muốn — instance mới cũng được cài
SSM Parameter Store Holds the agent config; the agent downloads it — change once, applies fleet-wideChứa config của agent; agent tải về — sửa một chỗ, áp cho cả fleet
  • Install the CloudWatch agent using SSM Run Command — SSM document AWS-ConfigureAWSPackage, package name AmazonCloudWatchAgent.Cài CloudWatch agent bằng SSM Run Command — SSM document AWS-ConfigureAWSPackage, tên package AmazonCloudWatchAgent.
  • Install using SSM State Manager with the same document when you want it to stay installed rather than be pushed once.Cài bằng SSM State Manager với cùng document khi muốn nó luôn được cài chứ không chỉ đẩy một lần.
  • Configure the agent by storing the configuration in SSM Parameter Store; instances download it from there.Cấu hình agent bằng cách lưu config vào SSM Parameter Store; instance tải về từ đó.
  • Prerequisite either way: the instance needs the SSM agent running and an instance role allowing SSM plus CloudWatchAgentServerPolicy.Điều kiện chung: instance phải chạy SSM agent và có instance role cho phép SSM cộng CloudWatchAgentServerPolicy.

6 · Synthetics Canary6 · Synthetics Canary

A canary is a small script that pretends to be a customer, on a schedule. Metrics tell you a server is healthy; a canary tells you the journey still works — login, checkout, the API returning sensible JSON. It catches the outage that every server-side metric happily reports as fine. Canary là một script nhỏ đóng vai khách hàng, chạy theo lịch. Metric cho biết server còn sống; canary cho biết hành trình vẫn chạy — đăng nhập, thanh toán, API trả JSON hợp lệ. Nó bắt được sự cố mà mọi metric phía server vẫn vui vẻ báo bình thường.

A canary monitors the primary region endpoint; when it fails the alarm invokes a Lambda that updates the Route 53 record to point at the secondary region Users Route 53 us-east-1 · primary endpoint ✗ down monitor Synthetics canary on a schedule trigger CloudWatch alarm invoke Lambda update DNS record traffic now goes here us-west-2 · standby endpoint ✓ healthy

Canary → alarm → Lambda → DNS change. A failover driven by the customer experience rather than by a health check on a port.

Canary → alarm → Lambda → đổi DNS. Failover được quyết định bởi trải nghiệm người dùng chứ không phải health check trên một cổng.

  • Configurable script that monitors your APIs, URLs and websites. It reproduces what your customers do programmatically to find issues before customers are impacted.Script cấu hình được, giám sát API, URL và website. Nó tái hiện đúng thao tác của khách hàng để phát hiện sự cố trước khi khách hàng bị ảnh hưởng.
  • Checks availability and latency of endpoints and can store load time data and screenshots of the UI.Kiểm tra tính sẵn sàng và độ trễ của endpoint, lưu được dữ liệu thời gian tải và ảnh chụp giao diện.
  • Scripts are written in Node.js or Python, with programmatic access to a headless Google Chrome browser — headless meaning a real browser engine with no window drawn, so the script can click and read the page like a person.Script viết bằng Node.js hoặc Python, có quyền điều khiển trình duyệt Google Chrome headless — headless nghĩa là engine trình duyệt thật nhưng không vẽ cửa sổ, nên script vẫn bấm và đọc trang như người dùng.
  • Integrates with CloudWatch Alarms, and can run once or on a regular schedule.Tích hợp CloudWatch Alarm, và chạy một lần hoặc theo lịch định kỳ.

BlueprintsBlueprint

A blueprint is a ready-made canary script you fill in rather than write from scratch. Blueprint là script canary làm sẵn, bạn chỉ điền thông tin thay vì viết từ đầu.

Heartbeat Monitor Load a URL, store a screenshot and an HTTP archive fileTải một URL, lưu ảnh chụp và file HTTP archive
API Canary Test basic read and write functions of REST APIsKiểm tra chức năng đọc/ghi cơ bản của REST API
Broken Link Checker Check all links inside the URL you are testingKiểm tra mọi link bên trong URL đang test
Visual Monitoring Compare a screenshot from this run with a baseline screenshotSo ảnh chụp lần chạy này với ảnh chuẩn
Canary Recorder Record your actions on a website and generate the script for themGhi lại thao tác trên website rồi tự sinh script
GUI Workflow Builder Verify actions can be taken on a page — e.g. a login formXác minh thao tác trên trang chạy được — ví dụ form đăng nhập

Exam reflexesPhản xạ khi thi

  • "Monitor EC2 memory / disk space used" → CloudWatch agent and a custom metric. Never a built-in one."Giám sát memory / disk đã dùng của EC2" → CloudWatch agent và custom metric. Không bao giờ là metric có sẵn.
  • "Auto Scaling reacts too slowly" → enable detailed monitoring (1-minute metrics)."Auto Scaling phản ứng chậm quá" → bật detailed monitoring (metric 1 phút).
  • "Alert when a specific string appears in the logs" → metric filter → alarm → SNS."Báo động khi log xuất hiện một chuỗi cụ thể" → metric filter → alarm → SNS.
  • "Stream logs to OpenSearch / a third party in real time" → subscription filter, not the S3 export (that one lags up to 12 hours)."Đẩy log sang OpenSearch / bên thứ ba real time" → subscription filter, không phải export S3 (cái đó trễ tới 12 giờ).
  • "Centralise logs from many accounts and regions" → subscription filter in each → one Kinesis Data Stream → Firehose → S3."Gom log từ nhiều account và region" → mỗi nơi một subscription filter → một Kinesis Data Stream → Firehose → S3.
  • "Instance is unreachable but the app is fine on a new host" → alarm with the EC2 recover action."Instance không truy cập được nhưng app chạy bình thường trên host mới" → alarm với action EC2 recover.
  • "Detect a broken checkout flow before users report it" → Synthetics canary, not a metric."Phát hiện luồng thanh toán hỏng trước khi người dùng báo" → Synthetics canary, không phải metric.