AWS Lambda Functions

Write, deploy and tune Lambda functions: handlers, triggers, execution roles, memory and timeouts, cold starts, layers and concurrency.

Intermediate · about 6 hours · difficulty 3/5

Learn first: STS, Assume Role and Federation, Amazon S3 Fundamentals

AWS Lambda runs your code in response to events without servers to manage. You pay per request and per millisecond of compute, and the free tier covers a generous number of invocations every month. Azure Functions and Google Cloud Functions / Cloud Run functions are the equivalents.

The basics

  • A handler function receives an event (an HTTP request, an S3 upload, a queue message) and a context.
  • Runtimes include Node.js, Python, Java, Go (via the OS-only runtime), .NET, or any language as a container image.
  • Each function has an execution role that decides what AWS APIs it can call.
  • Configure memory (128 MB to 10 GB); CPU scales with it. Timeout is up to 15 minutes.

Triggers

  • Synchronous: API Gateway, function URLs, ALB.
  • Asynchronous: S3, SNS, EventBridge (with retries and destinations or dead-letter queues).
  • Poll-based: SQS, DynamoDB Streams, Kinesis (event source mappings with batching).

Performance and scaling

  • Cold starts happen when a new execution environment is created. Keep packages small, initialize clients outside the handler, and use SnapStart or provisioned concurrency when latency matters.
  • Concurrency limits protect downstream databases. Use reserved concurrency to cap a function.
  • Use Powertools for AWS Lambda for structured logging, tracing and idempotency.

Pitfalls

  • Using Lambda for long-running or always-busy workloads where containers are cheaper.
  • Opening a new database connection on every invocation; use RDS Proxy or DynamoDB.
  • Non-idempotent handlers that double-charge when an event is retried.

You are ready to move on when you can deploy a function triggered by an S3 upload that writes metadata to DynamoDB, and explain cold starts and retries.

Key concepts

  • Handlers, events and context
  • Execution roles
  • Memory, CPU and timeouts
  • Sync, async and poll-based triggers
  • Cold starts
  • Concurrency and throttling
  • Layers and container images
  • Idempotency
  • Powertools for AWS Lambda

Mini project

Build an image thumbnail pipeline: uploading a JPG to an S3 bucket triggers a Lambda that writes a resized copy to a second bucket and records metadata in DynamoDB. Use small test images so everything stays within the always-free Lambda and DynamoDB limits, and delete buckets, table and function afterwards.

Check yourself

Your Lambda writes to S3 and is triggered by uploads to the same bucket. What goes wrong, and how do you prevent it?

Free resources