0Pricing
Serverless AWS Lambda Development · บทเรียน

การจัดการข้อผิดพลาดและการลองใหม่

ใช้งานกลไกจัดการข้อผิดพลาดที่แข็งแกร่ง กำหนดค่าการลองใหม่โดยอัตโนมัติ และทำความเข้าใจข้อผิดพลาดในการเรียกใช้ เพื่อสร้างระบบแบบไร้เซิร์ฟเวอร์ที่ทนทานยิ่งขึ้น

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

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

Why Error Handling Matters

In serverless applications, things can go wrong. Your function might fail, a dependency might be unavailable, or an event might be malformed.

Robust error handling is crucial for building resilient systems that can recover gracefully and notify you when issues occur. This lesson explores how AWS Lambda helps you achieve this.

Types of Lambda Errors

When working with Lambda, it's helpful to understand different types of errors:

  • Invocation Errors: Problems occurring before your code runs, like incorrect IAM permissions for Lambda to read from an event source.
  • Function Errors: Exceptions thrown by your function code, timeouts, or out-of-memory errors. These are the errors you primarily handle within your code.
  • Service Errors: Rare issues with the underlying AWS Lambda service itself.

Sync vs. Async Error Paths

How Lambda handles errors depends on the invocation type:

  • Synchronous: (e.g., via API Gateway, ALB) Lambda returns the error directly to the caller. The caller is responsible for retries.
  • Asynchronous: (e.g., via S3, SNS, SQS) Lambda places the event in an internal queue and automatically retries the function if it fails. You don't get immediate feedback.

We'll focus mostly on asynchronous error handling and retries in this lesson.

Lambda's Async Retries

For asynchronous invocations, Lambda automatically retries your function if it fails due to an unhandled exception or times out.

  • By default, Lambda retries twice (total of 3 attempts).
  • These retries are automatic and happen with a delay, often with exponential backoff.
  • This built-in mechanism helps ensure temporary issues don't lead to lost events.

Configuring Async Retries

You can customize the asynchronous invocation settings for your Lambda function:

  • Maximum retry attempts: Set this from 0 to 2. Setting it to 0 means no retries.
  • Maximum event age: Define how long Lambda should keep an event in its queue for retries, from 60 seconds to 6 hours.

These settings give you control over how long and how often Lambda attempts to process a failed event.

Catching Failed Events with DLQs

Even with retries, some events might still fail consistently (e.g., due to malformed data). These are often called 'poison pill' messages.

A Dead Letter Queue (DLQ) is a designated destination (an SQS queue or SNS topic) where Lambda sends events that have exhausted all retry attempts.

DLQs are vital for debugging, preventing data loss, and analyzing why certain events couldn't be processed.

Setting Up Your DLQ

To use a DLQ:

  1. Create an Amazon SQS queue or SNS topic.
  2. Configure your Lambda function's asynchronous invocation settings to point to this SQS queue or SNS topic as its DLQ.
  3. Ensure your Lambda function's execution role has permissions to send messages to the chosen DLQ (e.g., sqs:SendMessage or sns:Publish).

This ensures that no event is truly 'lost' even after multiple failures.

Handling Errors in Your Code

While Lambda handles retries, you should still implement error handling within your function code using try-catch blocks. This allows you to:

  • Gracefully manage expected errors.
  • Log detailed context for debugging.
  • Perform cleanup or partial rollbacks before re-throwing an exception to trigger Lambda's retry mechanism.

Try running this simple Java example:

public class Main {
  public static void main(String[] args) {
    processData("valid data");
    System.out.println("------------------");
    processData("data with error");
  }

  public static void processData(String data) {
    try {
      System.out.println("Attempting to process: " + data);
      if (data.contains("error")) {
        throw new RuntimeException("Critical processing error!");
      }
      System.out.println("Successfully processed: " + data.toUpperCase());
    } catch (Exception e) {
      System.err.println("Caught an error: " + e.getMessage());
      System.err.println("Further action (e.g., logging, retry logic) would go here.");
      // In a real Lambda, re-throwing would trigger a retry for async invocations
    }
  }
}

Understanding Invocation Errors

Sometimes, Lambda can't even start your function. These are invocation errors.

  • Examples: Incorrect IAM permissions for Lambda to access an S3 bucket trigger, a misconfigured VPC, or exceeding service quotas before execution.
  • Detection: These errors often appear in CloudWatch Logs for your function or as `Invocation errors` metrics in the Lambda console. They are not typically handled by your function's try-catch blocks.

Quick Check on Retries

Let's test your understanding of error handling and retries in AWS Lambda.

Recap: Building Resilient Lambdas

You've learned how to make your serverless applications more robust:

  • Differentiated between various types of Lambda errors.
  • Understood how synchronous vs. asynchronous invocations handle errors differently.
  • Explored Lambda's automatic retry mechanism for asynchronous calls.
  • Discovered the importance of Dead Letter Queues (DLQs) for failed events.
  • Saw how to implement basic error handling within your code using try-catch.

By applying these techniques, you can build more resilient and reliable serverless systems.

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

บทเรียน “การจัดการข้อผิดพลาดและการลองใหม่” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจัดการข้อผิดพลาดและการลองใหม่” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless AWS Lambda Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดการข้อผิดพลาดและการลองใหม่”

ใช้งานกลไกจัดการข้อผิดพลาดที่แข็งแกร่ง กำหนดค่าการลองใหม่โดยอัตโนมัติ และทำความเข้าใจข้อผิดพลาดในการเรียกใช้ เพื่อสร้างระบบแบบไร้เซิร์ฟเวอร์ที่ทนทานยิ่งขึ้น คุณปฏิบัติ Serverless AWS Lambda Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless AWS Lambda Development หรือไม่

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

บทเรียน “การจัดการข้อผิดพลาดและการลองใหม่” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Serverless AWS Lambda Development นี้ได้ไหม

ได้ บทเรียน Serverless AWS Lambda Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. บันทึกและตัวชี้วัดของ CloudWatch
  2. การจัดการข้อผิดพลาดและการลองใหม่
  3. การแก้จุดบกพร่องแอปพลิเคชันแบบไร้เซิร์ฟเวอร์
  4. ตัวชี้วัดแบบกำหนดเองและการแจ้งเตือน CloudWatch
← กลับไปที่ Serverless AWS Lambda Development