Function Timeout and Memory Limits
What you'll learn: How setting proper resource constraints on serverless functions prevents attackers from exploiting runaway executions, causing denial-of-service, or generating massive cloud bills.
Why Resource Limits Matter in Serverless Security
Serverless functions can be triggered by external events—HTTP requests, database changes, file uploads. Without proper limits, a malicious actor could:
- Trigger computationally expensive operations that run indefinitely
- Launch thousands of concurrent function invocations
- Drive up your AWS/Azure/GCP bill into thousands of dollars overnight
- Exhaust quotas and prevent legitimate functions from running
Think of it like this: A function without timeout is like a taxi meter that never stops running—even after you've reached your destination, the fare keeps climbing.
Timeout Limits
Every serverless function should have a maximum execution timeout. If your function normally processes an image in 5 seconds, set the timeout to 10 seconds. This prevents:
- Infinite loops from buggy or malicious code
- Slowloris-style attacks that keep connections open
- Unintended recursive calls that never terminate
Typical values: 3-30 seconds for APIs, up to 15 minutes for batch jobs (AWS Lambda max).
Memory Limits
Memory allocation directly affects:
- Cost: Higher memory = higher per-second billing
- CPU allocation: Cloud providers tie CPU power to memory settings
- Attack surface: Excessive memory allows data exfiltration or cryptomining to run longer
Set memory just above what your function needs (e.g., 256 MB for simple APIs, 1-2 GB for data processing). Monitor actual usage and adjust.
The Defense-in-Depth Approach
Combine limits with other controls you've learned: least-privilege execution roles (lesson 1951), VPC isolation (1954), and monitoring (1947). An attacker exploiting an injection vulnerability will hit resource limits before causing significant damage.
Key Takeaway: Always configure explicit timeout and memory limits on serverless functions—they're your safety valves against resource exhaustion attacks, runaway costs, and denial-of-service conditions.