0Pricing
AWS for Backend Developers (EC2, S3, RDS, Lambda) · บทเรียน

บันทึกและกลุ่มบันทึกของ CloudWatch

รวบรวม ตรวจสอบ และจัดเก็บบันทึกจากบริการ AWS และแอปพลิเคชันแบบกำหนดเองต่าง ๆ เพื่อการแก้ไขปัญหาและการตรวจสอบย้อนหลัง

บันทึกและกลุ่มบันทึกของ CloudWatch เป็นบทเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AWS for Backend Developers (EC2, S3, RDS, Lambda) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

CloudWatch Logs Intro

Imagine trying to fix a problem in your application without knowing what happened! That's where logs come in.

CloudWatch Logs is an AWS service that helps you collect, monitor, and store logs from your applications and various AWS services.

It's crucial for troubleshooting, auditing, and understanding how your systems are performing.

Log Groups Explained

At the core of CloudWatch Logs is the concept of a Log Group.

Think of a Log Group as a dedicated folder or container for logs that share the same characteristics, like logs from a specific application or service.

For example, all logs from your 'WebApp-Production' service might go into one Log Group.

Log Streams within Groups

Inside each Log Group, you'll find Log Streams.

A Log Stream is a sequence of log events from a single source within that Log Group.

  • For an EC2 instance, each instance might have its own Log Stream.
  • For a Lambda function, each invocation or version could generate a Log Stream.

They help you pinpoint the exact source of your log data.

Setting Up Log Groups

Many AWS services automatically create Log Groups for you when they generate logs, like AWS Lambda or AWS Fargate.

For custom applications, you'll typically create a Log Group manually and then configure your application or the CloudWatch Agent to send logs to it.

You can create and manage Log Groups via the AWS Management Console, AWS CLI, or SDKs.

Ingesting EC2 Logs

To send logs from applications running on an EC2 instance to CloudWatch Logs, you use the CloudWatch Agent.

  • Install the agent on your EC2 instance.
  • Configure it to monitor specific log files (e.g., /var/log/myapp.log).
  • The agent then streams these logs to a specified Log Group and Log Stream in CloudWatch Logs.

Lambda's Automatic Logs

AWS Lambda automatically integrates with CloudWatch Logs! Any output your Lambda function generates (e.g., using print() in Python or console.log() in Node.js) is sent to CloudWatch Logs.

Try running this simple Python Lambda function and check its logs:

import json

def lambda_handler(event, context):
    print("INFO: Lambda function execution started.")
    
    # Simulate processing an event
    if 'detail' in event and 'message' in event['detail']:
        print(f"DEBUG: Processing message: {event['detail']['message']}")
    else:
        print("DEBUG: No specific message in event.")
        
    # Simulate an error condition based on input
    if 'statusCode' in event and event['statusCode'] >= 400:
        print("ERROR: An error status was detected in the input event!")
        return {
            'statusCode': event['statusCode'],
            'body': json.dumps('Error processed!')
        }
    
    print("INFO: Lambda function execution finished successfully.")
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

Finding Info in Logs

Once your logs are in CloudWatch Logs, you can easily search and filter them.

  • Search: Look for specific keywords, phrases, or patterns across all your log events.
  • Filter: Narrow down results by time range, Log Stream, or even specific fields if your logs are structured (e.g., JSON).

This helps you quickly diagnose issues or find relevant events.

Managing Log Retention

Logs can consume a lot of storage, and you might have compliance requirements for how long to keep them.

Log Retention Policies allow you to define how long CloudWatch Logs should store your log events.

  • Options range from 'Never Expire' to 1 day, 30 days, 1 year, etc.
  • Setting appropriate retention helps manage costs and adhere to data policies.

Log Metrics Filters

Beyond just viewing logs, you can extract meaningful data from them using Metric Filters.

A Metric Filter defines a pattern to look for in your log events (e.g., the word "ERROR" or a specific status code).

When the pattern is matched, CloudWatch increments a custom metric, which you can then use to create alarms (as seen in the previous lesson!).

Log Group Basics

Time for a quick check on CloudWatch Logs!

Which statement best describes the relationship between CloudWatch Log Groups and Log Streams?

Logs Recap

Great job! You've learned the essentials of CloudWatch Logs.

We covered:

  • Log Groups for organizing logs.
  • Log Streams for individual log sources.
  • How to ingest logs from services like EC2 and Lambda.
  • How to search and filter logs efficiently.
  • Configuring retention policies and creating metric filters from logs.

Next, we'll explore CloudTrail for event-driven monitoring and auditing.

คำถามที่พบบ่อย

บทเรียน “บันทึกและกลุ่มบันทึกของ CloudWatch” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “บันทึกและกลุ่มบันทึกของ CloudWatch” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AWS for Backend Developers (EC2, S3, RDS, Lambda) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AWS for Backend Developers (EC2, S3, RDS, Lambda) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “บันทึกและกลุ่มบันทึกของ CloudWatch”

รวบรวม ตรวจสอบ และจัดเก็บบันทึกจากบริการ AWS และแอปพลิเคชันแบบกำหนดเองต่าง ๆ เพื่อการแก้ไขปัญหาและการตรวจสอบย้อนหลัง คุณปฏิบัติ AWS for Backend Developers (EC2, S3, RDS, Lambda) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน AWS for Backend Developers (EC2, S3, RDS, Lambda) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “บันทึกและกลุ่มบันทึกของ CloudWatch” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) นี้ได้ไหม

ได้ บทเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ตัวชี้วัดและการแจ้งเตือนของ CloudWatch
  2. บันทึกและกลุ่มบันทึกของ CloudWatch
  3. การตรวจสอบที่ขับเคลื่อนด้วยเหตุการณ์ด้วย CloudTrail
  4. การสร้างแดชบอร์ด CloudWatch
← กลับไปที่ AWS for Backend Developers (EC2, S3, RDS, Lambda)