JSTAcademy
0 XP
Dashboard
Crash Courses
Compute — EC2, Lambda & Choosing the Right Model
17 min
Basic+170 XP
Crash Courses · Basic

Compute — EC2, Lambda & Choosing the Right Model

Virtual machines vs. serverless functions — when to use each and how both work
17 min read+170 XP on completionCert: AWS Crash Course
Tap any word in the text below to start reading from there.

Compute — EC2, Lambda & Choosing the Right Model

Two Mental Models of Compute

EC2: I need a server. Give me a virtual machine, I'll install what I need and keep it running.

Lambda: I have a function. Run it whenever this event happens, for as long as it takes, and charge me only for that execution.

Every other AWS compute option (ECS, Fargate, App Runner) lives between these two extremes.

EC2 in Depth

EC2 instances are virtual machines running on AWS hardware. You choose:

DecisionOptions
Instance typet3.micro, m5.large, c6g.xlarge — controls CPU/RAM
AMIAmazon Linux 2023, Ubuntu 22.04, Windows Server, etc.
StorageEBS volumes (persistent) or instance store (ephemeral)
NetworkWhich VPC, subnet, and security groups

Instance families:

  • t (burstable) — cheapest, earns CPU credits when idle, spends them on spikes. Good for dev environments, low-traffic apps.
  • m (general purpose) — balanced CPU/RAM. The default for most workloads.
  • c (compute optimized) — high CPU-to-RAM ratio. Good for batch processing, video encoding.
  • r (memory optimized) — high RAM. Good for in-memory caches, large databases.

Key EC2 concepts:

# Connect to an instance via SSH
ssh -i my-key.pem ec2-user@<public-ip>

# User data — bootstrap script that runs on first launch
#!/bin/bash
yum update -y
yum install -y nodejs
node -e "require('http').createServer((r,s)=>s.end('ok')).listen(3000)"

Security Groups are the primary network control for EC2:

Inbound rules:
  Port 22 (SSH)  — my IP only (0.0.0.0/0 is a security risk)
  Port 443 (HTTPS) — 0.0.0.0/0
  Port 80 (HTTP)   — 0.0.0.0/0

Outbound rules:
  All traffic — 0.0.0.0/0 (default, allows updates/API calls)

Lambda in Depth

Lambda functions are event-driven. AWS manages all infrastructure; you write the handler.

// A simple Lambda handler (Node.js 20.x runtime)
export const handler = async (event) => {
  console.log('Event:', JSON.stringify(event))

  return {
    statusCode: 200,
    body: JSON.stringify({ message: 'Hello from Lambda' }),
  }
}

Lambda limits to know:

  • Max execution time: 15 minutes
  • Memory: 128 MB – 10 GB (CPU scales with memory)
  • Deployment package: 50 MB zipped (or 250 MB via Lambda Layers)
  • Concurrent executions: 1,000 per region by default (soft limit, can increase)

Common Lambda triggers:

  • API Gateway → HTTP request
  • S3 → file uploaded/deleted
  • SQS → message on a queue
  • EventBridge → scheduled cron or custom event
  • DynamoDB Streams → database change

The Decision Framework

UseWhen
**Lambda**Short-lived, event-driven, unpredictable traffic, zero-ops desired
**EC2**Long-running processes, need full OS control, legacy apps, steady high traffic
**ECS/Fargate**Containerized apps, more control than Lambda, less ops than EC2
**App Runner**Web apps and APIs, fully managed, simpler than ECS

Cost intuition:

  • Lambda: ~$0.20 per 1M invocations + duration. Near-zero cost at low volume.
  • t3.micro: ~$0.0104/hr = ~$7.50/month. Cheaper at sustained high request volume.
  • The crossover is roughly 1–5M monthly invocations depending on duration.
0%