Chương 08 · Infrastructure

Cloud Platforms — AWS / GCP / Azure

3 cloud providers chiếm ~67% market 2024 (AWS 31%, Azure 24%, GCP 12%). Mỗi cái có 200+ services. Học core abstractions: IaaS/PaaS/SaaS, compute (EC2/GCE/VM), storage (S3/GCS/Blob), networking (VPC), IAM, managed K8s (EKS/GKE/AKS), serverless. So sánh thuật ngữ tương đương để switch dễ dàng.

1. Vì sao Cloud?

Trước cloud (pre-2006), công ty phải:

  • Mua server, để trong datacenter / on-prem.
  • Capacity plan trước 6-12 tháng (overprovisioning).
  • Tự lo electricity, cooling, network.
  • Hire sysadmin 24/7.

2006 AWS ra mắt S3 + EC2 → thay đổi hoàn toàn. Cloud value:

  • Pay-as-you-go — không CapEx, OpEx theo use.
  • Elastic — scale up/down trong phút.
  • Global reach — deploy ở 30+ region trong giờ.
  • Managed services — không tự maintain DB, queue, cache.
  • Cutting-edge tech — GPU H100, custom silicon (Graviton, TPU).

1.1. Cloud trade-offs

Pros

  • Không CapEx
  • Scale fast
  • Managed services
  • Compliance built-in (SOC2, HIPAA)
  • Disaster recovery dễ

Cons

  • Cost can spiral (egress, NAT)
  • Vendor lock-in
  • Latency cho on-prem hybrid
  • Compliance lo data sovereignty
  • Skill gap — cloud rất sâu

1.2. Market share 2024

ProviderShareStrengths
AWS~31%Most mature, biggest service catalog, enterprise
Azure~24%Enterprise (Microsoft sales), AI (OpenAI), hybrid
GCP~12%Data/ML (BigQuery, TPU), K8s, networking
Alibaba~4%China + APAC
Oracle Cloud~3%Database workload
IBM, Tencent, Other~26%Niche

2. IaaS / PaaS / SaaS — 3 service model

You manage: Provider manages: ───────────────────────────────────────────────────────────────── On-prem ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ everything nothing IaaS ▓▓▓▓▓▓▓▓▓▓▓▓ app + os hardware, virt PaaS ▓▓▓▓ app OS, runtime, scaling Serverless ▓ code everything else SaaS — config only everything

2.1. IaaS — Infrastructure as a Service

Bạn rent VM, bạn quản lý OS + app. Examples: EC2, GCE, Azure VM.

2.2. PaaS — Platform as a Service

Bạn push code, provider chạy. Không lo OS. Examples: Heroku, AWS Elastic Beanstalk, GCP App Engine, Azure App Service.

2.3. Serverless / FaaS

Provider chạy code khi event (HTTP, queue). Pay per execution. Examples: AWS Lambda, GCP Cloud Functions, Azure Functions, Cloudflare Workers.

2.4. SaaS — Software as a Service

App đầy đủ, end user dùng. Examples: Gmail, Slack, GitHub, Datadog.

2.5. CaaS — Container as a Service

Run container không quản node. Examples: AWS Fargate, GCP Cloud Run, Azure Container Apps.

3. Region, Availability Zone, Edge

3.1. Region

Region = geographic area (us-east-1 = Northern Virginia). Mỗi region:

  • Có 2-6 AZ.
  • Isolate hoàn toàn — region die không ảnh hưởng region khác.
  • Latency khác nhau (us-east-1 ↔ ap-southeast-1 ~250ms).
  • Pricing khác (us-east-1 thường rẻ nhất).

3.2. Availability Zone (AZ)

AZ = data center vật lý isolated trong region (power, network, cooling riêng). Multi-AZ deployment:

  • Latency giữa AZ < 1ms (within region).
  • Dùng cho HA: web app trải 3 AZ → 1 AZ die còn 2.

3.3. Edge / PoP

CloudFront / Cloud CDN / Front Door — edge location toàn cầu, cache content gần user. ~400+ PoP toàn cầu.

