Skip to content

Terraform & CI/CD Platform trên GCP

Tại sao quan trọng

Infrastructure as Code không phải là tùy chọn trong môi trường production hiện đại — nó là điều kiện tiên quyết. Nhưng "có Terraform" và "dùng Terraform đúng cách" là hai thứ khác nhau hoàn toàn. Hầu hết các sự cố nghiêm trọng trong IaC xảy ra không phải vì cú pháp sai, mà vì hiểu sai các vấn đề cốt lõi:

  • State drift: resource thực tế và state file không đồng bộ, terraform apply kế tiếp destroy nhầm
  • State locking failure: hai pipeline chạy đồng thời, state file bị corrupt
  • Blast radius quá lớn: một state file cho toàn bộ infrastructure → một lỗi destroy everything
  • Secret exposure: service account keys, database passwords lộ trong state file
  • CI/CD pipeline không an toàn: build worker có quyền truy cập internet, image không được verify trước khi deploy

Chapter này đi vào cơ chế bên trong của toàn bộ stack automation: từ cách Terraform GCS backend implement state locking, cách Cloud Build worker pool vận hành trong VPC riêng, cách Artifact Registry scan vulnerability sau mỗi push, đến cách Cloud Build tự động sinh SLSA provenance và Binary Authorization enforce tại admission time.

Prerequisites

  • Terraform fundamentals (resources, providers, variables, outputs)
  • GCS fundamentals (objects, versioning, IAM)
  • Cloud Build basic usage
  • Chapter 31: IAM Deep Dive
  • Chapter 34: Binary Authorization

Cấu trúc Chapter

chapter-44-terraform-cicd/
├── 01.terraform-state-gcs-backend.md
├── 02.terraform-modules-iac-patterns.md
├── 03.terraform-gcp-security.md
├── 04.cloud-build-internals.md
├── 05.artifact-registry.md
└── 06.slsa-binary-auth-integration.md

Subtopics

  1. Terraform State với GCS Backend — Cơ chế lưu state dưới dạng GCS objects, state locking qua conditional writes, encryption options (Google-managed / CMEK / CSEK), workspace vs directory separation, bucket configuration best practices, failure modes khi state bị corrupt

  2. Terraform Modules & IaC Patterns cho GCP — Module design principles, terraform-google-modules ecosystem, project factory pattern, landing zone structure, remote state references, environment management với directory-based separation, anti-patterns phổ biến

  3. Terraform Security trên GCP — Service account cho Terraform (least privilege), service account impersonation thay key file, Workload Identity cho Cloud Build, sensitive outputs và state encryption, gcloud terraform vet, drift detection

  4. Cloud Build Internals — Triggers, Worker Pools, Caching — Build execution model, ephemeral worker lifecycle, trigger types và filter mechanics, private worker pools (VPC peering, NO_PUBLIC_EGRESS), build caching strategies (kaniko layer cache, GCS custom cache), substitution variables, service account và IAM, org policy allowedIntegrations

  5. Artifact Registry — Repository Model & Vulnerability Scanning — Repository types và modes (standard/remote/virtual), regional storage architecture, Artifact Analysis vulnerability scanning mechanism (on-push vs on-demand), SBOM generation, cleanup policies (delete vs keep), IAM model cấp repository

  6. SLSA Attestation & Binary Authorization Integration — SLSA framework levels, Cloud Build provenance structure (in-toto format), DSSE signing và tại sao không thể forged, Binary Authorization policy verification, attestation workflow end-to-end, limitations và edge cases

Mental Model

Toàn bộ stack này hình thành một supply chain integrity pipeline:

Developer push code

Cloud Build Trigger (webhook / schedule)

Build chạy trên Worker VM (private pool nếu cần VPC access)

Docker image push lên Artifact Registry

Artifact Registry: auto vulnerability scan

Cloud Build: generate SLSA L3 provenance → lưu vào Artifact Registry

Optional: custom attestor ký thêm attestation (scan passed)

Binary Authorization enforce tại deploy time
(kubectl apply / GKE admission webhook check provenance)

State management của Terraform là layer riêng biệt nhưng không kém phần quan trọng — nó là source of truth cho infrastructure state, và corrupt hay race condition ở đây có thể dẫn đến outage tương đương code bug nghiêm trọng nhất.