Obsługa błędów, ponowienia i kolejki dead-letter
Buduj niezawodne funkcje Lambda, poznając propagowanie błędów, działanie ponowień zależnie od typu wywołania oraz sposób, w jaki kolejki dead-letter przechwytują nieudane operacje.
Obsługa błędów, ponowienia i kolejki dead-letter to bezpłatna lekcja Serverless Backend with AWS Lambda & API Gateway na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Serverless Backend with AWS Lambda & API Gateway, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Serverless Backend with AWS Lambda & API Gateway zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Ucz się Serverless Backend with AWS Lambda & API Gateway dzięki korepetycjom AI — za darmo
Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.
- Kursy
- 12
- Lekcje
- 48
Często zadawane pytania
Czy lekcja „Obsługa błędów, ponowienia i kolejki dead-letter” jest bezpłatna?
Tak — pełny tekst „Obsługa błędów, ponowienia i kolejki dead-letter” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Serverless Backend with AWS Lambda & API Gateway, przejdź na CoddyKit PRO. Kurs Serverless Backend with AWS Lambda & API Gateway zawiera 4 lekcji w sumie.
Co nauczysz się w „Obsługa błędów, ponowienia i kolejki dead-letter”?
Buduj niezawodne funkcje Lambda, poznając propagowanie błędów, działanie ponowień zależnie od typu wywołania oraz sposób, w jaki kolejki dead-letter przechwytują nieudane operacje. Ćwiczysz Serverless Backend with AWS Lambda & API Gateway z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Serverless Backend with AWS Lambda & API Gateway?
Nie wymagamy żadnego doświadczenia. Serverless Backend with AWS Lambda & API Gateway w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Obsługa błędów, ponowienia i kolejki dead-letter”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Serverless Backend with AWS Lambda & API Gateway?
Tak. Każda lekcja Serverless Backend with AWS Lambda & API Gateway zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Środowisko uruchomieniowe i handler Lambda
- Zmienne środowiskowe i warstwy
- Logowanie i monitorowanie za pomocą CloudWatch
- Obsługa błędów, ponowienia i kolejki dead-letter