3.4. So sánh region naming

Region (Virginia)AWSGCPAzure
US Eastus-east-1us-east4eastus
US Westus-west-2 (Oregon)us-west1westus2
EU Westeu-west-1 (Ireland)europe-west1westeurope
Asia Pacificap-southeast-1 (Singapore)asia-southeast1southeastasia

4. Compute — VM

ConceptAWSGCPAzure
VMEC2 instanceCompute Engine VMVirtual Machine
ImageAMIImageVM Image
Spot/PreemptibleSpot InstanceSpot VM (preemptible)Spot VM
Auto-scalingAuto Scaling GroupManaged Instance GroupVirtual Machine Scale Set
Load BalancerALB / NLBCloud Load BalancerLoad Balancer / Application Gateway
Custom CPUGraviton (ARM)Tau / T2A (ARM)Cobalt 100 (ARM)

4.1. Instance types

Mỗi cloud có family:

  • General: t3, t4g (AWS), n2 (GCP), B/D (Azure) — balanced.
  • Compute-optimized: c5, c6 (AWS), c2 (GCP), F (Azure) — high CPU.
  • Memory-optimized: r5, x2 (AWS), m2 (GCP), E (Azure) — DB.
  • Storage-optimized: i3 (AWS), local SSD — DB heavy I/O.
  • GPU: p4, g5 (AWS), a2 (GCP) — ML/training.

4.2. Pricing model

  • On-demand — full price, pay per hour/sec.
  • Reserved (1-3 năm) — discount 40-72% nhưng cam kết.
  • Spot/Preemptible — discount 60-90% nhưng có thể bị reclaim 30s notice.
  • Savings Plan (AWS) — flexible commitment.

4.3. Spot/Preemptible best practice

Use case: stateless worker, batch job, CI runner. Tránh: stateful DB, single-instance critical.

# Terraform: spot instance
resource "aws_instance" "ci_runner" {
  ami           = "ami-..."
  instance_type = "c5.xlarge"

  instance_market_options {
    market_type = "spot"
    spot_options {
      max_price = "0.05"               # $0.05/hour max
      spot_instance_type = "one-time"
    }
  }
}

# K8s với spot
# AWS Karpenter / Cluster Autoscaler set node pool spot

4.4. Launch template + Auto Scaling Group

resource "aws_launch_template" "web" {
  image_id      = data.aws_ami.amazon_linux.id
  instance_type = "t3.medium"

  user_data = base64encode(templatefile("init.sh", {
    region = var.region
  }))

  network_interfaces {
    security_groups = [aws_security_group.web.id]
  }
}

resource "aws_autoscaling_group" "web" {
  name                = "web-asg"
  vpc_zone_identifier = aws_subnet.public[*].id
  min_size            = 2
  max_size            = 10
  desired_capacity    = 4
  health_check_type   = "ELB"
  target_group_arns   = [aws_lb_target_group.web.arn]

  launch_template {
    id      = aws_launch_template.web.id
    version = "$Latest"
  }

  tag {
    key                 = "Name"
    value               = "web"
    propagate_at_launch = true
  }
}

5. Storage

5.1. Object Storage — S3 / GCS / Blob

FeatureAWS S3GCSAzure Blob
ContainerBucketBucketContainer
ObjectObjectObjectBlob
TiersStandard/IA/Glacier/Deep ArchiveStandard/Nearline/Coldline/ArchiveHot/Cool/Cold/Archive
LifecycleLifecycle PolicyLifecycle RuleLifecycle Mgmt
VersioningYesYesYes
EncryptionSSE-S3, KMS, CAuto, CMEK, CSEKSSE
Pricing /GB/month$0.023 (Std)$0.020 (Std)$0.018 (Hot)

5.2. S3 example

# Tạo bucket
aws s3 mb s3://my-app-bucket --region us-east-1

# Upload
aws s3 cp file.txt s3://my-app-bucket/
aws s3 sync ./dist s3://my-app-bucket/ --delete

# Download
aws s3 cp s3://my-app-bucket/file.txt .

