Writing and testing Lambda code
1. Writing and testing Lambda code
Welcome back. In this video, you'll write a clear Lambda handler, return predictable responses, test with realistic events, and use CloudWatch Logs, AWS's logging service, to debug failures.2. Why testing and logging matter
Events are inputs you do not control, so assume they can be messy. Testing is a fire drill that finds problems early. Logs are your black box when something fails.3. One flow to remember
Send an event in, the handler runs, and you get a response out. Use logs per invocation, and reuse saved test events to repeat inputs.4. Where logs go: CloudWatch Logs
Lambda sends print output and errors to CloudWatch Logs by default. When a test fails, open the log group named /aws/lambda/ followed by your function name.5. The handler contract
Your handler has a simple contract. It receives an event payload and a context object with metadata, and it returns a response. Keeping the handler focused makes testing and debugging much easier.6. Walkthrough: event and context
Read request_id from context to identify this invocation in logs. Read time_left to avoid timeouts. Then return your response object.7. Parsing input safely
Treat the event as untrusted input. Use event dot get to avoid missing-key errors, validate required fields early, and return a clear 400 response when input is invalid.8. Walkthrough: validate required input
Read name with event.get so missing keys do not crash the handler. If it is missing, return a 400 error. Otherwise return a 200 response.9. Edge cases to test
Test more than the happy path: empty JSON, wrong types, extra fields, and large payloads. These tests show whether validation is strong and failures are predictable.10. Returning a predictable response
Keep a consistent response shape: statusCode plus a JSON string body. This matches common HTTP integrations like API Gateway and makes console tests closer to real usage.11. Walkthrough: returning JSON
Import json, build a body dictionary, and use json.dumps to turn it into JSON text. Then return statusCode and body.12. Logging and tracing
Logs are your fastest feedback loop. Log key fields, include the request ID for tracing, and avoid logging secrets or personal data. By default, Lambda logs end up in CloudWatch Logs.13. Walkthrough: structured logging
Print one JSON object that includes request_id and the event shape. This makes logs easier to search and helps you trace a single invocation.14. Testing with console events
Create named test events and reuse them. Run a valid event and an invalid event, then check the response and CloudWatch logs. Make small changes so cause and effect stay clear.15. A simple debug loop
When you get stuck, change one thing, deploy, run a test, and read logs. Repeat until the behavior matches what you expect.16. Let's practice!
Now, let's get some hands on practice with some exercises!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.