AWS Lambda: Serverless Computing

Demo on AWS Lambda

Running Code in Response to Events Without Managing Infrastructure

1. What is AWS Lambda?

AWS Lambda is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers. In a Lambda architecture, you simply upload your code, and the service handles the rest—from high availability and scaling to infrastructure maintenance and security patching.

Core Philosophy: Focus on "business logic" (the code) rather than "infrastructure" (the servers).

2. Lambda vs. Server-Based Computing (EC2)

Choosing between Lambda and traditional servers (like Amazon EC2) is a fundamental architectural decision based on cost, control, and performance needs.

Feature Server-Based (EC2) Serverless (Lambda)
Management High (OS, patching, scaling) Zero (Managed by AWS)
Cost Model Always-on (Pay for uptime) Pay-per-execution (Pay for use)
Scaling Manual or Auto-scaling groups Instant, automatic per request
Runtime Limit Unlimited Maximum 15 minutes per task
Hardware Access Full (Root/Admin access) Restricted (Sandbox environment)

3. How AWS Executes Lambda Internally

AWS Lambda uses a sophisticated virtualization technology called Firecracker MicroVMs to execute your code in safe, isolated environments. Here is the lifecycle of a Lambda execution:

  1. Event Trigger: An event source (like an S3 upload or an API request) sends a trigger to the Lambda service.
  2. The Sandbox (Init Phase): Lambda spins up a lightweight MicroVM. It downloads your code, initializes the runtime (e.g., Python or Node.js), and runs any static initialization code outside the handler.
  3. The Invoke Phase: The specific "handler" function is executed with the event data. This is where your business logic runs.
  4. Freeze & Thaw: Once the function finishes, Lambda "freezes" the environment. If another request arrives shortly after, it reuses the environment (a "Warm Start"). If no requests arrive, the environment is destroyed (a "Cold Start" occurs on the next request).

4. Key Use Cases

Real-time File Processing

Automatically generating thumbnails when a user uploads a high-resolution photo to an S3 bucket.

Serverless Backends (APIs)

Using Amazon API Gateway to trigger Lambda functions that handle HTTP requests for mobile or web applications.

Scheduled Tasks (Cron Jobs)

Running a cleanup script every night at 2:00 AM to delete old log files or generate daily financial reports.

Stream Processing

Processing thousands of website clickstream events per second from Amazon Kinesis to perform real-time analytics.

Summary

AWS Lambda is the cornerstone of modern cloud-native architecture. While it has limits (like the 15-minute execution cap and "Cold Start" latency), its ability to scale to zero costs when idle makes it the most cost-effective choice for event-driven workloads and microservices.