# Set encryption + versioning
aws s3api put-bucket-encryption --bucket my-app-bucket \
  --server-side-encryption-configuration '{"Rules":[{"ApplyServerSideEncryptionByDefault":{"SSEAlgorithm":"AES256"}}]}'

aws s3api put-bucket-versioning --bucket my-app-bucket \
  --versioning-configuration Status=Enabled

# Lifecycle (move to Glacier sau 90 ngày)
aws s3api put-bucket-lifecycle-configuration --bucket my-app-bucket \
  --lifecycle-configuration file://lifecycle.json

5.3. Block Storage — EBS / Persistent Disk / Managed Disk

Block storage attach vào VM. Persistence ngoài lifecycle VM.

TypeAWS EBSGCP PDAzure
SSD Generalgp3pd-balancedPremium SSD
SSD High-perfio2 (provisioned IOPS)pd-extremeUltra Disk
HDDst1, sc1pd-standardStandard HDD
NVMe locali3 instance storeLocal SSDLsv2

5.4. File Storage — EFS / Filestore / Files

NFS-compatible, RWX (mount nhiều VM).

  • AWS EFS — POSIX, scale tự động.
  • GCP Filestore — managed NFS.
  • Azure Files — SMB + NFS.
  • FSx (AWS) — Lustre, NetApp, Windows File Server.

6. Networking & VPC

6.1. VPC concept

VPC = isolated network trong cloud, có CIDR range (vd 10.0.0.0/16). Chia subnet (public/private), security group, NACL, route table.

VPC 10.0.0.0/16 ├── Public Subnet 10.0.1.0/24 (AZ-a) ──→ Internet Gateway ──→ Internet ├── Public Subnet 10.0.2.0/24 (AZ-b) ├── Private Subnet 10.0.10.0/24 (AZ-a) ─→ NAT Gateway ─→ IGW (egress only) ├── Private Subnet 10.0.20.0/24 (AZ-b) └── DB Subnet 10.0.100.0/24 (AZ-a) ── isolated, không egress

6.2. Public vs Private subnet

TypeRouteUse
Public0.0.0.0/0 → IGWLoad balancer, bastion
Private0.0.0.0/0 → NAT GatewayApp, K8s nodes (egress only)
DatabaseNo internet routeRDS, ElastiCache (max isolation)

6.3. Security Group vs NACL

  • Security Group (SG) — stateful firewall ở instance level. Allow only (no deny rule).
  • Network ACL (NACL) — stateless firewall ở subnet level. Allow + deny.

Practical: dùng SG cho 99% case. NACL chỉ cho subnet-wide block (IP blacklist).

resource "aws_security_group" "web" {
  name   = "web-sg"
  vpc_id = aws_vpc.main.id

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port       = 443
    to_port         = 443
    protocol        = "tcp"
    cidr_blocks     = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

# DB SG — chỉ cho web SG access
resource "aws_security_group" "db" {
  name   = "db-sg"
  vpc_id = aws_vpc.main.id

  ingress {
    from_port       = 5432
    to_port         = 5432
    protocol        = "tcp"
    security_groups = [aws_security_group.web.id]    # SG-as-source!
  }
}

6.4. Load Balancer

LayerAWSGCPAzure
L4 (TCP)NLBNetwork LBLB
L7 (HTTP)ALBHTTP(S) LBApplication Gateway
GlobalGlobal Accelerator + CloudFrontGlobal HTTP(S) LBFront Door

6.5. CDN

  • AWS CloudFront — 400+ PoP, integrate S3, ALB.
  • GCP Cloud CDN — Anycast IP.
  • Azure CDN / Azure Front Door.
  • Third-party: Cloudflare, Fastly, Akamai.

6.6. Egress cost — silent killer

Cảnh báo egress Data vào cloud free, data ra đắt: AWS $0.09/GB egress (us-east-1 → internet). GB free 100/month. Production app 10TB/month egress = ~$900. Multi-region replication: 1TB cross-region = $20-40. Budget cẩn thận!

Cách giảm: CDN (cache reduce egress), VPC Endpoint (S3 không qua internet), Direct Connect (commit pricing thấp hơn).

7. IAM — Identity & Access Management

7.1. Concepts

  • User / ServiceAccount — identity.
  • Group — collection of users.
  • Role — collection of permissions; users/services assume.
  • Policy — JSON rules (allow/deny actions on resources).

7.2. AWS IAM policy

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowReadS3",
      "Effect": "Allow",
      "Action": [
        "s3:GetObject",
        "s3:ListBucket"
      ],
      "Resource": [
        "arn:aws:s3:::my-bucket",
        "arn:aws:s3:::my-bucket/*"
      ]
    },
    {
      "Sid": "DenyDelete",
      "Effect": "Deny",
      "Action": "s3:DeleteObject",
      "Resource": "*"
    }
  ]
}

