OnCallReadyStart learning

AWS Lesson 39.5 · AWS VI: Architecting on AWS & the exams · 21 min read

Lambda: functions, cold starts, the REPORT line and concurrency

In plain words

Lambda is like a taxi rank instead of owning a car. You do not maintain anything; when someone needs a ride a taxi appears, drives, and you pay for the minutes. The first ride of the morning takes longer because a driver has to arrive and start the engine - that is a cold start. A rank only has so many taxis, so if everyone wants one at once some people wait; you can also reserve taxis for one customer, which means nobody else can use them.

Lambda: functions, cold starts, the REPORT line and concurrency

AWS Lambda runs your function when something asks for it and bills the milliseconds it runs. There is no server to patch and no capacity to plan - but there is still an operating system, a runtime, memory, a time limit and a limit on how many copies run at once. Those are what break in production, and every one of them shows up in two places you will read a lot: the function's configuration and the REPORT line at the end of each invocation in CloudWatch Logs.

Need to know: a function = a runtime (nodejs22.x, python3.13 ...), a handler (index.handler = the export handler of index.mjs), a deployment package (a zip, or a container image), an execution role Lambda assumes to call AWS (and to write its logs), a memory size from 128 MB to 10,240 MB - CPU is given in proportion, a full vCPU at 1,769 MB - and a timeout of up to 15 minutes (3 seconds by default). Each copy runs in an execution environment: created on demand (a cold start: download the code, start the runtime, run your module's top-level code), then reused for later invocations while it stays warm.

Deploy a function

greet/index.mjs is a small handler. Package it and create the function with the role try-fn-role (it trusts lambda.amazonaws.com and has AWSLambdaBasicExecutionRole, which is what lets it write logs):

$ cd ~/oncall-lab/labs/aws/try6/greet
$ cat index.mjs
export const handler = async (event, context) => {
  console.log('order', event.order, 'remaining ms', context.getRemainingTimeInMillis());
  if (event.sleep) await new Promise((r) => setTimeout(r, event.sleep));
  return { ok: true, order: event.order };
};
$ zip function.zip index.mjs
  adding: index.mjs (deflated 35%)
$ aws lambda create-function --function-name try-greet --runtime nodejs22.x --handler index.handler --role arn:aws:iam::111122223333:role/try-fn-role --zip-file fileb://function.zip --query '[FunctionName,State,LastUpdateStatus,MemorySize,Timeout]' --output text
try-greet	Pending	InProgress	128	3
$ aws lambda invoke --function-name try-greet --payload '{"order":1}' out.json
aws: [ERROR]: An error occurred (ResourceConflictException) when calling the Invoke operation: The operation cannot be performed at this time. The function is currently in the following state: Pending

Additional error details:
Type: User

A new function is Pending for a few seconds, and invoking it then fails - in a pipeline, wait for it first. --payload is plain JSON here because the profile sets cli_binary_format = raw-in-base64-out (CLI v2 expects base64 otherwise):

$ aws lambda wait function-active-v2 --function-name try-greet
$ aws lambda invoke --function-name try-greet --payload '{"order":1}' out.json && cat out.json; echo
{
    "StatusCode": 200,
    "ExecutedVersion": "$LATEST"
}
{"ok":true,"order":1}
$ aws lambda invoke --function-name try-greet --payload '{"order":2}' out.json >/dev/null && cat out.json; echo
{"ok":true,"order":2}

The log and the REPORT line

Every invocation writes START, your own output, END and REPORT to the log group /aws/lambda/<function>, one log stream per execution environment:

$ aws logs tail /aws/lambda/try-greet --format short
2026-09-22T20:00:07 INIT_START Runtime Version: nodejs:22.v51	Runtime Version ARN: arn:aws:lambda:eu-central-1::runtime:f957222d085657558b65a1503c7fa8cfb6b01756763f8d13be5aa2890e347515
2026-09-22T20:00:07 START RequestId: 6a4d1899-f849-4115-916e-172956cf4183 Version: $LATEST
2026-09-22T20:00:07 2026-09-22T20:00:07.648Z	6a4d1899-f849-4115-916e-172956cf4183	INFO	order 1 remaining ms 3000
2026-09-22T20:00:07 END RequestId: 6a4d1899-f849-4115-916e-172956cf4183
2026-09-22T20:00:07 REPORT RequestId: 6a4d1899-f849-4115-916e-172956cf4183	Duration: 0.90 ms	Billed Duration: 150 ms	Memory Size: 128 MB	Max Memory Used: 79 MB	Init Duration: 148.33 ms
2026-09-22T20:00:07 START RequestId: 7ccda42b-887a-426b-aa26-7798ce6ce942 Version: $LATEST
2026-09-22T20:00:07 2026-09-22T20:00:07.700Z	7ccda42b-887a-426b-aa26-7798ce6ce942	INFO	order 2 remaining ms 3000
2026-09-22T20:00:07 END RequestId: 7ccda42b-887a-426b-aa26-7798ce6ce942
2026-09-22T20:00:07 REPORT RequestId: 7ccda42b-887a-426b-aa26-7798ce6ce942	Duration: 0.90 ms	Billed Duration: 1 ms	Memory Size: 128 MB	Max Memory Used: 79 MB

Read the REPORT line field by field:

FieldMeaning
Durationhow long the handler ran
Billed Durationwhat you pay for: the duration rounded up to the next millisecond, plus the Init Duration on a cold start (managed runtimes, since August 2025)
Memory Sizethe configured memory
Max Memory Usedthe peak of this environment; close to Memory Size = an out-of-memory crash is near
Init Durationonly on a cold start: starting the runtime and running the module's top-level code
Status: timeout / erroronly when the invocation did not end normally

The first invocation had an INIT_START line and an Init Duration; the second reused the warm environment (same log stream) and had neither. Expensive setup - SDK clients, configuration, database connections - belongs at the top level of the module, so it runs once per environment, not once per request.

Timeouts and errors

The handler sleeps when the event says so. Ask it to sleep 5 seconds against its 3-second timeout:

$ aws lambda invoke --function-name try-greet --payload '{"order":3,"sleep":5000}' out.json; cat out.json; echo
{
    "StatusCode": 200,
    "FunctionError": "Unhandled",
    "ExecutedVersion": "$LATEST"
}
{"errorType":"Sandbox.Timedout","errorMessage":"RequestId: 91564798-e11d-494e-b394-85376b25ee81 Error: Task timed out after 3.00 seconds"}
$ aws logs tail /aws/lambda/try-greet --since 1m --format short | tail -3
2026-09-22T20:00:10 2026-09-22T20:00:10.900Z 91564798-e11d-494e-b394-85376b25ee81 Task timed out after 3.00 seconds
2026-09-22T20:00:10 END RequestId: 91564798-e11d-494e-b394-85376b25ee81
2026-09-22T20:00:10 REPORT RequestId: 91564798-e11d-494e-b394-85376b25ee81	Duration: 3000.00 ms	Billed Duration: 3000 ms	Memory Size: 128 MB	Max Memory Used: 79 MB	Status: timeout

The CLI exit code is 0: the invoke worked, the function failed. A failed invocation is "FunctionError": "Unhandled" in the response with the error in the payload - scripts must check it. A timeout also throws the environment away, so the next call is a cold start again. Raise memory and timeout with one configuration update and wait for it to apply:

$ aws lambda update-function-configuration --function-name try-greet --memory-size 512 --timeout 10 --query '[MemorySize,Timeout,LastUpdateStatus]' --output text
512	10	InProgress
$ aws lambda wait function-updated-v2 --function-name try-greet
$ aws lambda invoke --function-name try-greet --payload '{"order":4,"sleep":5000}' out.json && cat out.json; echo
{
    "StatusCode": 200,
    "ExecutedVersion": "$LATEST"
}
{"ok":true,"order":4}

More memory is also more CPU, so CPU-bound functions often finish enough faster at 512 or 1,024 MB to cost the same or less - the trade-off AWS Lambda Power Tuning (an open-source Step Functions tool) measures, and Compute Optimizer recommends memory sizes for functions from their history.

Three ways to invoke

  • Synchronous (RequestResponse): the caller waits for the result - the CLI, API Gateway, an ALB, a function URL. Errors go back to the caller; retries are the caller's job.
  • Asynchronous (Event): Lambda queues the event, answers 202 at once, and runs it; on an error it retries twice (after about 1 and 2 minutes), then sends the event to an on-failure destination or a DLQ - S3 notifications, SNS, EventBridge use this.
  • Event source mapping (poll-based): Lambda reads SQS, Kinesis, DynamoDB streams (and Kafka, MQ) for you and invokes with batches - the next lesson.
$ aws lambda invoke --function-name try-greet --invocation-type Event --payload '{"order":5}' /dev/null
{
    "StatusCode": 202
}
$ aws lambda invoke --function-name try-greet --invocation-type DryRun /dev/null
{
    "StatusCode": 204
}

Concurrency and throttling

Concurrency is the number of invocations running at the same moment - roughly requests per second x average duration in seconds (100 requests/s at 200 ms = 20). An account has a Regional pool (1,000 by default, raised on request), and each function scales by up to 1,000 new environments every 10 seconds. Two settings carve the pool:

  • Reserved concurrency - guarantees a function that many and also caps it there. 0 stops it entirely (the emergency off switch). At least 100 must stay unreserved for the rest.
  • Provisioned concurrency - environments initialized in advance on a version or alias: no cold starts for latency-critical APIs, billed while they exist.
$ aws lambda get-account-settings --query AccountLimit
{
    "TotalCodeSize": 80530636800,
    "CodeSizeUnzipped": 262144000,
    "CodeSizeZipped": 52428800,
    "ConcurrentExecutions": 1000,
    "UnreservedConcurrentExecutions": 1000
}
$ aws lambda put-function-concurrency --function-name try-greet --reserved-concurrent-executions 0
{
    "ReservedConcurrentExecutions": 0
}
$ aws lambda invoke --function-name try-greet --payload '{"order":6}' out.json
aws: [ERROR]: An error occurred (TooManyRequestsException) when calling the Invoke operation (reached max retries: 2): Rate Exceeded.

Additional error details:
Reason: ReservedFunctionConcurrentInvocationLimitExceeded
Type: User
$ aws lambda delete-function-concurrency --function-name try-greet

A throttled synchronous call fails with TooManyRequestsException (429) - the caller must retry with back-off; API Gateway turns it into an error for the client. A throttled asynchronous event is kept and retried for up to 6 hours; a throttled SQS batch just stays in the queue. The Throttles metric counts them all; ConcurrentExecutions shows how close you are.

On the exam (SAA-C03 3.3 "high-performing compute", 2.1; SOA-C03 1.3): memory = CPU, timeout up to 15 minutes (longer work = Fargate, Batch or Step Functions), cold starts and provisioned concurrency, reserved concurrency to protect a downstream database or as an off switch, synchronous vs asynchronous invocation, and the REPORT line's fields.

In an interview: "How do you reduce Lambda cold starts?" - "Do the expensive setup once at module level, keep the package and its dependencies small, pick a fast-starting runtime, give it enough memory, and for latency-critical paths use provisioned concurrency on an alias (or SnapStart for Java). Then confirm with Init Duration in the REPORT lines."

You can now: package and create a function with an execution role, wait for Active, invoke it and check FunctionError, read START / END / REPORT and spot cold starts, size memory and timeout, choose between synchronous, asynchronous and poll-based invocation, and use reserved and provisioned concurrency - including as an off switch.

Why it helps

Lambda removes servers but not operations: functions still have memory, timeouts, cold starts and concurrency limits, and those are what break in production. Reading the REPORT line, choosing memory (which also buys CPU), keeping setup at module level, and understanding reserved and provisioned concurrency are the daily skills of anyone running serverless systems - and they are tested directly on both Associate exams.

Commands in this lesson

cd cat zip aws

FAQ

Why does more memory sometimes make a function cheaper?

Lambda allocates CPU in proportion to memory, with a full vCPU at 1,769 MB. A CPU-bound function at a higher memory setting can finish so much faster that GB-seconds - memory times duration - go down. Measure it: run the function at several sizes (AWS Lambda Power Tuning automates this) and compare duration and cost.

What is in the REPORT line?

Duration (how long the handler ran), Billed Duration (rounded up, plus the init time on a cold start for managed runtimes), Memory Size, Max Memory Used, Init Duration on a cold start, and a Status when the invocation timed out or failed. It is the single most useful line for performance and cost questions about a function.

Does a FunctionError fail the CLI command?

No. The invoke call succeeded - Lambda ran the function - so the CLI exits 0, but the response contains FunctionError: Unhandled and the payload holds the error. Scripts and pipelines that invoke functions must check that field instead of trusting the exit code.

How long can a Lambda function run?

Up to 15 minutes per invocation, 3 seconds by default. Work that may take longer belongs in ECS on Fargate, AWS Batch or a Step Functions workflow that splits it into steps. A function that hits its timeout is stopped, logs Task timed out, and its execution environment is thrown away.

What is the difference between reserved and provisioned concurrency?

Reserved concurrency sets aside part of the account's concurrency for one function and caps it there; setting it to 0 stops all invocations. Provisioned concurrency keeps a number of execution environments initialized in advance on a version or alias, so those invocations have no cold start; it is billed while configured.

In an interview Mid

How do you reduce Lambda cold starts, and how do you see them?

A cold start creates a new execution environment and runs the module's top-level code; the logs show INIT_START and an Init Duration on that invocation's REPORT line. I keep the package and dependencies small, create SDK clients and connections at module level so warm invocations reuse them, give the function enough memory, and use provisioned concurrency on an alias for latency-critical paths. Then I compare Init Duration before and after.

Also asked: What happens when a Lambda function is throttled? · When would you set reserved concurrency to zero? · How does memory affect a function's cost and speed?

Practise this lesson in the terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.