![AWS Compute services for AI loads [Upcoming...]](/_ipx/_/images/devops_automation.jpeg)
AWS Compute services for AI loads [Upcoming...]
Lambda, ECS, EKS? A practical guide on when to use each service and what needs to change.
Andrés Renaud
Comprehensive Comparison: AWS Lambda vs. Amazon ECS (Duration, Costs, and Architectures)
This document synthesizes the operational, financial, and architectural differences between AWS Lambda and Amazon ECS, tailored for scaling decisions, demo environments, and handling API timeouts.
1. Core Duration & Operational Limits
The foundational choice between AWS Lambda and Amazon ECS is governed by their hard physical and architectural constraints.
| Metric / Feature | AWS Lambda | Amazon ECS (EC2 or Fargate) |
|---|---|---|
| Maximum Duration | 15 minutes (900 seconds) (Hard limit) | Unlimited (Can run 24/7 indefinitely) |
| Minimum Configurable Timeout | 1 second | None (Controlled entirely by app logic) |
| Default Timeout | 3 seconds | None |
| API Gateway Integration Limit | 29 seconds (Hard limit for HTTP) | Unlimited (ALB handles long-lived requests) |
| Memory Ceiling | Up to 10 GB | Highly customizable (up to hundreds of GBs on EC2) |
| Compute Ceiling | Up to 6 vCPUs | Highly customizable |
| Billing Model | Strict pay-per-millisecond of execution | Billed by provisioned capacity (even if idle) |
| Infrastructure Overhead | Completely serverless (No cluster management) | Requires clusters, task definitions, and VPCs |
2. The Financial Crossover Point (Lambda vs. ECS Fargate)
AWS Lambda charges a premium for flexibility and its ability to scale down to zero. Amazon ECS Fargate provides cheaper raw compute capacity per unit of time but requires continuous provisioning.
The 300-Hour Rule
Assuming a standard matching resource profile (1 vCPU and 2 GB of RAM):
- AWS Lambda Compute: ~$0.00003334 per second
- AWS ECS Fargate: ~$0.00001371 per second
- The 40% Utilization Rule: ECS Fargate is roughly 2x to 2.5x cheaper per second than Lambda. If your accumulated tasks run for more than ~300 hours a month (approx. 40% of a standard 730-hour month), the flat rate of a 24/7 Fargate container becomes cheaper than the accumulated Lambda bill.
Monthly Volume Scenarios (1-Minute Executions)
The optimal service choice shifts drastically as total monthly volume scales:
- 1,000 executions/month (~16.6 hours total compute):
- Lambda: ~$2.00 | ECS: ~$36.00 (24/7)
- Winner: AWS Lambda. ECS is highly wasteful here as you pay for 97.7% idle capacity.
- 10,000 executions/month (~166.6 hours total compute):
- Lambda: ~$20.00 | ECS: ~$36.00 (24/7)
- Winner: AWS Lambda. Your runtime is still safely under the 300-hour crossover mark.
- 100,000 executions/month (~1,666.6 hours total compute):
- Lambda: ~$200.00 | ECS: ~$36.00 to $72.00 (depending on parallel tasks)
- Winner: Amazon ECS. Total runtime exceeds the hours available in a single month, meaning tasks overlap concurrently. Lambda's millisecond premium becomes a massive financial penalty.
Traffic Pattern Variables
Before switching to ECS at high volumes, check how traffic arrives:
- The Massive Spike: If 100,000 tasks hit simultaneously once a month, use Lambda for instant concurrency. ECS containers take 30–60 seconds to boot up and will fail to handle instant walls of traffic.
- The Steady Stream: If tasks are distributed evenly across the month, ECS is the undisputed winner.
3. Demo Environment Optimization
For Demos, Proofs of Concept (PoC), or Dev environments, developer velocity and "scale-to-zero" cost structures override production math. AWS Lambda is highly recommended for demos, even if tasks have a long 10-minute runtime a few hundred times a month.
Why Lambda Wins for Demos
- The Cost of Scale-to-Zero: A demo running 300 times a month for 10 minutes totals 50 hours of execution. Lambda only bills for those 50 hours. ECS requires a container to run 24/7 to ensure it works whenever a stakeholder opens it, wasting 93% of the budget on idle runtime.
- The "Sleeping Client" Cold Start Problem: Turning ECS containers off to save money causes a 30-to-60 second boot delay when a user triggers it. This lag looks like a system failure to a client. Lambda boots instantly (under 2 seconds).
- No Infrastructure Maintenance: Lambda avoids the complex setup of VPCs, Application Load Balancers (ALBs), security groups, and target groups.
4. Bypassing the 29-Second API Gateway Timeout
When pairing API Gateway with a long-running Lambda function (e.g., a 10-minute demo task), you will hit API Gateway's hard 29-second integration timeout. To bypass this, convert your synchronous request into an asynchronous decoupled pattern.
User Browser ---> (API Gateway) ---> Instant 202 Response ---> User browser polls for status │ ▼ Asynchronous Hand-off │ ▼ (Lambda / SQS Queue) ---> Runs for up to 15 mins ---> Saves Output to S3/DynamoDB
Architectural Implementation Options
Pattern 1: Asynchronous Lambda Invocation
Configure API Gateway to pass an HTTP header X-Amz-Invocation-Type: Event.
- API Gateway triggers the Lambda and immediately returns a 202 Accepted status code to the client browser in under 100ms.
- The browser drops the active connection while the Lambda function continues running seamlessly in the background for its full 10-minute duration.
Pattern 2: The Queue Broker Pattern
Configure API Gateway to act as a direct proxy to Amazon SQS (Simple Queue Service).
- The API drops the payload into an SQS queue and immediately informs the browser that the task was successfully queued.
- The long-running Lambda function polls the SQS queue and processes tasks at its own pace, preventing system failure if multiple users access the demo concurrently.
Delivering the Final Results to the User
Because the initial connection is broken immediately, use one of these two methods to show results to the user after the 10-minute execution finishes:
- Method A: The Database Polling Strategy (Easiest): The background Lambda writes its final output to an Amazon DynamoDB table or an Amazon S3 bucket when finished. The frontend application uses a standard lightweight API call to check (poll) that storage layer every 10–20 seconds, updating the UI once the status changes to complete.
- Method B: WebSocket API Gateway: Use API Gateway WebSockets to maintain a persistent, bidirectional communication pipe. The frontend initiates the request, closes the HTTP cycle, and the background Lambda pushes the final output back through the open WebSocket channel to update the screen instantly.