7.3. IAM principles

  1. Least privilege — grant minimum permissions needed.
  2. No long-lived keys — dùng IAM Role + STS, OIDC từ GitHub.
  3. MFA mọi root account.
  4. Tách environment — prod ở account riêng, không share role với dev.
  5. Audit — CloudTrail (AWS), Cloud Audit Logs (GCP), Activity Log (Azure).
  6. Tag-based access control (ABAC) — scale hơn role-based.

7.4. AWS Organizations / GCP Org / Azure Tenant

Multi-account hierarchy:

AWS Organization ├── OU "Production" │ ├── Account: prod-app │ └── Account: prod-data ├── OU "Non-Production" │ ├── Account: staging │ └── Account: dev └── Account: shared-services (CI, registry)

Service Control Policy (SCP) ở org level — guardrails (vd: prod account không xóa được S3 bucket).

7.5. IAM trong Terraform — OIDC GHA

# GitHub OIDC provider
resource "aws_iam_openid_connect_provider" "github" {
  url             = "https://token.actions.githubusercontent.com"
  client_id_list  = ["sts.amazonaws.com"]
  thumbprint_list = ["1b511abead59c6ce207077c0bf0e0043b1382612"]
}

# Role assume được bởi GitHub Actions
resource "aws_iam_role" "gha_deploy" {
  name = "gha-deploy"

  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Effect = "Allow"
      Principal = {
        Federated = aws_iam_openid_connect_provider.github.arn
      }
      Action = "sts:AssumeRoleWithWebIdentity"
      Condition = {
        StringEquals = {
          "token.actions.githubusercontent.com:aud" = "sts.amazonaws.com"
        }
        StringLike = {
          "token.actions.githubusercontent.com:sub" = "repo:myorg/myrepo:ref:refs/heads/main"
        }
      }
    }]
  })
}

resource "aws_iam_role_policy_attachment" "gha_deploy" {
  role       = aws_iam_role.gha_deploy.name
  policy_arn = aws_iam_policy.deploy.arn
}

8. Managed Kubernetes — EKS / GKE / AKS

FeatureEKS (AWS)GKE (GCP)AKS (Azure)
Control plane cost$0.10/hourFree (Standard) / $0.10/hour (Autopilot)Free (Free tier)
Auto-upgradeManual / EKS Auto ModeAuto (channels)Auto
Auto-scaling nodeKarpenter / CABuilt-in CA, AutopilotCA
Network pluginVPC CNIVPC-native, AnthosAzure CNI
Best atAWS-integrationK8s features (oldest, mature)Microsoft stack

8.1. EKS quick start

# eksctl (CLI dễ nhất)
eksctl create cluster \
  --name dev-cluster \
  --region us-east-1 \
  --version 1.28 \
  --nodegroup-name workers \
  --node-type t3.medium \
  --nodes 3 \
  --nodes-min 1 \
  --nodes-max 10 \
  --managed

# Connect
aws eks update-kubeconfig --name dev-cluster --region us-east-1
kubectl get nodes

8.2. Production-grade với Terraform

module "eks" {
  source  = "terraform-aws-modules/eks/aws"
  version = "~> 20.0"

  cluster_name    = "prod-cluster"
  cluster_version = "1.28"

