Compute
AWS offers several compute options depending on how much control you need over the underlying infrastructure:
| Service | Model | You Manage | AWS Manages |
|---|---|---|---|
| EC2 | Virtual machines | OS, runtime, app, scaling | Hardware, hypervisor |
| Lambda | Serverless functions | Code only | Everything else |
| ECS | Container orchestration | Task definitions, app | Cluster (with Fargate) |
| EKS | Managed Kubernetes | Pods, Deployments | Control plane |
For creating an EKS cluster with Terraform (VPC, private API, managed node groups), see Kubernetes → EKS.
EC2 (Elastic Compute Cloud)
Section titled “EC2 (Elastic Compute Cloud)”EC2 gives you virtual machines (called instances) that you can configure with any OS, software, and settings.
Instance Types
Section titled “Instance Types”Instance types are named like m5.xlarge:
m 5 . xlarge │ │ │ │ │ └─ Size (nano → micro → small → medium → large → xlarge → 2xlarge ...) │ └─ Generation (higher = newer hardware) └─ Family| Family | Optimized For | Example Use Case |
|---|---|---|
| t (burstable) | General purpose, baseline + burst | Dev/test, small apps, microservices |
| m (general) | Balanced compute/memory/network | Web servers, application servers |
| c (compute) | CPU-intensive workloads | Batch processing, encoding, scientific modeling |
| r (memory) | Memory-intensive workloads | In-memory databases, caches, analytics |
| g/p (accelerated) | GPU workloads | ML training, graphics rendering |
| i (storage) | High I/O, local NVMe storage | Databases, data warehousing |
Launching an Instance
Section titled “Launching an Instance”# Launch a t3.micro instance with Amazon Linux 2023aws ec2 run-instances \ --image-id ami-0abcdef1234567890 \ # AMI (operating system image) --instance-type t3.micro \ --key-name my-key-pair \ # SSH key --security-group-ids sg-0abc123 \ # Firewall rules --subnet-id subnet-0abc123 \ # Which subnet/AZ --iam-instance-profile Name=MyEC2Profile # IAM role| Parameter | What It Is |
|---|---|
| AMI | Amazon Machine Image — the OS template (Amazon Linux, Ubuntu, Windows, custom) |
| Key pair | SSH key for remote access (.pem file). Create once, reuse across instances. |
| Security group | Firewall rules (inbound/outbound). Covered in Networking. |
| Subnet | Network placement within your VPC. Determines the AZ. |
| Instance profile | IAM role attached to the instance (no access keys needed). |
Instance Lifecycle
Section titled “Instance Lifecycle”pending → running → stopping → stopped → terminated ↘ shutting-down → terminated- Running — billed per second (minimum 60 seconds).
- Stopped — no compute charge, but EBS volumes still billed.
- Terminated — gone (EBS root volume deleted by default).
Pricing Models
Section titled “Pricing Models”| Model | Discount | Commitment | Best For |
|---|---|---|---|
| On-demand | None (full price) | None | Short-term, unpredictable workloads |
| Reserved | Up to 72% | 1 or 3 years | Steady-state, predictable workloads |
| Savings Plans | Up to 72% | 1 or 3 years (flexible) | Flexible commitment (can change instance type) |
| Spot | Up to 90% | None (can be interrupted) | Fault-tolerant batch jobs, CI/CD runners |
| Dedicated Hosts | Varies | Optional | Compliance (licensing, regulatory) |
User Data (Bootstrap Scripts)
Section titled “User Data (Bootstrap Scripts)”Run a script when the instance first boots:
#!/bin/bashyum update -yyum install -y httpdsystemctl start httpdecho "<h1>Hello from $(hostname)</h1>" > /var/www/html/index.htmlPass this as --user-data file://bootstrap.sh when launching.
Key EC2 Features
Section titled “Key EC2 Features”| Feature | What It Does |
|---|---|
| Elastic IP | A static public IP address you can attach/detach from instances |
| Placement groups | Control instance placement (cluster for low latency, spread for HA) |
| Auto Scaling | Automatically add/remove instances based on demand (CPU, request count, schedule) |
| Launch templates | Versioned templates for instance configuration (replaces launch configs) |
| AMI | Snapshot your configured instance as a custom image for reuse |
Lambda (Serverless)
Section titled “Lambda (Serverless)”Lambda runs your code in response to events — no servers to provision, patch, or scale. You pay only for the compute time consumed.
How Lambda Works
Section titled “How Lambda Works”Event source ──► Lambda function ──► Output(API Gateway, (your code) (response, S3, SQS, write to DB, schedule) send to SQS)- An event triggers the function (HTTP request, file upload, message, cron schedule).
- Lambda creates an execution environment (or reuses a warm one).
- Your code runs and returns a response.
- You’re billed for the duration (in 1ms increments) × memory allocated.
Creating a Lambda Function
Section titled “Creating a Lambda Function”import json
def lambda_handler(event, context): name = event.get('queryStringParameters', {}).get('name', 'World') return { 'statusCode': 200, 'body': json.dumps({'message': f'Hello, {name}!'}) }# Deploy (zip approach)zip function.zip lambda_function.pyaws lambda create-function \ --function-name hello \ --runtime python3.12 \ --handler lambda_function.lambda_handler \ --zip-file fileb://function.zip \ --role arn:aws:iam::123456789012:role/LambdaExecutionRole
# Invokeaws lambda invoke --function-name hello \ --payload '{"queryStringParameters":{"name":"Alice"}}' output.jsonCommon Event Sources
Section titled “Common Event Sources”| Source | Use Case |
|---|---|
| API Gateway | HTTP APIs (REST, WebSocket) |
| S3 | Process file uploads (resize images, parse CSVs) |
| SQS | Process queue messages |
| DynamoDB Streams | React to database changes |
| EventBridge (schedule) | Cron jobs (rate(1 hour), cron(0 9 * * ? *)) |
| SNS | Fan-out notifications |
| Kinesis | Stream processing |
Lambda Limits
Section titled “Lambda Limits”| Limit | Value |
|---|---|
| Timeout | Max 15 minutes |
| Memory | 128 MB – 10,240 MB |
| Package size | 50 MB (zip), 250 MB (unzipped), 10 GB (container image) |
| Concurrent executions | 1,000 per region (soft limit, can be increased) |
/tmp storage | 512 MB – 10,240 MB |
Lambda Best Practices
Section titled “Lambda Best Practices”- Keep functions small and focused — one function per task.
- Minimize cold starts — use provisioned concurrency for latency-sensitive functions, or choose lighter runtimes (Python, Node.js).
- Use environment variables for configuration (not hardcoded values).
- Use layers for shared libraries.
- Set appropriate memory — more memory = more CPU = faster execution (may cost less overall).
ECS (Elastic Container Service)
Section titled “ECS (Elastic Container Service)”ECS runs Docker containers on AWS. You define task definitions (like a Kubernetes pod spec) and ECS handles placement and scaling.
Launch Types
Section titled “Launch Types”| Launch Type | You Manage | AWS Manages | Best For |
|---|---|---|---|
| Fargate | Task definitions, app | Infrastructure (no EC2 instances) | Simplicity, most use cases |
| EC2 | EC2 instances + tasks | Orchestration | Full control, GPU workloads, cost optimization at scale |
ECS Concepts
Section titled “ECS Concepts”┌─────────────────────────────────────┐│ ECS Cluster ││ ┌─────────────────────────────┐ ││ │ Service (desired: 3) │ ││ │ ┌──────┐ ┌──────┐ ┌──────┐│ ││ │ │ Task │ │ Task │ │ Task ││ ││ │ │(cont-│ │(cont-│ │(cont-││ ││ │ │ainer)│ │ainer)│ │ainer)││ ││ │ └──────┘ └──────┘ └──────┘│ ││ └─────────────────────────────┘ │└─────────────────────────────────────┘| Concept | What It Is |
|---|---|
| Cluster | Logical grouping of tasks/services |
| Task definition | Blueprint for a container (image, CPU, memory, ports, env vars, IAM role) |
| Task | A running instance of a task definition (like a Kubernetes pod) |
| Service | Maintains a desired count of tasks, integrates with load balancers, handles rolling updates |
Task Definition Example
Section titled “Task Definition Example”{ "family": "my-app", "networkMode": "awsvpc", "requiresCompatibilities": ["FARGATE"], "cpu": "256", "memory": "512", "containerDefinitions": [{ "name": "app", "image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:latest", "portMappings": [{"containerPort": 8080}], "environment": [ {"name": "DB_HOST", "value": "mydb.cluster-xyz.us-east-1.rds.amazonaws.com"} ], "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group": "/ecs/my-app", "awslogs-region": "us-east-1", "awslogs-stream-prefix": "ecs" } } }]}EKS (Elastic Kubernetes Service)
Section titled “EKS (Elastic Kubernetes Service)”EKS is AWS’s managed Kubernetes service. AWS runs the control plane (API server, etcd, scheduler); you manage the worker nodes and your Kubernetes workloads.
EKS vs ECS
Section titled “EKS vs ECS”| ECS | EKS | |
|---|---|---|
| Orchestrator | AWS-proprietary | Kubernetes (open standard) |
| Learning curve | Lower (AWS-native concepts) | Higher (Kubernetes concepts) |
| Portability | AWS only | Multi-cloud, on-prem |
| Ecosystem | AWS tooling | Huge K8s ecosystem (Helm, Istio, Argo, etc.) |
| Best for | Simpler container workloads, AWS-only shops | Teams already using K8s, multi-cloud strategy |
Node Types
Section titled “Node Types”| Node Type | Description |
|---|---|
| Managed node groups | AWS manages EC2 instances (patching, scaling). You choose instance type. |
| Fargate | Serverless — no EC2. Each pod runs in its own micro-VM. |
| Self-managed | You manage the EC2 instances entirely (most control, most effort). |
Quick Start
Section titled “Quick Start”# Create cluster (using eksctl — the official CLI tool)eksctl create cluster \ --name my-cluster \ --region us-east-1 \ --nodegroup-name workers \ --node-type t3.medium \ --nodes 3
# Update kubeconfigaws eks update-kubeconfig --name my-cluster --region us-east-1
# Now use kubectl as normalkubectl get nodeskubectl apply -f deployment.yamlChoosing a Compute Service
Section titled “Choosing a Compute Service”| Workload | Recommended Service |
|---|---|
| Traditional web server, full OS control | EC2 |
| Event-driven, short-lived tasks (< 15 min) | Lambda |
| Containerized app, simple orchestration | ECS Fargate |
| Containerized app, Kubernetes ecosystem needed | EKS |
| Batch processing, fault-tolerant | EC2 Spot or Lambda |
| GPU / ML training | EC2 (p/g instances) or SageMaker |
Key Takeaways
Section titled “Key Takeaways”- EC2 gives you full control over virtual machines — choose instance type, OS, pricing model (on-demand, reserved, spot).
- Lambda is serverless — no servers to manage, pay per invocation, max 15 minutes per execution.
- ECS runs Docker containers with Fargate (serverless) or EC2 launch types. Good for AWS-native shops.
- EKS is managed Kubernetes — more complex but portable and ecosystem-rich.
- Use IAM roles (not access keys) for EC2 instances and Lambda functions.
- Auto Scaling + load balancers handle traffic spikes for EC2 and ECS.