AWS Cloud Architect & Developer · AWS Cloud Architecture
Lambda and Serverless
Event driven serverless compute with Lambda plus API Gateway and Step Functions.
Eight concepts on Lambda's execution model, cold starts, event sources, limits and pricing, plus API Gateway and Step Functions as the usual serverless companions.
- AWS Cloud Architect & Developer
- Medium level
- 8 concepts
- 5 practice questions
1Lambda execution model
Lambda runs your function in response to events without you provisioning servers. Each simultaneous in-flight invocation can get its own execution environment; concurrency is how many of those environments are running at once. You write the handler, set memory and timeout, and AWS scales the environments up and down with arrival rate — subject to the account concurrency limit (default 1,000 concurrent executions per Region, a soft limit you can raise).
Figure. Each concurrent event can get its own execution environment — scale is invocation concurrency, not one long VM.
How an invocation runs
- Event arrivesA trigger (API call, queue message, upload, schedule) targets the function.
- EnvironmentLambda places the call in an execution environment — new or reused.
- Scale with concurrencyMore overlapping calls mean more environments, up to the concurrency limit.
Lambda concurrency best means
- How many versions of the function code exist in S3
- How many execution environments are handling invocations at the same time
- How many Regions the function is copied to automatically
Concurrency counts simultaneous in-flight executions (environments). It is not the number of code versions or an automatic multi-Region copy.
2Cold starts and Provisioned Concurrency
A cold start is the extra latency when Lambda must initialise a new execution environment before your handler runs. Warm environments reuse a previous init and skip that cost. Provisioned Concurrency keeps a chosen number of environments initialised and ready so latency-sensitive APIs avoid cold starts — you pay to hold them warm. Spiky background work usually stays on on-demand concurrency; steady low-latency APIs are the Provisioned Concurrency cue.
Figure. Cold starts pay init latency; warm reuses the environment; provisioned concurrency keeps warm capacity ready.
When to buy warmth
- Measure the painIf init latency breaks an interactive SLA, cold starts matter.
- Steady API trafficProvisioned Concurrency holds environments ready for that floor of load.
- Spiky batchOn-demand concurrency is usually cheaper when occasional cold starts are fine.
| Approach | Effect | Cost cue |
|---|---|---|
| On-demand only | Cold start on new environments | Pay per use; accept init spikes |
| Provisioned Concurrency | Keeps N environments warm | Pay for provisioned capacity |
| Lean package / faster runtime | Shortens init when a cold start happens | Engineering effort, not a billing line |
A customer-facing API needs consistently low latency all day. Which lever targets cold starts directly?
- Raise the function timeout to 15 minutes
- Provisioned Concurrency
- Switch the trigger from API Gateway to polling S3 every hour
Provisioned Concurrency keeps environments warm so interactive calls skip init. Timeout only caps how long a run may last; an hourly S3 poll does not fix API cold starts.
3Event sources: push and poll
Lambda integrates with S3, DynamoDB Streams, SQS, SNS, API Gateway, EventBridge and more. Some sources push events to Lambda (API Gateway synchronous invoke, SNS/EventBridge asynchronous invoke). Others are poll-based: Lambda's event-source mapping pulls from SQS or a stream at a controlled rate. Putting SQS in front of a bursty producer lets Lambda drain the queue without being overwhelmed by a thundering push.
Figure. Burst lands on SQS; Lambda polls at a controlled rate instead of taking every push at once.
How to buffer bursts
- Producer spikesUploads or messages arrive faster than you want to invoke.
- QueueLand them on SQS so the burst becomes backlog, not concurrent stampede.
- PollLambda's event-source mapping pulls at a rate shaped by batch size and concurrency.
| Source | Typical invoke style | Exam cue |
|---|---|---|
| API Gateway | Push (often sync) | HTTP front door |
| S3, SNS, EventBridge | Push (async common) | React to an event |
| SQS, DynamoDB Streams | Poll (event-source mapping) | Buffer / ordered stream processing |
Producers can burst far above what Lambda should run at once. What pattern absorbs the spike?
- Invoke Lambda synchronously from every producer with no buffer
- Put SQS in front and let Lambda poll the queue
- Disable concurrency so every event waits inside API Gateway forever
SQS turns the burst into a backlog; Lambda's poller drains it under concurrency control. Unbuffered synchronous fan-out is the stampede; "disable concurrency" is not the buffering pattern.
4Memory, timeout and package limits
Configurable memory runs from 128 MB to 10,240 MB; CPU power scales up with the memory setting. Maximum timeout is 15 minutes (900 seconds) per invocation — longer work needs Step Functions chunking or a different compute service such as ECS/Fargate. Deployment packages: 50 MB zipped for direct upload, 250 MB unzipped, or up to 10 GB as a container image.
Lambda ceilings: memory 128 MB–10 240 MB, timeout up to 15 minutes, deployment package 50 MB zipped / 250 MB unzipped (or container image). The numbers are the concept; proportional bars would invent a false continuous trade-off.
| Knob | Range / cap |
|---|---|
| Memory | 128 MB – 10,240 MB (CPU scales with it) |
| Timeout | Up to 15 minutes (900 s) |
| Account concurrency | Default 1,000 per Region (soft) |
| Zip package | 50 MB zipped / 250 MB unzipped |
| Container image | Up to 10 GB |
A single job needs 40 minutes of continuous compute in one process. Lambda alone is wrong because
- Lambda cannot read from S3
- A single invocation cannot exceed the 15-minute timeout
- Lambda concurrency is fixed at one forever
Timeout tops out at 15 minutes per invocation. Long work needs Step Functions to orchestrate chunks, or another compute service — not one endless Lambda call.
5Pricing: requests and GB-seconds
Lambda bills per request plus compute time measured in GB-seconds: memory in GB multiplied by duration in seconds, with duration rounded up to the nearest 1 ms. Raising memory also raises CPU, so a higher memory setting can shorten duration enough that total GB-seconds — and cost — fall. Tune memory to minimise GB-seconds for the workload, not only to "add RAM".
Figure. Price tracks GB-seconds: memory setting × duration. More memory can finish sooner — compare the product.
How one invocation's compute bill is shaped
- Convert memoryExpress the configured memory in GB (512 MB → 0.5 GB).
- Measure durationUse the billed duration in seconds (after 1 ms rounding).
- MultiplyGB-seconds = memory(GB) × duration(s); requests add their own line item.
GB-seconds for one run
A function configured at 512 MB runs for 200 ms. How many GB-seconds of compute does that invocation consume?
- Memory512 MB = 0.5 GB
- Duration200 ms = 0.2 s
- GB-seconds = 0.5 \times 0.20.1 GB-s
Pro tip. If doubling memory halves duration, GB-seconds stay flat while wall-clock latency improves — and request charges are unchanged.
512 MB for 200 ms costs how many GB-seconds of compute (ignore the per-request fee)?
- 0.1
- 1.0
- 512 × 200
0.5 GB × 0.2 s = 0.1 GB-seconds. Leaving memory in MB or duration in ms without converting units invents a nonsense product.
6API Gateway in front of Lambda
API Gateway fronts Lambda with REST or HTTP APIs. It terminates HTTPS, can authorise callers, throttle, cache, and transform request/response shapes so the function stays a thin handler. Synchronous invoke means the caller waits on Lambda's response; design timeouts and error mapping at the gateway as carefully as in the function.
Figure. API Gateway is the HTTPS front door: routing, auth, throttling — then an integration invoke to Lambda or HTTP.
What the gateway absorbs
- Edge concernsTLS, auth, throttling and caching sit at API Gateway.
- InvokeGateway calls Lambda (commonly synchronous for request/response APIs).
- Map the resultStatus codes and bodies are shaped for HTTP clients before leaving the gateway.
You want HTTPS, API keys/throttling and a Lambda handler behind it. Which service is the usual HTTP front door?
- API Gateway
- SQS
- Step Functions Express only
API Gateway is the HTTP front door for Lambda. SQS is a queue; Step Functions orchestrates workflows, not general HTTPS API termination.
7Step Functions orchestration
Step Functions coordinates multiple Lambda functions (and other AWS integrations) as a state machine: Task states call workers, Choice branches on data, Retry/Catch handle transient failure. Standard workflows are long-running with exactly-once execution semantics and a full execution history — the durable business-process cue. Express workflows are for high-volume, short event processing with at-least-once semantics.
Figure. Step Functions orchestrates tasks and branches; Standard vs Express is durability/duration, not a different shape.
How to model an order flow
- One Task per stepValidate payment, reserve inventory, send confirmation — each a Task (often Lambda).
- Retry / CatchAttach backoff and failure routing on Tasks that can flake.
- Pick Standard vs ExpressDurable audited business process → Standard; high-volume short pipeline → Express.
| Type | Semantics | Exam cue |
|---|---|---|
| Standard | Exactly-once; long-running; full history | Orders, approvals, durable workflows |
| Express | At-least-once; high volume; short | Streaming / high-throughput event paths |
Payment → inventory → confirmation must be durable, auditable and exactly-once. Which Step Functions type?
- Express
- Standard
- Neither — only one Lambda may ever call another
Standard workflows give exactly-once execution and full history for durable business processes. Express is the high-volume at-least-once option.
8S3 thumbnail pipeline pattern
A classic serverless pipeline: ObjectCreated on an uploads prefix invokes Lambda; the function reads the object, writes a thumbnail to a different prefix or bucket, and uses a dead-letter queue for failures. Writing thumbnails back into the same watched prefix is the recursive-invocation trap — the new object fires the trigger again. IAM on the execution role needs s3:GetObject on the source and s3:PutObject on the destination only.
Figure. Write thumbs outside the trigger prefix so ObjectCreated does not loop; DLQ captures poison events.
How the pipeline is wired
- TriggerS3 event notification on ObjectCreated for the uploads prefix.
- TransformLambda reads, resizes, writes the thumbnail elsewhere.
- Isolate failureAttach an SQS DLQ so poison events are not silently lost.
Destination must differ from the trigger prefix
# trigger: s3://bucket/uploads/*
# output: s3://bucket/thumbs/* # NOT uploads/
aws s3 cp thumbs/out.jpg s3://bucket/thumbs/out.jpgThumbnails written back to the same S3 prefix that triggers the function risk
- Silent no-ops forever
- Recursive invocations as each thumbnail fires ObjectCreated again
- Automatic Multi-AZ failover of the bucket
Each write under the watched prefix emits ObjectCreated again. Use a separate destination prefix/bucket; Multi-AZ is not an S3 bucket failover feature in this sense.
Notes
- Lambda Execution Model: Lambda runs code in response to events without server management, scaling automatically by creating concurrent execution environments per simultaneous request.
- Cold Starts: A new execution environment incurs initialization latency (cold start); Provisioned Concurrency keeps environments warm to eliminate it for latency-sensitive APIs.
- Event Sources: Lambda integrates with S3, DynamoDB Streams, SQS, SNS, API Gateway, and EventBridge, using either push (synchronous/async) or poll-based (stream/queue) invocation.
- Step Functions: A serverless orchestrator that coordinates multiple Lambda functions into workflows using state machines, with Standard (long-running, exactly-once) and Express (high-volume, at-least-once) types.
- API Gateway: Fronts Lambda with REST or HTTP APIs, handling throttling, authorization, caching, and request/response transformation.
Formulas
- Lambda memory: configurable from 128 MB to 10,240 MB; CPU scales proportionally with memory.
- Lambda timeout: maximum execution duration is 15 minutes (900 seconds) per invocation.
- Lambda concurrency: default account limit of 1,000 concurrent executions per Region (soft limit, can be raised).
- Lambda pricing: billed per request plus GB-seconds = (memory in GB) x (duration in seconds), rounded up to the nearest 1 ms.
- Deployment package: 50 MB zipped direct upload, 250 MB unzipped, or up to 10 GB via container image.
Exam traps & shortcuts
- If a workload needs more than 15 minutes, Lambda is the wrong answer - choose ECS/Fargate or Step Functions to orchestrate chunks.
- For steady low-latency API traffic, use Provisioned Concurrency; for spiky background work, standard on-demand concurrency is cheaper.
- To decouple and buffer bursts before Lambda, put an SQS queue in front so Lambda polls at a controlled rate instead of being overwhelmed.
- Increasing memory often lowers total cost because faster CPU shortens duration - tune memory to minimize GB-seconds, not just to add RAM.
Reference tables
| Need | Service |
|---|---|
| HTTPS API in front of a function | API Gateway + Lambda |
| Buffer bursts before compute | SQS → Lambda (poll) |
| Multi-step durable workflow | Step Functions Standard |
| Work longer than 15 minutes in one shot | Not Lambda alone — chunk or use other compute |
Recap
Read only this the night before.
- Model
- Event → execution environment; concurrency = simultaneous environments (default 1,000/Region soft).
- Cold starts
- New environment init cost; Provisioned Concurrency keeps warm for steady APIs.
- Sources
- Push (API/SNS/S3) vs poll (SQS/streams); queue to absorb bursts.
- Limits
- Memory 128 MB–10 GB; timeout 15 min; zip 50/250 MB; image 10 GB.
- Price
- Requests + GB-seconds; more memory can cut duration and cost.
- Companions
- API Gateway for HTTP; Step Functions Standard for durable exactly-once flows; never write S3 outputs into the trigger prefix.
Practise Lambda and Serverless
Reading is free and needs no account. Practice, mocks and progress live in the app.
- 5 exam-style questions on this topic, with explanations
- A 5-question practice set that ends the chapter
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device