  vpc_id     = module.vpc.vpc_id
  subnet_ids = module.vpc.private_subnets

  cluster_endpoint_public_access = true

  cluster_addons = {
    coredns                = { addon_version = "v1.10.1-eksbuild.7" }
    kube-proxy             = {}
    vpc-cni                = {}
    aws-ebs-csi-driver     = {}
  }

  eks_managed_node_groups = {
    general = {
      desired_size = 3
      min_size     = 2
      max_size     = 10
      instance_types = ["t3.medium"]
    }

    spot = {
      desired_size = 2
      min_size     = 0
      max_size     = 20
      instance_types = ["c5.large", "c5a.large", "m5.large"]
      capacity_type  = "SPOT"
    }
  }
}

8.3. GKE Autopilot (zero-config)

gcloud container clusters create-auto autopilot-cluster \
  --region=us-central1

Autopilot: Google quản node, billing per Pod resource (request CPU/RAM). Phù hợp team không muốn lo node management.

9. Serverless / Functions

9.1. AWS Lambda

// index.js
exports.handler = async (event) => {
  console.log('Event:', JSON.stringify(event));

  return {
    statusCode: 200,
    body: JSON.stringify({ message: 'Hello!' }),
  };
};
# Deploy với SAM
sam init --runtime nodejs20.x
sam build
sam deploy --guided

# Hoặc Terraform

9.2. Lambda pricing

  • Free tier: 1M request + 400,000 GB-second/month.
  • Sau đó: $0.20/1M request + $0.0000166667/GB-s.
  • 1M request 128MB chạy 200ms = ~$0.20 + $0.42 = ~$0.62.

So sánh với EC2 t3.micro $7.5/month: Lambda rẻ hơn cho traffic < ~3M request/month, đắt hơn cho continuous load.

9.3. Cold start

Lambda có thể "ngủ" nếu không có request. Request đầu tiên: cold start (init runtime, load code) — 100ms - vài giây.

Mitigations:

  • Provisioned Concurrency — pre-warm N instance ($).
  • Languages khởi động nhanh: Go, Rust, Python > Java, .NET.
  • Lambda SnapStart (Java) — pre-init snapshot.
  • Smaller deployment package.

9.4. Use cases serverless

  • API GW + Lambda — REST API simple.
  • S3 trigger — process upload.
  • Cron — EventBridge schedule.
  • Stream processing — Kinesis/SQS triggers.
  • Webhook handler.

9.5. Cloud Run (GCP) — middle ground

gcloud run deploy myapp \
  --image gcr.io/myproject/myapp:1.0 \
  --platform managed \
  --region us-central1 \
  --allow-unauthenticated

Cloud Run = container serverless. Pay per request, scale-to-zero, max 60min request. Tốt hơn Lambda cho:

  • Long-running request (Lambda max 15min).
  • Container portability (any language).
  • Less vendor lock-in.

Equivalent: AWS App Runner, Azure Container Apps.

10. Managed Databases

DBAWSGCPAzure
Postgres/MySQLRDSCloud SQLAzure Database
Postgres serverlessAurora Serverless v2AlloyDBHyperscale
Distributed SQLAuroraSpannerCosmos DB (multi-model)
NoSQL documentDynamoDBFirestoreCosmos DB
NoSQL key-valueDynamoDBDatastoreCosmos DB
CacheElastiCache (Redis/Memcached)MemorystoreCache for Redis
Data WarehouseRedshiftBigQuerySynapse
SearchOpenSearch(no managed)Cognitive Search

10.1. RDS example (Terraform)

resource "aws_db_subnet_group" "default" {
  name       = "main"
  subnet_ids = module.vpc.database_subnets
}

