AWS Lambda
Invocation models, event source mappings, concurrency, versions and aliases, VPC networking, packaging and the limits the exam asks about.
Exam tasks: 2.5 (performance and scalability with serverless), 4.3 and 4.4 (modernize to serverless), 3.3
The decision: is the work short, event-driven and spiky enough for Lambda, and if so, how will it be invoked, how many copies may run at once, and what must it reach on the network?
Lambda runs your code in managed execution environments. You pay for requests and for memory-time, and you manage no servers. Everything else in a question, such as retries, ordering and cold starts, follows from how the function is invoked.
Invocation models
| Model | Examples | Who retries | Failure handling |
|---|---|---|---|
| Synchronous | API Gateway, ALB, Function URL, SDK RequestResponse | The caller | Caller sees the error or a 429 when throttled |
| Asynchronous | S3, SNS, EventBridge, SDK Event | Lambda, up to 2 retries by default | Event age limit up to 6 hours, then a DLQ or an on-failure destination |
| Event source mapping | SQS, Kinesis, DynamoDB Streams, MSK, Amazon MQ | Lambda keeps the batch until it succeeds or expires | Depends on the source, see below |
- A destination can also receive successes. It carries the request and response context, and a DLQ does not.
- For SQS, Lambda polls, scales consumers with queue depth and deletes the batch on success. Turn on
ReportBatchItemFailuresso one bad message returns to the queue and the rest of the batch is not repeated. Set the queue's visibility timeout to at least six times the function timeout. - For Kinesis and DynamoDB Streams, order is kept per shard. A failing batch blocks that shard until it succeeds or expires, so use a retry limit, bisect batch on error and an on-failure destination. The parallelization factor (up to 10) runs several batches per shard while keeping order per key.
One poison message blocks a stream shard
With a stream source, a record that always fails stops every record behind it in that shard. Limit retries and send the failed batch to an on-failure destination. Raising the timeout or the memory does not help.
Versions, aliases and packaging
| Concept | What to know |
|---|---|
$LATEST | The mutable working copy. Never point production at it |
| Version | An immutable snapshot of code and configuration |
| Alias | A named pointer to one version, or a weighted split between two. This is how CodeDeploy runs canary and linear Lambda releases |
| Layers | Shared libraries and runtimes, up to five per function. They count toward the 250 MB unzipped limit |
| Container image | Up to 10 GB, built from a Lambda base image or a custom image that implements the runtime API |
| Architecture | arm64 (Graviton) is cheaper per millisecond than x86 for most code |
| Function URL | A built-in HTTPS endpoint with AWS_IAM or no authentication. Fine for simple webhooks, with no API Gateway features |
- Execution role says what the function may call. A resource-based policy says who may invoke it, which is how S3, SNS and other accounts get in.
- Environment variables total 4 KB and can be encrypted with a customer managed KMS key. Keep secrets in Secrets Manager.
- Code signing with AWS Signer blocks unsigned or tampered packages at deployment.
Concurrency
| Unreserved | Reserved concurrency | Provisioned concurrency | |
|---|---|---|---|
| What it does | Shares the account pool | Guarantees and caps a function's concurrency | Keeps environments initialized and ready |
| Cold starts | Yes | Yes | No, up to the provisioned amount |
| Extra cost | No | No | Yes, while provisioned |
| Use it for | Most functions | Protect a downstream DB, or stop one function starving others | Latency-sensitive APIs, predictable peaks |
- Set reserved concurrency to 0 to throttle a function completely, for example during an incident.
- Schedule provisioned concurrency with Application Auto Scaling for known peaks such as business hours.
- SnapStart (Java, Python, .NET) cuts cold starts by restoring a snapshot of an initialized environment, at lower cost than provisioned concurrency.
- Throttled synchronous calls get a 429. Asynchronous events are retried, then go to a DLQ or an on-failure destination.
Lambda hammering a database
Scaling to thousands of concurrent functions can exhaust a relational database's connections. Use RDS Proxy to pool connections, and reserved concurrency to cap the function, rather than a bigger database instance.
Lambda in a VPC
- Attach a function to a VPC only when it must reach private resources such as RDS, ElastiCache or internal APIs.
- Lambda creates shared Hyperplane ENIs per subnet and security group combination when you configure the function, so cold starts no longer pay for ENI creation.
- A VPC-attached function has no public IP, even in a public subnet. For internet access, put it in private subnets with a route to a NAT gateway. For AWS services, use VPC endpoints and skip NAT charges.
- Use at least two subnets in different AZs.
Scaling behavior
- Lambda adds execution environments as demand grows, up to 1,000 new concurrent executions every 10 seconds per function, until it reaches the account's concurrency quota.
- Concurrency roughly equals requests per second multiplied by average duration in seconds. A 200 ms function at 500 requests per second needs about 100 concurrent executions.
- Memory sets CPU. A CPU-bound function often runs cheaper at higher memory, because it finishes faster. Use AWS Lambda Power Tuning or Compute Optimizer to find the point.
- Compute Savings Plans cover Lambda duration.
Lambda signals
"Under 15 minutes, event-driven, unpredictable traffic, no servers" means Lambda. "Longer than 15 minutes", "needs a GPU", or "steady high utilization" points to containers or EC2 instead.
Scenarios
Reporting partial batch failures makes Lambda delete the successful messages and return only the failed ones, and a dead-letter queue with a redrive policy collects a message that keeps failing. A batch size of 1 works but removes the efficiency of batching and does not isolate the bad message. A very short visibility timeout causes messages to be delivered again while still being processed. SNS would push asynchronously and does not remove the poison message.
A VPC-attached function has no public IP address, so internet access needs a NAT gateway. A gateway endpoint carries S3 traffic without NAT charges. A public subnet does not help, because the function's interface never receives a public address. An interface endpoint cannot reach an arbitrary third-party API.
Related
- Compute and containers: choosing between Lambda, containers and EC2.
- Decoupling: SQS, SNS and EventBridge as Lambda event sources.
- Deployment strategies: CodeDeploy canary and linear releases with aliases.
Further reading
Compute and containers
Choosing between EC2, Lambda, ECS, EKS, Fargate and Elastic Beanstalk, plus ECS network modes, Lambda concurrency and VPC access, and edge compute.
Containers on AWS
ECS, EKS, Fargate and ECR compared, with task networking, IAM roles, placement, scaling and the private-subnet endpoints that image pulls need.