Skip to Content
Asterrr's Handbook
2 · New solutions

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.

AWS CloudSynchronousAsynchronousEvent source mappingwait for resultqueued, retriedbatchesper-shard batchesafter retriesAPI Gateway orALBS3 or SNS eventSQS queueKinesis orDynamoDB streamLambda functionDLQ oron-failuredestination
Three ways to invoke Lambda. Callers wait for synchronous invocations, Lambda queues asynchronous ones, and Lambda polls streams and queues for you.
15 min
Maximum function timeout.
10,240 MB
Maximum memory. CPU scales with memory.
1,000
Default concurrent executions per Region, a soft quota you can raise.
6 MB
Synchronous request and response payload. Asynchronous invocations accept up to 1 MB.
250 MB
Unzipped deployment package, including layers. Container images go up to 10 GB.
10 GB
Maximum ephemeral storage in /tmp.

Invocation models

ModelExamplesWho retriesFailure handling
SynchronousAPI Gateway, ALB, Function URL, SDK RequestResponseThe callerCaller sees the error or a 429 when throttled
AsynchronousS3, SNS, EventBridge, SDK EventLambda, up to 2 retries by defaultEvent age limit up to 6 hours, then a DLQ or an on-failure destination
Event source mappingSQS, Kinesis, DynamoDB Streams, MSK, Amazon MQLambda keeps the batch until it succeeds or expiresDepends 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 ReportBatchItemFailures so 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

ConceptWhat to know
$LATESTThe mutable working copy. Never point production at it
VersionAn immutable snapshot of code and configuration
AliasA named pointer to one version, or a weighted split between two. This is how CodeDeploy runs canary and linear Lambda releases
LayersShared libraries and runtimes, up to five per function. They count toward the 250 MB unzipped limit
Container imageUp to 10 GB, built from a Lambda base image or a custom image that implements the runtime API
Architecturearm64 (Graviton) is cheaper per millisecond than x86 for most code
Function URLA 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

UnreservedReserved concurrencyProvisioned concurrency
What it doesShares the account poolGuarantees and caps a function's concurrencyKeeps environments initialized and ready
Cold startsYesYesNo, up to the provisioned amount
Extra costNoNoYes, while provisioned
Use it forMost functionsProtect a downstream DB, or stop one function starving othersLatency-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

VPCPrivate subnetsPublic subnetLambda(Hyperplane ENI)RDSNAT gatewayVPC endpointsInternetS3, DynamoDB,SQS
A VPC-attached Lambda uses a shared Hyperplane ENI. Reach the internet through a NAT gateway and AWS services through VPC endpoints.
  • 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

Scenario
A Lambda function consumes an SQS queue. When one message in a batch of ten is malformed, the whole batch returns to the queue and the nine good messages are processed again, several times. The team wants only the bad message retried and eventually isolated. What should they do?
Scenario · choose 2
A function in private subnets reads from Amazon RDS, calls a third-party HTTPS API, and writes to S3. The API calls time out, although the database works. The subnets have no route to the internet. Which TWO changes fix the API calls and keep S3 traffic private and cheap? Choose TWO.

Further reading

On this page