Terraform
Hạ tầng GCP được quản lý bằng Terraform, đặt tại thư mục terraform/.
Thành phần
| File | Nội dung |
|---|---|
provider.tf |
Cấu hình provider google (project, region, zone) |
network.tf |
VPC tri-vpc, subnet tri-subnet, firewall rules (HTTP, SSH, internal) |
main.tf |
VM tri-vm-2 chạy image nginx build từ Packer |
docker_vm.tf |
VM tri-docker-vm, tự cài Docker qua metadata_startup_script |
artifact_registry.tf |
Artifact Registry Docker repo tri-docker-repo |
services.tf |
Bật các GCP API cần thiết (Compute, Artifact Registry, Container Analysis/Scanning) |
variables.tf |
Toàn bộ biến cấu hình, có giá trị mặc định |
outputs.tf |
Output: IP VM, tên VPC/subnet, URL Artifact Registry... |
Mạng & Firewall
- VPC:
tri-vpc, subnettri-subnet(10.10.0.0/24, regionasia-southeast1). - Firewall:
allow-http: mở port 80 cho instance có taghttp-server.allow-ssh: mở port 22 cho instance có tagssh.allow-internal: mở toàn bộ TCP/UDP/ICMP nội bộ trong dải subnet.
Compute Instances
tri-vm-2(main.tf): dùng image do Packer build (vm_disk_image, mặc địnhnginx-20260813-110247), taghttp-server+ssh.tri-docker-vm(docker_vm.tf): dùng imagedebian-cloud/debian-12, tự cài Docker Engine (docker-ce, containerd, buildx, compose plugin) khi khởi động.
Sử dụng
cd terraform
terraform init
terraform plan
terraform apply
Xem giá trị output sau khi apply:
terraform output
Biến quan trọng
| Biến | Mặc định | Ghi chú |
|---|---|---|
project_id |
user-twfljuofjeac |
GCP project |
region / zone |
asia-southeast1 / asia-southeast1-a |
|
vm_disk_image |
nginx-20260813-110247 |
Image build từ Packer |
artifact_registry_repo_id |
tri-docker-repo |
Nơi push image Docker |
State hiện đang lưu local (
terraform.tfstate). Nếu làm việc nhóm, cân nhắc chuyển sang remote backend (GCS bucket).