Ví dụ Instance
Instance là máy chủ ảo chạy trên FPT Cloud. Trang này đưa ra các ví dụ Terraform cho những tình huống thường gặp, từ một Instance đơn lẻ tới nhóm Instance phân bố trên nhiều máy chủ vật lý. Khái niệm về Instance xem tại Tổng quan Instance.
Điều kiện tiên quyết
- ✅ Đã hoàn thành Cài đặt và cấu hình.
- ✅ Biết cách tra cứu image, flavor và storage policy — xem Tra cứu bằng data source.
Khối dùng chung
Các ví dụ trên trang dùng chung khối tra cứu và Subnet dưới đây. Lưu vào file shared.tf:
# shared.tf
data "fptcloud_vpc" "example" {
name = "example-vpc"
}
# shared.tf
data "fptcloud_image" "ubuntu" {
vpc_id = data.fptcloud_vpc.example.id
filter {
key = "name"
values = ["UBUNTU-22.04"]
match_by = "substring"
}
}
# shared.tf
data "fptcloud_flavor" "small" {
vpc_id = data.fptcloud_vpc.example.id
filter {
key = "cpu"
values = ["2"]
}
filter {
key = "memory_mb"
values = ["4096"]
}
}
# shared.tf
data "fptcloud_storage_policy" "ssd" {
vpc_id = data.fptcloud_vpc.example.id
filter {
key = "name"
values = ["Premium-SSD"]
}
}
# shared.tf
resource "fptcloud_subnet" "app" {
vpc_id = data.fptcloud_vpc.example.id
name = "app-subnet"
type = "NAT_ROUTED"
cidr = "10.10.1.0/24"
gateway_ip = "10.10.1.1"
}
Instance Linux dùng SSH key
Đây là cấu hình phổ biến nhất: một Instance Linux bạn đăng nhập bằng SSH key. Tham số ssh_key nhận chính nội dung public key.
# instance.tf
resource "fptcloud_instance" "app" {
vpc_id = data.fptcloud_vpc.example.id
name = "app-server-01"
image_name = data.fptcloud_image.ubuntu.images[0].name
flavor_name = data.fptcloud_flavor.small.flavors[0].name
subnet_id = fptcloud_subnet.app.id
storage_size_gb = 40
storage_policy_id = data.fptcloud_storage_policy.ssd.storage_policies[0].id
ssh_key = file(pathexpand("~/.ssh/id_rsa.pub"))
status = "POWERED_ON"
}
Đăng ký SSH key trên FPT Cloud
Đăng ký public key bằng fptcloud_ssh_key để key xuất hiện trong Compute Engine → SSH Keys trên Portal, rồi dùng lại cho nhiều Instance:
# ssh.tf
resource "fptcloud_ssh_key" "deployer" {
name = "deployer-key"
public_key = file(pathexpand("~/.ssh/id_rsa.pub"))
}
resource "fptcloud_instance" "worker" {
vpc_id = data.fptcloud_vpc.example.id
name = "worker-01"
image_name = data.fptcloud_image.ubuntu.images[0].name
flavor_name = data.fptcloud_flavor.small.flavors[0].name
subnet_id = fptcloud_subnet.app.id
storage_size_gb = 40
storage_policy_id = data.fptcloud_storage_policy.ssd.storage_policies[0].id
ssh_key = fptcloud_ssh_key.deployer.public_key
status = "POWERED_ON"
}
Nếu key đã được đăng ký trên Portal, đọc key theo tên và dùng public_key của nó:
# ssh.tf
data "fptcloud_ssh_key" "team" {
name = "team-key"
}
Sau đó đặt ssh_key = data.fptcloud_ssh_key.team.public_key cho Instance.
Đổi ssh_key của một Instance đang chạy sẽ xóa và tạo lại Instance. Nếu cần thay key, cập nhật trực tiếp trong hệ điều hành của Instance thay vì sửa cấu hình Terraform.
Instance dùng mật khẩu
Dùng password thay cho ssh_key khi bạn đăng nhập bằng mật khẩu. Truyền mật khẩu qua biến có sensitive = true để Terraform không in ra màn hình:
# windows.tf
variable "admin_password" {
type = string
sensitive = true
}
data "fptcloud_image" "windows" {
vpc_id = data.fptcloud_vpc.example.id
filter {
key = "name"
values = ["WINSRV-STD2022"]
match_by = "substring"
}
}
# windows.tf
resource "fptcloud_instance" "windows" {
vpc_id = data.fptcloud_vpc.example.id
name = "win-server-01"
image_name = data.fptcloud_image.windows.images[0].name
flavor_name = data.fptcloud_flavor.small.flavors[0].name
subnet_id = fptcloud_subnet.app.id
storage_size_gb = 80
storage_policy_id = data.fptcloud_storage_policy.ssd.storage_policies[0].id
password = var.admin_password
status = "POWERED_ON"
}
Cung cấp mật khẩu qua biến môi trường khi chạy lệnh:
export TF_VAR_admin_password="YOUR_STRONG_PASSWORD"
terraform apply
Mỗi Instance dùng đúng một trong hai tham số ssh_key hoặc password. Mật khẩu vẫn được lưu trong state, vì vậy hãy bảo vệ file state cẩn thận.
Instance có IP private cố định
Đặt private_ip khi hệ thống khác cần gọi tới Instance ở một địa chỉ biết trước. Địa chỉ phải nằm trong dải CIDR của Subnet và chưa được dùng:
# fixed-ip.tf
resource "fptcloud_instance" "db" {
vpc_id = data.fptcloud_vpc.example.id
name = "db-server-01"
image_name = data.fptcloud_image.ubuntu.images[0].name
flavor_name = data.fptcloud_flavor.small.flavors[0].name
subnet_id = fptcloud_subnet.app.id
private_ip = "10.10.1.50"
storage_size_gb = 100
storage_policy_id = data.fptcloud_storage_policy.ssd.storage_policies[0].id
ssh_key = file(pathexpand("~/.ssh/id_rsa.pub"))
status = "POWERED_ON"
}
private_ip chỉ được dùng khi tạo Instance. Đổi giá trị này sau khi tạo không thay đổi IP của Instance.
Nhiều Instance giống nhau
Dùng count để tạo nhiều Instance cùng cấu hình. Tên được đánh số tự động:
# web.tf
variable "web_count" {
type = number
default = 3
}
resource "fptcloud_instance" "web" {
count = var.web_count
vpc_id = data.fptcloud_vpc.example.id
name = format("web-server-%02d", count.index + 1)
image_name = data.fptcloud_image.ubuntu.images[0].name
flavor_name = data.fptcloud_flavor.small.flavors[0].name
subnet_id = fptcloud_subnet.app.id
storage_size_gb = 40
storage_policy_id = data.fptcloud_storage_policy.ssd.storage_policies[0].id
ssh_key = file(pathexpand("~/.ssh/id_rsa.pub"))
status = "POWERED_ON"
}
Lệnh trên tạo web-server-01, web-server-02 và web-server-03. Tăng web_count để thêm Instance mà không ảnh hưởng các Instance đang chạy.
Nhiều Instance với cấu hình khác nhau
Dùng for_each với một map khi mỗi Instance có vai trò và kích thước riêng:
# servers.tf
locals {
servers = {
api = { disk_gb = 40 }
worker = { disk_gb = 80 }
report = { disk_gb = 120 }
}
}
# servers.tf
resource "fptcloud_instance" "server" {
for_each = local.servers
vpc_id = data.fptcloud_vpc.example.id
name = "${each.key}-server"
image_name = data.fptcloud_image.ubuntu.images[0].name
flavor_name = data.fptcloud_flavor.small.flavors[0].name
subnet_id = fptcloud_subnet.app.id
storage_size_gb = each.value.disk_gb
storage_policy_id = data.fptcloud_storage_policy.ssd.storage_policies[0].id
ssh_key = file(pathexpand("~/.ssh/id_rsa.pub"))
status = "POWERED_ON"
}
Với for_each, mỗi Instance có địa chỉ ổn định như fptcloud_instance.server["api"]. Xóa một phần tử khỏi map chỉ xóa đúng Instance đó, khác với count có thể làm dịch chỉ số của các Instance phía sau.
Instance Group phân bố trên nhiều máy chủ vật lý
Instance Group với chính sách Soft Anti Affinity ưu tiên đặt các Instance trên các máy chủ vật lý khác nhau, giúp một sự cố phần cứng không làm dừng toàn bộ nhóm. Theo Tổng quan Instance Group, một nhóm cần từ 2 tới 10 Instance chưa thuộc nhóm nào khác.
Tra cứu chính sách và tạo nhóm cho các Instance ở ví dụ web phía trên:
# group.tf
data "fptcloud_instance_group_policy" "anti_affinity" {
vpc_id = data.fptcloud_vpc.example.id
filter {
key = "name"
values = ["Soft Anti Affinity"]
}
}
resource "fptcloud_instance_group" "web" {
vpc_id = data.fptcloud_vpc.example.id
name = "web-group"
policy_id = data.fptcloud_instance_group_policy.anti_affinity.instance_group_policies[0].id
vm_ids = fptcloud_instance.web[*].id
}
Mọi tham số của fptcloud_instance_group đều tạo lại nhóm khi thay đổi, kể cả vm_ids. Thêm hoặc bớt Instance trong vm_ids sẽ xóa nhóm cũ và tạo nhóm mới.
Instance tắt theo nhu cầu
Tham số status quyết định trạng thái nguồn của Instance. Đặt POWERED_OFF cho Instance chỉ cần chạy khi có việc, ví dụ máy dùng để kiểm thử:
# test-box.tf
variable "test_box_running" {
type = bool
default = false
}
resource "fptcloud_instance" "test_box" {
vpc_id = data.fptcloud_vpc.example.id
name = "test-box-01"
image_name = data.fptcloud_image.ubuntu.images[0].name
flavor_name = data.fptcloud_flavor.small.flavors[0].name
subnet_id = fptcloud_subnet.app.id
storage_size_gb = 40
storage_policy_id = data.fptcloud_storage_policy.ssd.storage_policies[0].id
ssh_key = file(pathexpand("~/.ssh/id_rsa.pub"))
status = var.test_box_running ? "POWERED_ON" : "POWERED_OFF"
}
Bật Instance bằng terraform apply -var="test_box_running=true".
Đọc IP private của Instance
Instance vừa tạo có thể chưa báo IP private trong một thời gian ngắn. Đọc lại Instance bằng data source để lấy IP, ví dụ khi gắn Security Group:
# ip.tf
data "fptcloud_instance" "app" {
vpc_id = data.fptcloud_vpc.example.id
id = fptcloud_instance.app.id
}
output "app_private_ip" {
value = data.fptcloud_instance.app.private_ip
}
Nếu app_private_ip rỗng sau lần apply đầu tiên, chạy lại terraform apply.
Tham số thường dùng
| Tham số | Bắt buộc | Khi thay đổi |
|---|---|---|
vpc_id | Có | Tạo lại Instance. |
name | Có | Đổi tên tại chỗ. Tên không được chứa khoảng trắng. |
image_name | Có | Tạo lại Instance. |
subnet_id | Có | Tạo lại Instance. |
flavor_name | Nên đặt | Resize tại chỗ. Xem Thay đổi và mở rộng hạ tầng. |
storage_size_gb | Có | Tăng dung lượng root disk tại chỗ. Không giảm được. |
storage_policy_id | Có | Chuyển root disk sang storage policy mới tại chỗ. |
status | Có | Bật hoặc tắt Instance tại chỗ. Giá trị: POWERED_ON, POWERED_OFF. |
ssh_key hoặc password | Có, chọn một | Tạo lại Instance. |
private_ip | Không | Chỉ dùng khi tạo. |
tag_ids | Không | Cập nhật tag tại chỗ. Xem Ví dụ tag. |
Danh sách đầy đủ tham số xem tại fptcloud_instance trong tài liệu tham chiếu provider.
Root disk thuộc về Instance và được quản lý bằng storage_size_gb và storage_policy_id. Không khai báo fptcloud_storage cho root disk. Để gắn Security Group, dùng tham số apply_to của Security Group như trong Ví dụ network.