Terraform 101: 30 phút từ cài đặt đến con EC2 đầu tiên Mở đầu: Lần đầu tôi "code" cả đám mây Năm đầu tiên làm DevOps, tôi tạo EC2 bằng console AWS: click chuột 15 phút, chọn AMI, chọn instance type, chọn subnet, thêm tag, nhấn Launch. Xong 1 con. Chiều đó cần tạo thêm 5 con nữa ở region khác — tôi lại click 15 phút × 5 lần. Cuối tuần quên ghi chép, không nhớ mình đã cấu hình Security Group thế nào. Rồi tôi gặp Terraform. Lần đầu chạy terraform apply , thấy terminal in ra từng dòng "Creating...", rồi 2 phút sau con EC2 hiện ra trên AWS — tôi hiểu ngay: đây là cách mình sẽ làm việc từ giờ trở đi. Terraform là gì? (Định nghĩa bằng 1 câu) Terraform là công cụ Infrastructure as Code (IaC) của HashiCorp. Bạn viết code khai báo infrastructure bạn muốn có, Terraform đọc code đó, so sánh với thực tế trên cloud, rồi tạo/sửa/xóa resource để thực tế khớp với code. Khác với script Python/Bash gọi API tuần tự, Terraform là declarative — bạn nói "tôi muốn có 1 con EC2 t3.micro" chứ không nói "bước 1: gọi API tạo EC2, bước 2: đợi 30 giây..." Cài đặt Terraform macOS (Homebrew) bash brew tap hashicorp/tap brew install hashicorp/tap/terraform terraform -version # Terraform v1.9.x on darwin_arm64 Linux (Ubuntu/Debian) bash wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list sudo apt update && sudo apt install terraform Windows (Winget) powershell winget install Hashicorp.Terraform Cấu trúc thư mục chuẩn Tạo folder cho bài học đầu tiên: bash mkdir terraform-101 && cd terraform-101 Một dự án Terraform cơ bản gồm 4 file chính: plain terraform-101/ ├── main.tf # Khai báo resource ├── variables.tf # Định nghĩa biến đầu vào ├── outputs.tf # Giá trị trả ra sau khi apply └── terraform.tfvars # Giá trị thực tế của biến (không commit lên Git) Lưu ý: File .tfvars thường chứa sensitive data. Bài #3 sẽ nói kỹ, còn bây giờ ta dùng hardcode tạm để tập trung vào flow. Viết file đầu tiên main.tf hcl terraform { required_version = ">= 1.5.0" required_providers { aws = { source = "hashicorp/aws" version = "> 5.0" } } } provider "aws" { region = "ap-southeast-1" } resource "aws_instance" "first_server" { ami = "ami-0c02fb55956c7d316" # Amazon Linux 2 (ap-southeast-1) instance_type = "t3.micro" tags = { Name = "terraform-101-instance" Environment = "learning" ManagedBy = "terraform" } } Giải thích từng khối: Table Khối Ý nghĩa terraform {} Khai báo version Terraform và providers cần dùng required_providers Nói Terraform tải AWS provider từ HashiCorp Registry provider "aws" {} Cấu hình kết nối AWS (region, credentials) resource "aws_instance" "first_server" {} Khai báo 1 EC2 instance. "aws_instance" là resource type, "first_server" là tên local trong code Terraform workflow 4 bước Bước 1: terraform init bash terraform init Terraform tải AWS provider về thư mục .terraform/ , tạo file .terraform.lock.hcl để "khóa" version provider. File này quan trọng — commit nó lên Git để team dùng chung version. Output bạn sẽ thấy: plain Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "> 5.0"... - Installing hashicorp/aws v5.60.0... Bước 2: terraform plan bash terraform plan Terraform đọc code, gọi AWS API để kiểm tra thực tế hiện tại, rồi in ra execution plan — danh sách những gì sẽ tạo/sửa/xóa. Bước này không thay đổi gì trên AWS . Output: plain Terraform will perform the following actions: # aws_instance.first_server will be created + resource "aws_instance" "first_server" { + ami = "ami-0c02fb55956c7d316" + instance_type = "t3.micro" + tags = { + "Environment" = "learning" + "ManagedBy" = "terraform" + "Name" = "terraform-101-instance" } ... } Plan: 1 to add, 0 to change, 0 to destroy. Dấu + nghĩa là "sẽ tạo mới". Dấu ~ là "sẽ sửa". Dấu - là "sẽ xóa". Bước 3: terraform apply bash terraform apply Terraform hiển thị plan lần nữa, hỏi bạn xác nhận: plain Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes Gõ yes → Terraform gọi AWS API tạo EC2. Sau 1-2 phút: plain Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Vào AWS Console → EC2 → bạn sẽ thấy instance đang running. Bước 4: terraform destroy bash terraform destroy Dọn dẹp toàn bộ resource Terraform đã tạo. Gõ yes để xác nhận. Đây là cách tôi tiết kiệm tiền sau mỗi buổi học — tạo rồi xóa, không để resource chạy thừa qua đêm. File lock quan trọng: .terraform.lock.hcl Sau init , Terraform tạo file .terraform.lock.hcl . File này ghi nhận chính xác version provider đã tải (ví dụ: 5.60.0 thay vì ~> 5.0 ). Tại sao quan trọng? Nếu đồng nghiệp clone repo và init , Terraform sẽ đọc lock file để tải đúng version. Không có nó, provider có thể tự động nâng lên 5.99.0 và gây lỗi không tương thích. gitignore # .gitignore — commit lock file, ignore thư mục .terraform/ .terraform/ *.tfstate .tfstate. crash.log *.tfvars Commit .terraform.lock.hcl lên Git. Đừng commit thư mục .terraform/ . Bài tập thực hành Tạo EC2 như hướng dẫn, SSH vào bằng key pair (tạo thêm aws_key_pair resource nếu muốn thử) Sửa tag Environment = "learning" thành "production" , chạy terraform plan — quan sát dấu ~ (modify in-place) Thêm 1 tag mới Owner = "your-name" , apply, kiểm tra trên console terraform destroy để dọn dẹp Kết luận Trong 30 phút, bạn đã: Cài đặt Terraform Hiểu workflow init → plan → apply → destroy Tạo và xóa 1 EC2 hoàn toàn bằng code Biết file .terraform.lock.hcl quan trọng như thế nào Nhưng bài này mới chỉ là "hello world". Bạn có thắc mắc: "Terraform biết con EC2 này đã tạo rồi để sửa chứ không tạo mới, nhưng nó lưu thông tin đó ở đâu?" Câu trả lời nằm ở state file — chủ đề của bài tiếp theo. Key takeaway: Terraform không phải là công cụ "chạy script" — nó là công cụ "đồng bộ trạng thái". Code của bạn là mục tiêu, AWS là thực tế, và Terraform là cầu nối.
Terraform: Cài đặt, AWS Provider và Resource Lifecycle
Installing Terraform, configuring AWS credentials, and writing your first configuration file — from terraform init to terraform apply, creating and destroying an EC2 instance entirely through code.
20/08/2026
6 min read
9 views
Link copied
PP Peter Parker
Series: Terraform