Configuring Memory, Timeouts and Quotas for Performance and Cost
This lesson explains how Lambda runs your code, which settings you control, such as memory and timeout, which fixed quotas you must design around, and how to tune a function for speed and cost.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the Init, Invoke and Shutdown phases of the execution environment and how they cause cold starts.
- Explain the effect of the memory, timeout, ephemeral storage and environment variable settings on a function.
- Distinguish the quotas that can be raised from the fixed limits that a design must work within.
- Identify memory and initialisation changes that reduce both duration and cost.
How Lambda runs your code
Lambda runs each function inside an execution environment: a small, isolated virtual machine that holds your code, the language runtime, the memory you configured and a temporary disk at /tmp. Lambda creates environments as requests arrive and reuses them while they stay busy.
| Stage | What happens |
|---|---|
| Init (cold start) | Lambda creates the environment, downloads your code, starts the runtime and runs any code outside your handler |
| Invoke | Lambda runs your handler for one request |
| Reuse (warm start) | The environment handles the next request straight away, skipping Init |
| Shutdown | After a period of inactivity the environment is removed, along with its memory and /tmp |
Three consequences shape how you write a function:
- Each environment handles one request at a time. Ten simultaneous requests run in ten environments, which is why concurrency equals the number of environments.
- Environments share nothing. Data in memory or
/tmpis visible only to later requests that land in the same environment. - Reuse is likely but never guaranteed. Treat anything left over from a previous request as a cache, not as stored state.
This is why SDK clients and connections belong outside the handler: they are created once during Init and reused by every warm invocation.
import os
import boto3
table = boto3.resource("dynamodb").Table(os.environ["TABLE_NAME"]) # runs once per cold start
def handler(event, context):
table.put_item(Item={"pk": event["orderId"], "status": "NEW"}) # runs on every invocation
return {"remaining_ms": context.get_remaining_time_in_millis()}Function settings
Lambda's limits fall into two groups. Function settings are values you choose for each function within an allowed range. Fixed quotas are set by AWS and cannot be raised, so a workload that exceeds one needs a different design.
Memory and CPU
Memory is the most important performance setting, because Lambda allocates CPU in proportion to it: 1,769 MB gives one full vCPU, and 10,240 MB gives up to six. A function with too little memory runs slowly or fails with an out-of-memory error. The REPORT line that each invocation writes to CloudWatch Logs shows Max Memory Used, so you can compare it with the memory size.
You pay for memory multiplied by duration, so extra memory often lowers the cost of CPU-bound work by finishing it sooner. To find the best size, test several values with the open-source AWS Lambda Power Tuning tool, and compare the results with the maximum memory used in the REPORT log lines.
Timeout
The timeout caps how long one invocation can run. The default is only 3 seconds and the maximum is 15 minutes. When the timeout is reached, Lambda stops the invocation and logs Task timed out after N seconds. This counts as a function error, so the usual retry rules apply.
Set the timeout a little above the function's normal duration, so a stuck call fails quickly instead of running, and costing, for minutes. Keep it in line with the services around the function: API Gateway waits 29 seconds by default, and an SQS queue's visibility timeout should be at least six times the function timeout. Work that needs more than 15 minutes belongs in Step Functions, ECS on Fargate or AWS Batch.
Temporary storage (/tmp)
Each execution environment has its own /tmp disk: 512 MB by default, included in the price, and up to 10,240 MB, charged above 512 MB. Files stay there between invocations in the same environment, which makes /tmp useful as a cache. They are lost when the environment shuts down and are never shared with other environments, so use Amazon S3 or mount Amazon EFS for permanent or shared files.
Environment variables
Environment variables pass configuration, such as a table name or log level, to your code without changing it. All keys and values together are limited to 4 KB, and this cannot be raised. Values are encrypted at rest with an AWS managed key or your own KMS key, but secrets are still better kept in Secrets Manager or Parameter Store.
Variables are saved with each published version, so changing one means publishing a new version. For configuration larger than 4 KB, use Parameter Store or AWS AppConfig.
Changing function settings
You can change function settings in three places:
- Console: open the function's Configuration tab. Memory, timeout and
/tmpstorage are under General configuration; variables are under Environment variables. - AWS CLI:
aws lambda update-function-configuration. - AWS SAM or CloudFormation: the
MemorySize,Timeout,EphemeralStorageandEnvironmentproperties.
The same settings with the CLI and in a SAM template:
aws lambda update-function-configuration --function-name orders-api \
--memory-size 1024 \
--timeout 30 \
--ephemeral-storage Size=2048 \
--environment "Variables={TABLE_NAME=orders,LOG_LEVEL=INFO}"OrdersFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.13
MemorySize: 1024 # MB
Timeout: 30 # seconds
EphemeralStorage:
Size: 2048 # MB of /tmp
Environment:
Variables:
TABLE_NAME: ordersFixed quotas
Deployment package size
Code is deployed as a .zip archive or as a container image:
| Format | Limit | Notes |
|---|---|---|
| .zip, zipped size | 50 MB | For direct upload through the console, CLI or API; upload larger archives from S3 |
| .zip, unzipped size | 250 MB | Includes all layers |
| Container image | 10 GB | Stored in Amazon ECR |
A package that is too large fails to deploy, with an error such as Unzipped size must be smaller than 262144000 bytes. Remove unused dependencies, or switch to a container image for large libraries such as machine learning frameworks. Layers do not help, because they count towards the 250 MB limit.
Payload size
The payload is the event sent to a function and the response it returns. Synchronous invocations allow 6 MB in each direction, and asynchronous invocations allow 1 MB (older exam questions may still quote 256 KB). Response streaming can return more than 6 MB. An oversized request is rejected with RequestTooLargeException (HTTP 413).
The fix is to pass large data by reference. Instead of sending a 20 MB report, the caller uploads it to S3 and sends {"bucket": "reports", "key": "2026/10/report.csv"}, and the function downloads the object itself.
Settings and fixed quotas
The following table summarises the function settings and fixed quotas described above.
| Limit | Type | Default | Range or maximum |
|---|---|---|---|
| Memory | Setting | 128 MB | 128 MB to 10,240 MB |
| Timeout | Setting | 3 seconds | 1 to 900 seconds (15 minutes) |
/tmp storage | Setting | 512 MB | 512 MB to 10,240 MB |
| Environment variables | Setting | None | 4 KB in total |
| Deployment package | Fixed quota | — | .zip: 50 MB zipped for direct upload, 250 MB unzipped. Container image: 10 GB |
| Payload | Fixed quota | — | Synchronous: 6 MB. Asynchronous: 1 MB |
Account-wide limits, such as concurrency, are covered in the lesson Managing Concurrency, Throttling and Cold Starts. You can see them, and request increases where allowed, in the Service Quotas console.
15min
Maximum function timeout
10,240MB
Maximum memory
6MB
Synchronous payload, in each direction
250MB
Unzipped .zip package, including layers
Performance and cost checklist
The performance and cost of a Lambda function depend largely on its configuration. The following checklist summarises the principal measures.
- Right-size memory instead of keeping the 128 MB default.
- Set the timeout just above the normal duration.
- Create SDK clients and connections outside the handler.
- Keep packages small, and use a container image for very large dependencies.
- Choose the arm64 (AWS Graviton) architecture for better price performance on most workloads.
- Pass large payloads and files through S3.
A function that times out under load
An image-processing function has 128 MB of memory and a 3-second timeout. It works in testing but times out in production on large images. The developer works through the options:
| Change | Effect |
|---|---|
| Raise memory to 512 MB | CPU rises in proportion to memory, so processing takes about 1 s instead of 4 s |
| Raise the timeout to 30 s | Stops timeouts but does not make the function faster |
| Move the S3 and DynamoDB clients outside the handler | Reused across invocations; avoids reconnecting each time |
Store temporary images in /tmp (raise ephemeral storage if needed) | Avoids memory pressure for large files |
Raising memory is the decisive change, and it need not cost more. Lambda bills GB-seconds: 128 MB for 4 s is 0.125 x 4 = 0.5 GB-seconds, and 512 MB for 1 s is 0.5 x 1 = 0.5 GB-seconds, the same price for a function that now finishes well within its timeout. Beyond the point where more CPU stops helping, extra memory only adds cost; Lambda Power Tuning finds that point by measurement.
A client sends a 15 MB file to a function in a synchronous call. Why does this fail, and what design avoids it?
Show answer
Synchronous payloads are limited to 6 MB, so the request is rejected with RequestTooLargeException. The client should upload the file to S3, for example with a presigned URL, and pass the bucket and key to the function.
Exam Tips
Remember for the exam
- A slow, CPU-bound function is fixed by increasing memory, which also increases CPU.
- The default timeout is 3 seconds and the maximum is 15 minutes; longer work needs Step Functions, ECS on Fargate or AWS Batch.
- Package size and payload size are fixed quotas: use a container image for large dependencies, and S3 for large payloads.
- Layers count towards the 250 MB unzipped limit, so they do not solve an oversized package.
- Environment variables are limited to 4 KB in total; larger configuration belongs in Parameter Store or AppConfig.
- Initialise SDK clients and connections outside the handler to speed up warm invocations.
- For better price performance, choose the arm64 (Graviton) architecture.