Skip to main content

MODULE 12 - Lambda: Troubleshooting and Best Practices

Overview​

This module delves into Lambda troubleshooting and best practices. You will learn to diagnose common errors and optimize your functions.

Learning Objectives​

  • Identify common Lambda errors
  • Diagnose timeout issues
  • Analyze CloudWatch logs effectively
  • Apply best practices
  • Optimize performance

Weekly Content​

1. Common Lambda Errors​

┌─────────────────────────────────────────────────────────────┐
│ COMMON LAMBDA ERRORS │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. TIMEOUT │
│ Symptom: "Task timed out after X seconds" │
│ Causes: │
│ • Processing too long │
│ • Slow external call (API, DB) │
│ • Blocked network connection │
│ Solutions: │
│ • Increase timeout (max 15 min) │
│ • Optimize code │
│ • Check VPC/Security Groups │
│ │
│ 2. OUT OF MEMORY │
│ Symptom: "Runtime exited with error" │
│ Causes: │
│ • Data too large │
│ • Memory leak │
│ Solutions: │
│ • Increase memory │
│ • Stream instead of loading everything │
│ │
│ 3. PERMISSION DENIED │
│ Symptom: "AccessDenied" or "not authorized" │
│ Causes: │
│ • Missing IAM role │
│ • Incorrect policy │
│ Solutions: │
│ • Check execution role │
│ • Add necessary permissions │
│ │
│ 4. IMPORT ERROR │
│ Symptom: "Unable to import module" │
│ Causes: │
│ • Misconfigured handler │
│ • Missing dependency │
│ Solutions: │
│ • Check file and handler name │
│ • Include all dependencies │
│ │
└─────────────────────────────────────────────────────────────┘

2. Analyzing CloudWatch Logs​

Lambda Logs in CloudWatch:

/aws/lambda/function-name
│
├── START RequestId: xxx Version: $LATEST
│ └── Start of execution
│
├── [INFO] Your application logs (print, logging)
│
├── END RequestId: xxx
│ └── End of execution
│
└── REPORT RequestId: xxx
Duration: 123.45 ms
Billed Duration: 200 ms
Memory Size: 128 MB
Max Memory Used: 64 MB
└── Important metrics!

Useful search:
• "ERROR" or "Exception" for errors
• "Task timed out" for timeouts
• "Memory" for memory issues

3. Diagnostic Method​

┌─────────────────────────────────────────────────────────────┐
│ LAMBDA DIAGNOSTIC │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. Identify the symptom │
│ └── Exact error message? │
│ │
│ 2. Check CloudWatch logs │
│ └── Is there a stack trace? │
│ └── What is the last log before the error? │
│ │
│ 3. Check metrics │
│ └── Duration vs configured Timeout? │
│ └── Memory Used vs Memory Size? │
│ │
│ 4. Test locally │
│ └── Reproduce with the same data │
│ │
│ 5. Check configuration │
│ └── Correct handler? │
│ └── Environment variables? │
│ └── IAM role? │
│ │
└─────────────────────────────────────────────────────────────┘

4. Best Practices​

✅ LAMBDA BEST PRACTICES

PERFORMANCE:
• Initialize connections outside the handler (reuse)
• Use appropriate memory (more memory = more CPU)
• Minimize package size

SECURITY:
• Principle of least privilege (IAM)
• Encrypted environment variables (KMS)
• No secrets in code

OBSERVABILITY:
• Structured logging (JSON)
• Custom metrics if needed
• X-Ray for tracing

RESILIENCE:
• Error handling (try/catch)
• Retries with backoff
• Dead Letter Queue for failures

Exercise: Troubleshooting Challenge​

Scenario​

A Lambda function fails intermittently. Here are the symptoms:

  • Sometimes times out after 3 seconds
  • Sometimes "Connection refused" error
  • Logs show attempts to connect to an external API

Questions​

  1. What are your hypotheses?
  2. What tests do you perform?
  3. What solutions do you propose?

Deliverable​

Troubleshooting Debrief:

  • Issues encountered
  • Diagnostic method used
  • Solutions applied
  • Lessons learned
  1. Lambda Troubleshooting
  2. Validation Quiz - 30 questions to validate your knowledge
  3. Lab: Lambda Troubleshooting

Next week: MODULE 13 - Observability