What Is AWS Lambda?
The Serverless Model
"Serverless" doesn't mean no servers exist — it means you don't manage them. AWS provisions, scales, and maintains the underlying infrastructure. You only write and deploy your function code.
This is a shift from traditional hosting:
Traditional: You → provision server → deploy app → manage uptime → pay 24/7
Serverless: You → write function → deploy → AWS handles the rest → pay per call
How Lambda Works
- You write a function — a piece of code with a defined entry point called a handler
- You attach one or more triggers — events that invoke the function
- When a trigger fires, AWS spins up a container, runs your handler, and shuts it down
- You're billed only for the execution duration, rounded to 1ms
Trigger (HTTP request) → Lambda service → Container starts → Handler runs → Response returned → Container idles/shuts down
Anatomy of a Lambda Function
Node.js Handler
export const handler = async (event, context) => {
console.log('Event:', JSON.stringify(event));
return {
statusCode: 200,
body: JSON.stringify({ message: 'Hello from Lambda!' }),
};
};
| Parameter | What it contains |
|---|---|
event | The trigger data (HTTP request body, S3 object info, etc.) |
context | Metadata about the invocation (function name, timeout remaining, request ID) |
| Return value | The response — for API Gateway, must include statusCode and body |
Python Handler
import json
def handler(event, context):
print(f"Event: {json.dumps(event)}")
return {
'statusCode': 200,
'body': json.dumps({'message': 'Hello from Lambda!'})
}
Common Triggers
| Trigger | Use Case |
|---|---|
| API Gateway | Build REST APIs — HTTP request invokes the function |
| S3 | Process files when uploaded to a bucket |
| SQS | Process messages from a queue (background jobs) |
| EventBridge | Scheduled tasks (cron-style) or custom event bus |
| DynamoDB Streams | React to database changes in real time |
| SNS | Fan-out notifications to multiple subscribers |
Cold Starts
When Lambda hasn't been invoked recently, the container that runs your function needs to be initialized — this is called a cold start and adds latency (typically 100ms–1s depending on runtime and package size).
Subsequent calls to a "warm" container are fast. Strategies to reduce cold starts:
- Keep functions small (fewer dependencies = faster init)
- Use Provisioned Concurrency for latency-sensitive functions
- Prefer Node.js or Python over Java (lighter runtimes)
Billing
Lambda billing has two components:
| Metric | Free Tier | After Free Tier |
|---|---|---|
| Requests | 1M/month | $0.20 per 1M requests |
| Duration | 400,000 GB-seconds/month | $0.0000166667 per GB-second |
For most small to medium applications, Lambda is effectively free or costs cents per month.
Limitations
| Limit | Value |
|---|---|
| Max execution timeout | 15 minutes |
| Max memory | 10,240 MB |
| Max deployment package | 250 MB (unzipped) |
| Max response payload | 6 MB (synchronous) |
Lambda is not suitable for long-running processes (use EC2 or Fargate instead) or large in-memory datasets.