Lambda
Definition: AWS Lambda is serverless compute — you upload
a function (handler code); AWS runs it when an event triggers it.
You do not provision or manage servers, and you pay per invocation and
duration (GB-seconds), not for idle VMs.
Example upload Rest API code to Lambda, and once client hits lambda will
start the service and you pay as you go
Serverless here means you do not patch OS or scale instances —
AWS does. There are still servers behind the service.
Where Lambda fits in architecture
Lambda is the worker that runs small units of logic in response to events. It sits behind a trigger, not as a long-running web server (unless fronted by API Gateway for request/response APIs).
Event source Lambda Downstream
───────────── ────── ──────────
API Gateway ──HTTP request──► your handler ──► DynamoDB, S3, SNS
S3 bucket ──object created► your handler ──► resize image, log
SQS queue ──message───────► your handler ──► process order
EventBridge ──schedule/event► your handler ──► cleanup, sync
Step Functions ──Task step───► your handler ──► one step in workflow
CloudWatch ──alarm────────► your handler ──► notify, remediate
Simple example — resize image on S3 upload
User uploads photo.jpg to S3. S3 notifies Lambda; Lambda
reads the file, creates a thumbnail, saves it to another bucket.
# Invoke manually for testing (optional)
aws lambda invoke \
--function-name ResizeImage \
--payload file://s3-event-sample.json \
out.json
Lambda vs EC2 (when to pick which)
| Lambda | EC2 | |
|---|---|---|
| Runs | On event; seconds to 15 min max | Always on; full VM control |
| Scale | Automatic per concurrency | You configure Auto Scaling |
| Cost | Per invocation + duration | Per hour while instance runs |
| Best for | Sporadic, small, event-driven tasks | Steady load, custom OS, long jobs |