Amazon Web Services offers a diverse portfolio of compute services designed to handle every possible workload—from traditional virtual servers to serverless functions and container orchestration. Understanding these options is critical for architecting cost-effective, scalable applications.
1. EC2 (Elastic Compute Cloud) – Virtual Servers in the Cloud
EC2 is the foundational compute service that provides resizable virtual machines. It offers the most control but requires the most management overhead.
Instance Types (The "Family" System)
AWS organizes EC2 instances into families optimized for specific workloads:
| Family | Code | Use Case |
|---|---|---|
| General Purpose | t, m | Web servers, development environments (t4g, m7i) |
| Compute Optimized | c | Batch processing, gaming servers, ad serving (c7i) |
| Memory Optimized | r, x | Databases, in-memory caches (r7i, x2idn) |
| Storage Optimized | i, d | Data warehouses, NoSQL databases (i4i, d3) |
| Accelerated Computing | p, g | Machine learning, graphics rendering (p5, g5) |
Example: Launching a t4g.small instance (ARM-based Graviton)
aws ec2 run-instances \ --image-id ami-0c7217cdde317cfec \ --instance-type t4g.small \ --key-name my-key \ --subnet-id subnet-0a1b2c3d4e5f67890 \ --count 1
Purchasing Options
| Option | Description | Best For |
|---|---|---|
| On-Demand | Pay by the hour/second | Short-term, unpredictable workloads |
| Reserved Instances | 1-3 year commitment, up to 72% discount | Steady-state applications |
| Spot Instances | Bid on unused capacity, up to 90% discount | Fault-tolerant, stateless workloads |
| Savings Plans | Flexible commitment across compute services | Mixed workloads with consistent usage |
User Data: Automating Bootstrapping
You can run scripts when an instance first launches:
#!/bin/bash # This script installs Apache and serves a custom webpage yum update -y yum install -y httpd systemctl start httpd systemctl enable httpd echo "<h1>Hello from EC2 at $(hostname -f)</h1>" > /var/www/html/index.html
Launch with User Data:
aws ec2 run-instances \ --image-id ami-0c55b159cbfafe1f0 \ --instance-type t2.micro \ --user-data file://bootstrap.sh
2. AWS Lambda – Serverless Functions
Lambda runs code in response to events without provisioning servers. It automatically scales from zero to thousands of concurrent executions.
Supported Runtimes
- Node.js, Python, Java, Go, Ruby, C#, PowerShell
- Custom runtimes via Runtime API
Key Concepts
| Concept | Description |
|---|---|
| Memory | 128 MB to 10,240 MB (allocates proportional CPU) |
| Timeout | Up to 900 seconds (15 minutes) |
| Concurrency | Default soft limit: 1,000 executions per region |
| Ephemeral Storage | 512 MB to 10,240 MB in /tmp |
Advanced Example: S3-Triggered Image Processor
Lambda Function (Python):
import boto3
import urllib.parse
import os
from PIL import Image
s3 = boto3.client('s3')
def lambda_handler(event, context):
# Get bucket and key from S3 event
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'])
# Download original image
download_path = f'/tmp/{key}'
s3.download_file(bucket, key, download_path)
# Create thumbnail
with Image.open(download_path) as img:
img.thumbnail((128, 128))
thumb_path = f'/tmp/thumb_{key}'
img.save(thumb_path)
# Upload thumbnail to different bucket
thumb_bucket = f'{bucket}-thumbnails'
s3.upload_file(thumb_path, thumb_bucket, f'thumb_{key}')
return {
'statusCode': 200,
'body': f'Created thumbnail for {key}'
}
Deployment Package:
pip install pillow -t . zip -r function.zip . aws lambda create-function \ --function-name image-resizer \ --runtime python3.11 \ --role arn:aws:iam::123456789012:role/lambda-s3-role \ --handler lambda.lambda_handler \ --timeout 30 \ --memory-size 1024 \ --zip-file fileb://function.zip
3. ECS (Elastic Container Service) – Container Orchestration
ECS is AWS's native container orchestration service for running Docker containers at scale.
Launch Types
| Launch Type | Description | Use Case |
|---|---|---|
| Fargate | Serverless, no EC2 management | Simplicity, variable workloads |
| EC2 | You manage the underlying instances | Cost optimization, dedicated hosts |
Core Components
┌─────────────────────────────────────────────┐ │ ECS Cluster │ │ ┌───────────────────────────────────────┐ │ │ │ Service │ │ │ │ (Maintains task count, load balancer)│ │ │ │ ┌─────────────────────────────────┐ │ │ │ │ │ Task │ │ │ │ │ │ ┌─────────┐ ┌─────────┐ │ │ │ │ │ │ │Container│ │Container│ │ │ │ │ │ │ │ (Web) │ │ (Sidecar)│ │ │ │ │ │ │ └─────────┘ └─────────┘ │ │ │ │ │ └─────────────────────────────────┘ │ │ │ └───────────────────────────────────────┘ │ └─────────────────────────────────────────────┘
Task Definition Example (JSON)
{
"family": "web-app",
"networkMode": "awsvpc",
"executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
"containerDefinitions": [
{
"name": "nginx",
"image": "nginx:latest",
"memory": 512,
"cpu": 256,
"essential": true,
"portMappings": [
{
"containerPort": 80,
"protocol": "tcp"
}
],
"environment": [
{
"name": "ENVIRONMENT",
"value": "production"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/web-app",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "web"
}
}
}
],
"requiresCompatibilities": ["FARGATE"],
"cpu": "256",
"memory": "512"
}
Deploy Task:
aws ecs register-task-definition --cli-input-json file://task-def.json aws ecs run-task --cluster production --task-definition web-app
4. EKS (Elastic Kubernetes Service) – Managed Kubernetes
EKS provides managed Kubernetes control planes with integration into AWS services.
Key Features
- Managed Control Plane: AWS handles etcd, API server, and scheduler
- Node Options: Self-managed EC2, Managed Node Groups, or Fargate
- Add-ons: VPC CNI, CoreDNS, kube-proxy, and marketplace integrations
Quick Setup with eksctl
# Install eksctl curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp sudo mv /tmp/eksctl /usr/local/bin # Create a cluster eksctl create cluster \ --name my-cluster \ --region us-east-1 \ --nodegroup-name standard-workers \ --node-type t3.medium \ --nodes 3 \ --nodes-min 1 \ --nodes-max 4 \ --managed
Deploying an Application to EKS
# deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.25 ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: nginx-service spec: type: LoadBalancer selector: app: nginx ports: - port: 80 targetPort: 80
Apply to cluster:
kubectl apply -f deployment.yaml kubectl get services # Returns LoadBalancer DNS name
5. AWS Batch – Managed Batch Processing
Batch runs thousands of batch computing jobs across EC2 instances, dynamically scaling based on volume.
Job Definition Example
{
"jobDefinitionName": "data-processor",
"type": "container",
"containerProperties": {
"image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/data-processor:latest",
"vcpus": 2,
"memory": 4096,
"command": ["python", "process.py", "--input", "Ref::s3Input"],
"environment": [
{"name": "OUTPUT_BUCKET", "value": "my-results-bucket"}
],
"executionRoleArn": "arn:aws:iam::123456789012:role/batch-execution-role"
},
"retryStrategy": {
"attempts": 3
},
"timeout": {
"attemptDurationSeconds": 3600
}
}
Submit a Job:
aws batch submit-job \
--job-name my-job-001 \
--job-queue high-priority-queue \
--job-definition data-processor:1 \
--parameters '{"s3Input": "s3://my-data/input.csv"}'
6. Lightsail – Simplified Compute for Beginners
Lightsail is the easiest AWS compute service, offering pre-configured virtual servers with predictable pricing ($3.50–$100+/month).
What You Get
- Virtual server (similar to EC2 but simplified)
- Static IP
- DNS management
- SSD storage
- One-click installs (WordPress, LAMP, Node.js)
Create via CLI:
aws lightsail create-instances \ --instance-name my-website \ --availability-zone us-east-1a \ --blueprint-id wordpress \ --bundle-id nano_3_0
7. AWS Elastic Beanstalk – PaaS for Developers
Elastic Beanstalk abstracts infrastructure entirely. You upload code, and AWS handles capacity provisioning, load balancing, auto-scaling, and monitoring.
Supported Platforms
- Java, .NET, Node.js, PHP, Python, Ruby, Go
- Docker containers
- Custom platforms
Deploying a Node.js Application
Application Structure:
my-app/ ├── app.js ├── package.json └── .ebextensions/ └── nodecommand.config
Deploy:
# Install EB CLI pip install awsebcli # Initialize application eb init -p node.js my-app --region us-east-1 # Create environment and deploy eb create my-app-env
Configuration File (.ebextensions/options.config):
option_settings: aws:autoscaling:launchconfiguration: InstanceType: t3.medium EC2KeyName: my-key aws:elasticbeanstalk:environment: EnvironmentType: LoadBalanced aws:elasticbeanstalk:application:environment: NODE_ENV: production DATABASE_URL: postgres://...
8. AWS Outposts – Hybrid Cloud Compute
Outposts brings native AWS services to on-premises data centers, enabling a consistent hybrid experience.
Use Cases
- Low-latency workloads requiring on-premises data residency
- Local data processing (factories, retail stores)
- Migration with legacy dependencies
9. AWS Wavelength – Edge Compute for 5G
Wavelength embeds AWS compute within 5G network edges, enabling ultra-low latency applications (1–10 ms).
Ideal For
- Autonomous vehicles
- AR/VR applications
- Real-time gaming
- Industrial automation
Comparison Matrix: Choosing the Right Compute Service
| Service | Abstraction Level | Management Overhead | Scaling Speed | Best For |
|---|---|---|---|---|
| EC2 | IaaS (Infrastructure) | High | Minutes | Full control, legacy apps |
| Lambda | FaaS (Serverless) | None | Milliseconds | Event-driven, intermittent workloads |
| ECS | CaaS | Medium | Seconds | Docker containers, microservices |
| EKS | CaaS (Kubernetes) | Medium-High | Seconds | Kubernetes ecosystem, portability |
| Batch | Managed Batch | Low | Minutes | High-performance computing, data processing |
| Lightsail | Simplified IaaS | Very Low | Minutes | Simple websites, beginners |
| Elastic Beanstalk | PaaS | Low | Minutes | Developers who don't want infrastructure management |
| Outposts | Hybrid | High | N/A | On-premises with AWS APIs |
Cost Optimization Strategies
- Right-sizing: Use CloudWatch metrics to identify underutilized instances and downgrade
- Spot Instances: For stateless workloads, save up to 90% over On-Demand
- Savings Plans: Commit to $X/hour of compute usage across EC2, Fargate, and Lambda
- Graviton Processors: ARM-based instances are 20% cheaper and 20% more efficient
- Lambda Provisioned Concurrency: Pay for idle capacity to eliminate cold starts (use only when necessary)
Next Steps
- Networking: Explore VPC, subnets, and security groups to secure your compute resources
- Storage: Pair compute with EBS (block storage), EFS (file storage), or S3 (object storage)
- Monitoring: Set up CloudWatch dashboards and alarms for all compute services
- Automation: Use Infrastructure as Code (CloudFormation, Terraform, CDK) to provision compute resources repeatably