การจัดการข้อผิดพลาด การลองใหม่ และคิวจดหมายตาย
สร้างฟังก์ชัน Lambda ที่ทนทานด้วยการทำความเข้าใจว่าข้อผิดพลาดส่งต่อกันอย่างไร การลองใหม่ทำงานแตกต่างกันตามประเภทการเรียกใช้อย่างไร และคิวจดหมายตายเก็บความล้มเหลวไว้อย่างไร
การจัดการข้อผิดพลาด การลองใหม่ และคิวจดหมายตาย เป็นบทเรียน Serverless Backend with AWS Lambda & API Gateway ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Serverless Backend with AWS Lambda & API Gateway และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Serverless Backend with AWS Lambda & API Gateway มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Errors in Serverless Functions
When a function throws or times out, the platform treats it as a failed invocation. How that failure is handled depends on how the function was invoked.
- Synchronous: error returns to the caller
- Asynchronous: platform retries automatically
- Stream/poll: behavior depends on the source
Synchronous Error Handling
For synchronous calls (like an API request) the error is returned immediately to the caller. The caller, such as API Gateway, decides how to map it to an HTTP status.
Asynchronous Retries
For asynchronous invocations the platform automatically retries failed events a couple of times with delay before giving up. Your code must be safe to run more than once.
Idempotency
Because retries can re-run an event, functions should be idempotent: processing the same event twice produces the same result without duplicate side effects.
def handler(event, context):
key = event["id"]
if already_processed(key):
return "skip"
process(key)
mark_processed(key)
return "ok"Try/Except in the Handler
Catch expected errors and respond gracefully; let truly unexpected errors bubble up so the platform can retry or record them.
def handler(event, context):
try:
return do_work(event)
except ValidationError as e:
return {"statusCode": 400, "body": str(e)}Dead-Letter Queues
A dead-letter queue (DLQ) captures events that still fail after all retries, so they are not lost. You can inspect and reprocess them later.
- Attach an SQS queue or SNS topic as the DLQ
- Failed events land there with metadata
Configuring a DLQ
You point the function at a DLQ target. After exhausting retries, the platform delivers the failed event there instead of dropping it.
aws lambda update-function-configuration \
--function-name worker \
--dead-letter-config TargetArn=arn:aws:sqs:...:failed-eventsDestinations: Success and Failure
Beyond DLQs, destinations route the result of async invocations on success or failure to another service, with richer context than a DLQ provides.
Handling Stream Failures
For stream sources (like a queue or stream), a failing batch can block progress. Configure batch bisection or a maximum retry age so a single poison message does not stall the whole stream.
Timeouts vs Errors
A timeout is a kind of failure: the function did not finish in time. Set timeouts realistically and instrument long operations so you can tell timeouts apart from thrown errors.
Observability for Failures
Log errors with context, emit metrics for failure counts, and alarm on DLQ depth. A growing DLQ is an early signal that something is systematically broken.
Quick Check
Test your error-handling understanding.
Recap
You learned robust Lambda error handling: how errors propagate per invocation type, why idempotency matters with retries, how to use try/except wisely, and how dead-letter queues and destinations capture failures so events are never silently lost.
เรียนรู้ Serverless Backend with AWS Lambda & API Gateway ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การจัดการข้อผิดพลาด การลองใหม่ และคิวจดหมายตาย” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดการข้อผิดพลาด การลองใหม่ และคิวจดหมายตาย” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless Backend with AWS Lambda & API Gateway ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless Backend with AWS Lambda & API Gateway มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดการข้อผิดพลาด การลองใหม่ และคิวจดหมายตาย”
สร้างฟังก์ชัน Lambda ที่ทนทานด้วยการทำความเข้าใจว่าข้อผิดพลาดส่งต่อกันอย่างไร การลองใหม่ทำงานแตกต่างกันตามประเภทการเรียกใช้อย่างไร และคิวจดหมายตายเก็บความล้มเหลวไว้อย่างไร คุณปฏิบัติ Serverless Backend with AWS Lambda & API Gateway ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless Backend with AWS Lambda & API Gateway หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Serverless Backend with AWS Lambda & API Gateway บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การจัดการข้อผิดพลาด การลองใหม่ และคิวจดหมายตาย” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Serverless Backend with AWS Lambda & API Gateway นี้ได้ไหม
ได้ บทเรียน Serverless Backend with AWS Lambda & API Gateway ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- รันไทม์และตัวจัดการของ Lambda
- ตัวแปรสภาพแวดล้อมและเลเยอร์
- การบันทึกและการเฝ้าติดตามด้วย CloudWatch
- การจัดการข้อผิดพลาด การลองใหม่ และคิวจดหมายตาย