Lesson 2Logs

CloudWatch Logs and EventBridge

CloudWatch Logs รวม logs จาก EC2, CloudTrail, Route 53, Lambda และ application ของคุณไว้ให้ค้นหา วิเคราะห์ ตั้ง alarm และส่งต่อไป processing pipeline ได้

Log Group, Log Stream และ Retention

Log Group เป็น container หลักของ logs เช่น /aws/ec2/app ส่วน Log Stream มักแทน instance, process หรือ runtime stream ถ้าไม่ตั้ง retention logs อาจเก็บยาวและเกิดค่า storage ต่อเนื่อง จึงควรกำหนด retention ตาม compliance และ troubleshooting need

CloudWatch Logs Insights

Logs Insights ใช้ query log data เพื่อหา error, latency, user id, request id หรือ pattern ที่เกิดขึ้นในช่วงเวลา incident Query ที่ดีควรมี filter แคบพอเพื่อลด scanned data และ cost

fields @timestamp, @message
| filter @message like /ERROR/
| sort @timestamp desc
| limit 20

Metric filter และ Subscription filter

  • Metric filter: แปลง pattern ใน logs เป็น CloudWatch metric เช่นนับคำว่า ERROR
  • Subscription filter: ส่ง logs ไปปลายทางอื่น เช่น Lambda, Kinesis Data Firehose หรือ central logging account

EventBridge

Amazon EventBridge ใช้รับ event จาก AWS services, SaaS หรือ custom applications แล้ว route ไป targets ตาม rule ใช้ทำ automation เช่นแจ้งเตือนเมื่อ instance state เปลี่ยน, trigger runbook หรือตอบสนองต่อ CloudTrail event บางชนิด

Common mistakes

  • ไม่มี retention policy ทำให้ log cost โตเงียบๆ
  • log sensitive data เช่น password, token หรือ PII ลง application log
  • query Logs Insights กว้างเกินและ scan data มากโดยไม่จำเป็น
  • สร้าง EventBridge rule แล้วไม่มี dead-letter/error handling สำหรับ target

Review questions

  1. Log Group ต่างจาก Log Stream อย่างไร?
  2. Metric filter ใช้ทำอะไร?
  3. EventBridge เหมาะกับ operational automation แบบใด?