resource "aws_db_instance" "main" {
  identifier              = "myapp-prod"
  engine                  = "postgres"
  engine_version          = "15.4"
  instance_class          = "db.r5.large"
  allocated_storage       = 100
  storage_type            = "gp3"
  storage_encrypted       = true

  db_name                 = "myapp"
  username                = "app"
  password                = var.db_password   # Vault / Secrets Manager

  multi_az                = true              # HA
  backup_retention_period = 30
  backup_window           = "02:00-04:00"
  maintenance_window      = "sun:04:00-sun:05:00"

  performance_insights_enabled = true

  deletion_protection = true

  vpc_security_group_ids = [aws_security_group.db.id]
  db_subnet_group_name   = aws_db_subnet_group.default.name

  skip_final_snapshot = false
  final_snapshot_identifier = "myapp-prod-final"
}

10.2. Backup & Recovery

  • Snapshot — point-in-time backup, restore tạo DB mới.
  • PITR (Point-In-Time Recovery) — restore tới timestamp cụ thể (within retention).
  • Cross-region snapshot — DR.
  • Read replica — async replication, read scaling.

11. Cost Management & FinOps

11.1. Cost monitoring

  • AWS Cost Explorer — view spend, forecast.
  • GCP Cost / Budget alerts.
  • Azure Cost Management.
  • Third-party: Vantage, Infracost (Terraform), CloudHealth, Datadog.

11.2. Common cost traps

TrapCostFix
Egress traffic$0.09/GBCDN, VPC endpoint, compress
NAT Gateway$0.045/GB + $0.045/hourSingle NAT (giảm HA), VPC endpoint cho S3
EBS unused$0.10/GB/monthDelete volume khi terminate EC2
Idle resourcesVaryCron stop dev/staging at night
Over-provisioned RDSdb.r5 vs db.t3 = 5×Monitor utilization, downsize
S3 versioning10× storageLifecycle delete old versions
CloudWatch logs$0.50/GB ingestLog level filter, S3 archive
Cross-AZ data$0.01/GB each directionAffinity routing trong cùng AZ

11.3. Reserved Instances / Savings Plans

Cam kết 1-3 năm cho discount 40-72%. Phù hợp baseline traffic. On-demand cho burst.

AWS Compute Savings Plans linh hoạt nhất — apply EC2/Lambda/Fargate.

11.4. FinOps practices

  1. Tag mọi resource (Owner, Project, Env, CostCenter).
  2. Showback / Chargeback — chia phí theo team/project.
  3. Budget alert — Slack notification khi vượt 80%.
  4. Right-sizing — review monthly utilization.
  5. Auto shutdown dev/staging cuối ngày.
  6. Spot for stateless.
  7. Reserved cho predictable.

12. Multi-cloud strategy

12.1. Lý do multi-cloud

  • Avoid vendor lock-in.
  • Best-of-breed per workload (BigQuery cho analytics, AWS cho legacy).
  • Compliance (data sovereignty per region).
  • Negotiation leverage.
  • Disaster recovery cross-cloud.

12.2. Khó khăn multi-cloud

  • Skill: team phải biết 2-3 cloud → expensive.
  • Network: cross-cloud networking phức tạp + đắt egress.
  • Security: IAM khác nhau, audit khó.
  • Cost optimization khó (cant negotiate volume).
  • Tooling: K8s + Terraform + Crossplane help nhưng vẫn cần per-cloud expertise.

12.3. Realistic patterns

  • Primary + DR: AWS chính, GCP backup. Active-passive.
  • Best-of-breed: AWS infra + GCP BigQuery + Cloudflare CDN.
  • Compliance split: EU data ở Azure EU, US data ở AWS.
  • Hybrid: on-prem chính + cloud burst.

Avoid: "true multi-cloud active-active" — overhead phần lớn không xứng. Đa số công ty thắng khi single-cloud + multi-region.

