Serverless Containers: When and Why to Use AWS Fargate over Lambda

Sumit KumarSumit Kumar
4 min read

You’ve heard the hype: "Go serverless! Ditch servers! Scale infinitely with Lambda!" And it’s true—AWS Lambda revolutionized how we deploy event-driven code. But when I tried running my data-crunching Python script (you know, the one with 20 dependencies and a 30-minute runtime), Lambda ghosted me. Hard.
Here’s the secret: Serverless isn’t just about functions. Sometimes, you need the power of containers without the infrastructure headache. That’s where AWS Fargate enters the chat.
🔍 Why Lambda Hits a Wall (and When You’ll Feel It)

Lambda is brilliant for bite-sized tasks:

  • ⚡️ Blazing-fast triggers (e.g., resize an image on S3 upload)

  • 🚦 API Gateway backends handling quick requests

  • 🔔 Event-driven glue (SNS → DynamoDB updates)

❌ 15-minute timeout ceiling  
❌ Zip file dependency nightmares (looking at you, Python Pandas)  
❌ Cold starts haunting your latency-sensitive APIs  
❌ No control over networking or OS-level tuning

(Ask me about the time I tried running a machine learning inference in Lambda… 💀)
🐳 Enter Fargate: Your Container, Minus the Server Drama

Fargate is serverless containers. You package your app (Node.js, Java, Rust—anything!) into a Docker container. AWS runs it without you managing EC2 clusters.

Think of it like this:

🔸 Lambda = A single function on a shared runtime
🔸 Fargate = Your entire app environment, scaled and managed

Where Fargate shines:

  • ⏳ Tasks running >15 mins (hourly data syncs, video encoding)

  • 🧩 Complex apps needing custom runtimes/libraries

  • 🔒 Microservices demanding full VPC networking control

  • 📦 Legacy apps you’ve "Dockerized" but don’t want to re-architect

⚔️ The Showdown: Lambda vs. Fargate

Battle GroundLambdaFargate
Startup TimeCold starts possibleAlways warm (no cold starts!)
Max Runtime15 minutesUnlimited (run for days!)
PricingPer request + execution timePer vCPU/memory per second
ControlLimited env, no VPC tweaksFull OS/network customization
Language SupportOfficial runtimes + hacks"If it fits in Docker, it ships"

When to Choose Which? (Real Talk)

Reach for Lambda when:

  • You’re processing quick events (under 15 min).

  • Your app is stateless (no sticky sessions needed).

  • You’re prototyping an MVP yesterday.

  • Examples: Auth hooks, simple CRUD APIs, S3 file processing.

Switch to Fargate when:

  • Your task outgrows Lambda’s timeout (e.g., batch processing).

  • You’re tired of wrestling Lambda layers for niche dependencies.

  • You need predictable performance (goodbye cold starts!).

  • Examples: Web scrapers, long-report generation, stateful APIs, or any Dockerized app.

Cost & Performance: The Nerd Breakdown

  • Lambda wins for spiky, infrequent workloads. That free tier (1M requests/month!) is glorious for early-stage apps.

  • Fargate wins for steady/long tasks. Paying $0.04044 per vCPU-hour beats Lambda’s $0.00001667 per GB-second when your job runs 24/7.

Pro Tip: Use the AWS Pricing Calculator before committing. A 30-min daily batch job? Fargate saves 60% vs. Lambda.

Real-World Example: The URL Scraper That Broke Lambda

My client needed to scrape 500 product pages daily. Each run took 25 mins.

Lambda Approach:

def lambda_handler(event, context):  
    scrape_website()  # Fails at 15 mins 😭

Result: Timeout errors. Partial data. Tears.
Fargate Fix:

FROM python:3.9  
COPY requirements.txt .  
RUN pip install -r requirements.txt # BeautifulSoup, requests, etc.  
COPY scraper.py .  
CMD ["python", "scraper.py"]  # Runs happily for 25+ mins!

Result: Scheduled via CloudWatch Events → runs in Fargate → saves to S3. Zero infra management.
The Takeaway: It’s Not Lambda vs. Fargate—It’s Lambda and Fargate

They’re complementary tools:

  • Use Lambda for event-driven micro-tasks.

  • Use Fargate for heavy lifting and container freedom.

"Serverless" isn’t about functions—it’s about focusing on code, not servers. Fargate extends that promise to containers.

  1. Experiment: Run a Dockerized Flask app on Fargate (guide here).

  2. Optimize: Got a Lambda hitting timeouts? Try porting it to Fargate.

I’d love to hear your war stories!
Struggled with Lambda’s limits?
Migrated a monolith to Fargate?
Need a step-by-step deployment guide?

Let me know in the comments below! 👇

0
Subscribe to my newsletter

Read articles from Sumit Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sumit Kumar
Sumit Kumar