Free course · 14 lessons · Intermediate

Python Serverless on AWS Lambda

Run production Python on AWS Lambda through real failures: leaking module state, native wheels that will not load, slow import-time cold starts and SnapStart, concurrent I/O with threads, Decimal and datetime serialisation, Pydantic validation, FastAPI with Mangum or the Lambda Web Adapter, Powertools batch processing and idempotency, timeouts, memory and /tmp, database connections and testing with pytest and moto. Every lesson has a runnable Python simulation; shared serverless architecture topics link to the Node.js course.

Start the course

1. Python on Lambda: Runtime Fundamentals

How Python functions really run on Lambda: module state, packaging native dependencies, import-time cold starts, and concurrent I/O.

  1. The Handler, Init Code and Leaking State — Why one customer saw another customer's data, and what belongs at module level in a Python function.
  2. Packaging Dependencies and Native Wheels — Why a function that works on a developer's Mac fails on Lambda with 'invalid ELF header', and how to package Python dependencies correctly.
  3. Import Time, Heavy Libraries and SnapStart — Why importing pandas makes every cold start slow even for requests that never use it, and what to do about it.
  4. Concurrent I/O: Threads and asyncio — Why a function that calls five services one after another takes two seconds, and how to fan out safely in Python.

2. APIs and Events in Python

Responses and serialisation, validation, running FastAPI on Lambda, and batch processing with Powertools.

  1. JSON Responses: Decimals, Dates and Errors — Why reading a number from DynamoDB crashes the API with a 502, and how to return responses reliably.
  2. Validating Events with Pydantic — Why malformed events cause KeyErrors deep in business logic, and how to validate at the boundary.
  3. FastAPI on Lambda: Mangum and the Web Adapter — Why a FastAPI app deployed to Lambda returns 404 for every route, and how to run ASGI apps on Lambda well.
  4. Batch Processing and Idempotency with Powertools — Why an SQS consumer in Python sends duplicate emails, and how Powertools makes partial failures and retries safe.

3. Running Python Functions in Production

Timeouts and remaining time, memory and /tmp, database connections, and testing.

  1. Timeouts, Remaining Time and Structured Logs — Why a batch job silently loses work when it hits the timeout, and how to stop cleanly and leave a trail.
  2. Memory, Large Files and /tmp — Why a CSV processor runs out of memory on big files and slowly fills /tmp, and how to stream data instead.
  3. Database Connections from Python — Why a function fails for minutes after a database failover, and how to manage relational connections in Python functions.
  4. Testing Python Functions with pytest and moto — Why the only tests deploy to AWS, and how to test Python Lambda code quickly and realistically.

4. Capstone Case Studies

Design complete Python serverless solutions under realistic constraints.

  1. Capstone: A Nightly Import of Five Million Rows — Design a reliable nightly import of a large partner CSV into DynamoDB with Python on AWS.
  2. Capstone: Serve a FastAPI Service on Lambda, or Not — Decide how to run an existing FastAPI service on AWS and plan the move with evidence.