13. Bài tập

  1. Cloud account: setup AWS Free Tier, GCP $300 credit, hoặc Azure $200 credit. Setup MFA cho root account.
  2. EC2 first server: launch t3.micro, SSH vào, install nginx, expose qua public IP.
  3. VPC custom: tạo VPC 10.99.0.0/16 với 2 public + 2 private + 2 DB subnet ở 2 AZ. NAT Gateway. Verify private subnet có internet egress.
  4. Multi-tier app: web (public subnet, ALB) + app (private subnet) + RDS (DB subnet). Security Group chain: ALB → web → DB.
  5. S3 + lifecycle: tạo bucket. Upload file. Setup versioning. Lifecycle: transition to IA sau 30 ngày, Glacier sau 90, expire sau 365.
  6. IAM least privilege: tạo user "developer" chỉ có quyền read EC2, write S3 1 bucket cụ thể. Test bằng aws sts assume-role.
  7. OIDC GitHub: setup IAM OIDC provider + role cho GitHub Actions deploy S3. Workflow assume role không cần long-lived key.
  8. EKS cluster: dùng eksctl tạo cluster 3-node. Deploy app. Sau test, eksctl delete cluster để khỏi tốn.
  9. Lambda + API GW: viết Lambda Hello World, expose qua API Gateway. Test với curl.
  10. Cloud Run: container hóa app, deploy lên GCP Cloud Run. Verify scale-to-zero (sleep 5 phút, hit lại — cold start).
  11. RDS Postgres: deploy db.t3.micro multi-AZ. Connect từ EC2 trong VPC. Backup snapshot. Restore.
  12. Cost analysis: review AWS Cost Explorer / Billing 30 ngày. Identify top 5 spend. Đề xuất 3 optimizations.
  13. Cross-cloud: setup VPN giữa AWS VPC và GCP VPC (hoặc Cloud Interconnect). Ping VM cross-cloud.
  14. Disaster recovery drill: backup RDS cross-region. Simulate region failure: terminate primary, restore từ snapshot ở region 2. Đo RTO/RPO.

14. Quiz

Quiz cuối Chương 8

Region và Availability Zone (AZ) khác nhau ở:

  • Cùng nghĩa
  • AZ là country
  • Region là geographic area (us-east-1 = Virginia); AZ là data center vật lý isolated trong region (power/network/cooling riêng); 1 region = 2-6 AZ
  • Region nhỏ hơn AZ
Multi-AZ deploy: trải app qua nhiều AZ trong cùng region cho HA — latency < 1ms giữa AZ. Multi-region deploy: cross-region cho DR (AZ-level disaster). Region die hiếm (nhưng đã xảy ra: us-east-1 12/2021). Production critical: multi-region.

IaaS / PaaS / Serverless khác nhau ở:

  • Pricing only
  • Mức độ provider quản lý: IaaS (bạn lo OS + app), PaaS (bạn lo app, provider lo OS+runtime), Serverless (bạn lo code, provider lo phần còn lại)
  • IaaS là cũ nhất
  • Cùng giá
Phổ Spectrum quản lý: On-prem (bạn lo all) → IaaS (EC2, GCE) → CaaS (Fargate, Cloud Run) → PaaS (Beanstalk, App Engine) → FaaS/Serverless (Lambda, Cloud Functions) → SaaS (Gmail). Mỗi step lên: ít control, ít flexibility, nhanh hơn để launch. Trade-off chọn theo team size + workload.

Spot/Preemptible instance phù hợp với workload:

  • Stateful database
  • Single-instance critical service
  • Web app session-stateful
  • Stateless worker, batch job, CI runner — tolerant của 30s preemption notice; tiết kiệm 60-90% so on-demand
Spot: cloud bán capacity dư với discount cao. Provider có thể reclaim với 30s-2min notice. Phù hợp workload "interruptible": batch processing, distributed training (PyTorch checkpoint), CI runner, K8s spot node group cho stateless pods. Avoid: DB primary, single-instance app, long-running stateful job không có checkpoint.

Egress (data ra cloud) cost trap:

  • Data IN free, data OUT internet đắt ($0.09/GB AWS, sau 100GB free) — 10TB egress/month có thể $900+; mitigate bằng CDN, VPC endpoint, compression
  • Cả IN và OUT đều đắt
  • Free unlimited
  • Chỉ tính cross-region
Common cost surprise: dev không nhận thức egress pricing → API trả nhiều JSON → bill $$. Fix: (1) CDN cache giảm origin egress; (2) VPC Endpoint cho S3/DynamoDB không qua internet; (3) Compress (gzip, brotli); (4) Multi-region = cross-region transfer cost; (5) Cross-AZ transfer trong region cũng tính ($0.01/GB direction).

