Batch 4: Cloud Ansible AWS & Azure Docker Apache & NGINX ← Full TOC

← Cloud Index

💻 Compute & Storage — Virtual machines, serverless, object storage, block storage

Cloud compute ranges from full VMs to serverless functions. Storage includes object storage (S3/Blob), block storage (EBS/Managed Disks), and file storage (EFS/Azure Files). Choosing the right storage type is critical for performance and cost.

📋 Reference

Term / ServiceDescription / ValueNotes
Virtual MachineEC2 (AWS) / Azure VM / GCE (GCP)Full OS on dedicated or shared hardware
Instance typet3.micro / m5.large / c5.xlargeFamily (t=burstable,m=general,c=compute) + size
AMI / ImageAmazon Machine Image / VM ImageOS snapshot used to launch instances
Spot InstanceUp to 90% off — interruptible VMsCheapest option — workloads must tolerate interruption
Reserved Instance1-3 year commitment — up to 75% offPredictable workloads with steady usage
On-DemandPay per second/hour — no commitmentDefault — flexible but most expensive
Savings PlansFlexible commitment discountCommit to $/hour spend, not specific instance types
Serverless / LambdaRun code without managing serversEvent-driven: API call, S3 upload, schedule, queue
Lambda limits15 min timeout, 10GB memory, 250MB codeKnow the limits for serverless design
ContainerLightweight portable runtime (Docker)See Chapter 40 for full container reference
FargateServerless containers — no EC2 managementECS/EKS task runs without managing servers
Object StorageS3 (AWS) / Azure Blob / GCSFlat namespace, HTTP API, unlimited scale, cheap
S3 bucketNamed container for objects — globally unique nameObjects: key + value + metadata
S3 storage classStandard / IA / Glacier / Deep ArchiveTradeoff between access speed and cost
S3 lifecycleAutomatically move objects to cheaper storage classese.g., move to IA after 30 days, Glacier after 90
Block StorageEBS (AWS) / Azure Managed Disk / Persistent DiskAttached to one VM — like a hard drive
EBS volume typegp3 (general) / io2 (high IOPS) / st1 (throughput)Choose based on workload IOPS and throughput needs
EBS snapshotPoint-in-time backup of EBS volumeStored in S3, used to create AMIs or restore volumes
File StorageEFS (AWS) / Azure Files / Filestore (GCP)Shared NFS/SMB filesystem — mount on multiple VMs
Database managedRDS / Azure DB / Cloud SQLManaged relational DB — backups, patches, HA included
NoSQL managedDynamoDB / Cosmos DB / FirestoreManaged NoSQL — serverless, auto-scale
CacheElastiCache (Redis/Memcached) / Azure CacheIn-memory cache layer between app and database
CDNCloudFront / Azure CDN / Cloud CDNCache static content at edge — lower latency globally
Storage gatewayAWS Storage Gateway / Azure StorSimpleHybrid: on-prem access to cloud storage

💡 Example

# Cloud Compute & Storage — AWS CLI examples
# Install: pip install awscli / brew install awscli
# Configure: aws configure  (enter access key, secret, region)

# ── EC2 (Virtual Machines) ────────────────────────────────────
# Launch instance
aws ec2 run-instances \
  --image-id ami-0c55b159cbfafe1f0 \
  --instance-type t3.micro \
  --key-name MyKeyPair \
  --security-group-ids sg-12345678 \
  --subnet-id subnet-12345678 \
  --count 1 \
  --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=WebServer}]'

# List running instances
aws ec2 describe-instances \
  --filters "Name=instance-state-name,Values=running" \
  --query 'Reservations[].Instances[].[InstanceId,PublicIpAddress,Tags[?Key==`Name`].Value|[0]]' \
  --output table

# Stop / start / terminate
aws ec2 stop-instances    --instance-ids i-1234567890abcdef0
aws ec2 start-instances   --instance-ids i-1234567890abcdef0
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0

# ── S3 (Object Storage) ───────────────────────────────────────
# Create bucket
aws s3 mb s3://mywebuniversity-content --region us-east-1

# Upload file/folder
aws s3 cp   local-file.html   s3://mywebuniversity-content/
aws s3 sync ./html/            s3://mywebuniversity-content/ --delete

# Download
aws s3 cp s3://mywebuniversity-content/file.html ./
aws s3 sync s3://mywebuniversity-content/ ./local-copy/

# List objects
aws s3 ls s3://mywebuniversity-content/
aws s3 ls s3://mywebuniversity-content/ --recursive --human-readable

# Set public read (static website hosting)
aws s3 website s3://mywebuniversity-content/ \
  --index-document index.html --error-document error.html

# Lifecycle rule — move to Glacier after 90 days
aws s3api put-bucket-lifecycle-configuration \
  --bucket mywebuniversity-content \
  --lifecycle-configuration '{
    "Rules": [{
      "ID": "MoveToGlacier",
      "Status": "Enabled",
      "Filter": {"Prefix": "logs/"},
      "Transitions": [{"Days": 90, "StorageClass": "GLACIER"}],
      "Expiration": {"Days": 365}
    }]
  }'

# ── Lambda (Serverless) ───────────────────────────────────────
# Package function
zip function.zip lambda_function.py

# Create function
aws lambda create-function \
  --function-name MyWebUniversityAPI \
  --runtime python3.12 \
  --role arn:aws:iam::123456789012:role/lambda-execution-role \
  --handler lambda_function.lambda_handler \
  --zip-file fileb://function.zip \
  --timeout 30 \
  --memory-size 256

# Invoke function
aws lambda invoke \
  --function-name MyWebUniversityAPI \
  --payload '{"path":"/courses","method":"GET"}' \
  --cli-binary-format raw-in-base64-out \
  response.json && cat response.json

# Lambda function template
cat > lambda_function.py << 'LAMBDA'
import json

def lambda_handler(event, context):
    print(f"Event: {json.dumps(event)}")
    return {
        'statusCode': 200,
        'headers': {'Content-Type': 'application/json',
                    'Access-Control-Allow-Origin': '*'},
        'body': json.dumps({
            'message': 'Hello from MyWebUniversity Lambda!',
            'path':    event.get('path', '/'),
            'method':  event.get('httpMethod', 'GET'),
        })
    }
LAMBDA

← Cloud Fundamentals  |  🏠 Index  |  Cloud Monitoring & Cost →