Difficulty: Intermediate
What is AWS Lambda, how does it run your code, and what are its key limits?
AWS Lambda is a serverless compute service that runs your function in response to events (an HTTP request through API Gateway, a file landing in S3, a message on SQS, a schedule) and bills only for the time your code runs, rounded to the millisecond. You do not manage servers or scaling. The key limits are a 15-minute maximum timeout, 128 MB to 10,240 MB of memory, and a default of 1,000 concurrent executions per Region per account.
Each environment handles one request at a time. Ten simultaneous requests need ten environments, which is what "concurrency" means.
| Limit | Value |
|---|---|
| Timeout | Up to 900 seconds (15 minutes) |
| Memory | 128 MB to 10,240 MB; CPU scales with memory (up to 6 vCPUs) |
Ephemeral /tmp storage | 512 MB default, up to 10,240 MB |
| Deployment package | 50 MB zipped (direct upload), 250 MB unzipped including layers; container images up to 10 GB |
| Synchronous payload | 6 MB request and 6 MB response |
| Concurrency | 1,000 per Region by default (a soft quota you can raise) |
| Environment variables | 4 KB total |
Cold starts add latency, typically from under a hundred milliseconds for small Node.js or Python functions to a second or more for heavy Java or .NET apps. Ways to reduce them:
Good fits: APIs with uneven traffic, event processing (thumbnail generation on S3 upload, processing UPI payment webhooks), scheduled jobs, glue between AWS services.
Poor fits: jobs longer than 15 minutes (use Fargate, Step Functions, or Batch), workloads needing persistent connections or local state, steady high throughput where an always-on container is cheaper, and anything needing a GPU.
import json
import boto3
# Runs once per execution environment (cold start), reused when warm
s3 = boto3.client("s3")
def handler(event, context):
# Triggered by an S3 ObjectCreated event
for record in event["Records"]:
bucket = record["s3"]["bucket"]["name"]
key = record["s3"]["object"]["key"]
head = s3.head_object(Bucket=bucket, Key=key)
print(f"{key}: {head['ContentLength']} bytes")
print("Time left (ms):", context.get_remaining_time_in_millis())
return {"statusCode": 200, "body": json.dumps({"ok": True})}
Creating the boto3 client at module level means only the cold start pays for it. context.get_remaining_time_in_millis() helps long tasks stop cleanly before the timeout.
aws lambda update-function-configuration \
--function-name thumbnailer \
--memory-size 1024 --timeout 60
# Provisioned concurrency must target a version or alias
aws lambda put-provisioned-concurrency-config \
--function-name checkout-api --qualifier live \
--provisioned-concurrent-executions 20
Raising memory also raises CPU. Provisioned concurrency keeps 20 environments warm on the 'live' alias for latency-sensitive endpoints.
AWS Lambda, Cold Start, Concurrency, Provisioned Concurrency, Execution Limits