Cơ bản
Quy trình cốt lõi
terraform init # install providers & modules terraform plan # preview changes terraform apply # apply changes terraform destroy # tear down all resources
Lệnh cần thiết
terraform initKhởi tạo thư mục làm việc, tải providers
terraform planHiển thị kế hoạch thực thi mà không áp dụng
terraform applyÁp dụng thay đổi vào hạ tầng
terraform destroyPhá hủy tất cả resources được quản lý
terraform fmtĐịnh dạng file .tf theo phong cách chuẩn
terraform validateKiểm tra cú pháp cấu hình
terraform showHiển thị state hoặc plan hiện tại
terraform outputIn các giá trị output
Providers
Cấu hình Provider
terraform { required_providers { aws = { source = "hashicorp/aws", version = "~> 5.0" } } } provider "aws" { region = "us-east-1" }
Ghi chú về Provider
sourceĐịa chỉ registry (hashicorp/aws, hashicorp/google)
versionRàng buộc phiên bản (~> 5.0, >= 3.0, < 4.0)
.terraform.lock.hclFile lock — commit vào version control
aliasDùng nhiều cấu hình cho cùng một provider
Resources
Resource Blocks
resource "aws_instance" "web" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t3.micro" tags = { Name = "web-server" } }
Meta-Arguments của Resource
depends_onPhụ thuộc rõ ràng vào resource khác
countTạo nhiều instances (count = 3)
for_eachTạo instances từ map hoặc set
providerChọn alias provider không mặc định
lifecycleTùy chỉnh hành vi tạo/cập nhật/phá hủy
Tham chiếu Resources
# type.name.attribute aws_instance.web.id aws_instance.web.public_ip aws_vpc.main.cidr_block
Biến số
Khai báo biến
variable "region" { type = string default = "us-east-1" } variable "instance_count" { type = number description = "Number of instances" }
Thiết lập giá trị biến
-var 'region=us-west-2'Cờ CLI
-var-file=prod.tfvarsTải từ file .tfvars
terraform.tfvarsTự động tải nếu tồn tại
TF_VAR_regionBiến môi trường
Interactive promptHỏi khi plan/apply nếu không có giá trị mặc định
Các loại biến
string"us-east-1"
number42
booltrue / false
list(string)["a", "b"]
map(string){ key = "val" }
object({...})Kiểu có cấu trúc với các thuộc tính đặt tên
Outputs
Định nghĩa Outputs
output "instance_ip" { value = aws_instance.web.public_ip description = "Public IP of the web server" } output "db_password" { value = random_password.db.result sensitive = true }
Lệnh Output
terraform outputIn tất cả outputs
terraform output instance_ipIn một output cụ thể
terraform output -jsonĐịnh dạng JSON để scripting
sensitive = trueẨn giá trị khỏi output CLI
module.vpc.vpc_idTruy cập outputs của module con
State
Remote Backend
terraform { backend "s3" { bucket = "my-tf-state" key = "prod/terraform.tfstate" region = "us-east-1" } }
Lệnh State
terraform state listLiệt kê tất cả resources trong state
terraform state show <addr>Hiển thị thuộc tính của một resource
terraform state mv <src> <dst>Đổi tên / di chuyển resource trong state
terraform state rm <addr>Xóa resource khỏi state (giữ hạ tầng)
terraform state pullTải remote state về stdout
terraform import <addr> <id>Import hạ tầng hiện có vào state
Modules
Dùng Modules
module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "~> 5.0" cidr = "10.0.0.0/16" }
Nguồn Module
"./modules/vpc"Đường dẫn cục bộ
"terraform-aws-modules/vpc/aws"Terraform Registry
"github.com/org/repo//module"GitHub repository
"s3::https://bucket/module.zip"S3 bucket
Cấu trúc Module
modules/vpc/ main.tf # resources variables.tf # input variables outputs.tf # output values
Data Sources
Đọc resources hiện có
data "aws_ami" "ubuntu" { most_recent = true filter { name = "name" values = ["ubuntu/images/hvm-ssd/*"] } owners = ["099720109477"] }
Data Sources phổ biến
data.aws_amiTìm AMI theo bộ lọc
data.aws_vpcTìm VPC hiện có
data.aws_caller_identityID tài khoản AWS hiện tại
data.aws_regionRegion AWS hiện tại
data.terraform_remote_stateĐọc outputs từ file state khác
data.externalChạy chương trình bên ngoài để lấy dữ liệu
Lifecycle
Quy tắc Lifecycle
resource "aws_instance" "web" { lifecycle { create_before_destroy = true prevent_destroy = true ignore_changes = [tags] } }
Tùy chọn Lifecycle
create_before_destroyTạo replacement trước khi phá hủy cái cũ
prevent_destroyBáo lỗi nếu terraform destroy nhắm vào resource này
ignore_changesKhông phát hiện drift trên các thuộc tính được liệt kê
replace_triggered_byBắt buộc thay thế khi resource được tham chiếu thay đổi
preconditionXác thực giả định trước khi apply
postconditionXác thực kết quả sau khi apply
Các mẫu thường dùng
Vòng lặp & Điều kiện
# for_each with a map resource "aws_iam_user" "users" { for_each = toset(["alice", "bob"]) name = each.value } # conditional resource count = var.create_db ? 1 : 0
Hàm hữu ích
file("key.pub")Đọc nội dung file
join(", ", list)Nối list thành chuỗi
lookup(map, key, default)Tìm trong map với giá trị dự phòng
length(list)Số phần tử
toset(["a", "b"])Chuyển list thành set (cho for_each)
try(expr, fallback)Trả về dự phòng nếu expr lỗi
templatefile(path, vars)Render một file template