Security Group vs NACL trong AWS VPC:

  • SG là deprecated
  • NACL chỉ allow rule
  • SG: stateful, instance-level, allow only; NACL: stateless, subnet-level, allow + deny — dùng SG cho 99% case, NACL cho subnet-wide IP block
  • Phải dùng cả 2
SG stateful: response auto-allowed (không cần rule egress riêng). Easier. NACL stateless: phải rule cả 2 chiều, support DENY (cho IP block list). Practical: SG primary, NACL chỉ khi cần guard subnet-wide. Best practice SG: SG-as-source ("allow from sg-web") thay CIDR — auto update khi instance scale.

IAM least privilege là:

  • Cho admin all-access
  • Grant minimum permissions cần thiết, không hơn — explicit allow + deny by default; audit through CloudTrail/Cloud Audit Logs
  • Chỉ allow read
  • Block all
Default IAM: deny everything. Mỗi action phải explicit allow. Least privilege: chỉ grant action + resource cần thiết. Vd: Lambda chỉ cần s3:GetObject 1 bucket → policy chỉ allow đó. Lợi: blast radius nhỏ khi credential leak. Audit: review IAM Access Analyzer (AWS), Recommender (GCP) để identify unused permissions.

Managed K8s control plane cost so sánh:

  • Tất cả $0.50/hour
  • EKS free
  • GKE Standard free
  • EKS $0.10/hour, GKE Standard có 1 cluster free + sau đó $0.10/hour, AKS free; nodes tính riêng
EKS: $0.10/hour (~$73/month) cho control plane. GKE: 1 zonal cluster free + thêm $0.10/hour. AKS: control plane free (chỉ tính node). Autopilot mode (GKE/EKS): pay per Pod resource thay node — no node management. Cost optimization: bấm nodes Spot, autoscale, pack bin pods.

AWS Lambda cold start là:

  • Request đầu tiên sau period idle — Lambda init runtime (load code, JVM startup, ...) trước execute → 100ms-vài giây extra latency; mitigate bằng Provisioned Concurrency, smaller package, fast-startup language (Go/Python > Java)
  • Lambda crash
  • Network slow
  • Always slow
Lambda "ngủ" sau 5-15 phút idle. Wake up: container init + runtime init + code init. Java cold start có thể 3-5s; Go ~50-100ms; Python ~200ms. Mitigate: Provisioned Concurrency (pre-warm N instance, có cost); SnapStart cho Java (snapshot-based init); minimize package size; code layer reuse.

Multi-cloud "best-of-breed" là:

  • Active-active mọi service mọi cloud
  • Cấm dùng cloud nào
  • Mix: AWS infra + GCP BigQuery (analytics) + Cloudflare CDN — mỗi service chọn cloud tốt nhất; ít overhead hơn full multi-cloud
  • Chỉ dùng on-prem
"True" multi-cloud (active-active mọi thứ) overhead lớn: skill 3 cloud, network cross-cloud phức tạp + đắt egress, IAM 3 hệ. Pragmatic: chọn cloud chính (AWS), cherry-pick service nổi bật từ cloud khác (BigQuery, Vertex AI cho ML, Cloudflare CDN). Đa số công ty thắng với single-cloud + multi-region.

FinOps practice quan trọng nhất khi mới adopt cloud:

  • Mua Reserved Instance ngay
  • Tag everything (Owner, Project, Env, CostCenter) → chia phí theo team, identify waste, accountability
  • Force serverless
  • Multi-cloud
Tag là foundation FinOps: không tag → không biết ai consume, không thể chargeback. Practice: mandatory tag policy enforce qua SCP. Phổ biến tags: Owner (team email), Project, Env (prod/staging/dev), CostCenter, ManagedBy (Terraform/CFN). Cost report group by tag → showback per team. Reserved chỉ optimize predictable baseline (sau khi đo).

Hoàn thành Chương 8. Tiếp theo: Chương 9 — Configuration Management & Secrets →