Khi làm việc với Terraform, việc tách các file cấu hình triển khai sẽ giúp dự án dễ bảo trì, mở rộng và áp dụng cho nhiều môi trường (Dev, Test, Production). Dưới đây là các cách quản lý biến trong Terraform: Variables File, Override Variables, và Output Variables.
Xem thêm:
1. Sử dụng Variables File
Nếu bạn khai báo trực tiếp các giá trị như tên tài nguyên, vị trí (location),… trong main.tf
, việc tái sử dụng cho nhiều môi trường sẽ gặp khó khăn.
Giải pháp: Tách các giá trị cấu hình vào một file riêng biệt, thường là variables.tf
.
terraform-project/ ├── main.tf ├── variables.tf
variables.tf:
variable "resource_group_name" { type = string default = "example-resources" description = "Tên của Resource Group" } variable "resource_group_location" { type = string default = "West Europe" description = "Vị trí của Resource Group" } variable "app_service_plan_name" { type = string default = "example-app-service-plan" description = "Tên của App Service Plan" } variable "app_service_name" { type = string default = "example-app-service" description = "Tên của App Service" }
main.tf
:
resource "azurerm_resource_group" "example" { name = var.resource_group_name location = var.resource_group_location } resource "azurerm_app_service_plan" "example" { name = var.app_service_plan_name location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name kind = "App" sku { tier = "Standard" size = "S1" } } resource "azurerm_app_service" "example" { name = var.app_service_name location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name app_service_plan_id = azurerm_app_service_plan.example.id }
2. Override Variables với .tfvars
Khai báo default trong variables.tf
chỉ phù hợp với môi trường cố định. Khi triển khai nhiều môi trường, cần linh hoạt gán giá trị biến.
Giải pháp: Sử dụng file .tfvars
để gán giá trị cụ thể cho từng môi trường mà không cần chỉnh sửa file variables.tf
.
terraform-project/ ├── main.tf ├── variables.tf ├── terraform.tfvars
terraform.tfvars
:
resource_group_name = "example-resources" resource_group_location = "West Europe" app_service_plan_name = "example-app-service-plan" app_service_name = "example-app-service"
variables.tf:
variable "resource_group_name" { type = string description = "Tên của Resource Group" } variable "resource_group_location" { type = string description = "Vị trí của Resource Group" } variable "app_service_plan_name" { type = string description = "Tên của App Service Plan" } variable "app_service_name" { type = string description = "Tên của App Service" }
main.tf
:
resource "azurerm_resource_group" "example" { name = var.resource_group_name location = var.resource_group_location } resource "azurerm_app_service_plan" "example" { name = var.app_service_plan_name location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name kind = "App" sku { tier = "Standard" size = "S1" } } resource "azurerm_app_service" "example" { name = var.app_service_name location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name app_service_plan_id = azurerm_app_service_plan.example.id }
3. Sử dụng Output Variables
Output variables giúp trích xuất và hiển thị thông tin từ các resource sau khi thực thi. Output có thể được sử dụng trong CI/CD pipeline, chia sẻ giữa các module, hoặc đơn giản là để hiển thị thông tin quan trọng sau khi deploy.
Ví dụ trích xuất các thông tin:
- Địa chỉ IP của Virtual Machine hoặc Web App.
- ID hoặc tên của một resource.
- Hostname hoặc URL của một dịch vụ.
- Truyền dữ liệu giữa các module Terraform.
Cấu trúc thư mục:
terraform-demo/ ├── main.tf ├── variables.tf └── outputs.tf
main.tf:
provider "azurerm" { features {} } resource "azurerm_resource_group" "example" { name = var.resource_group_name location = var.resource_group_location }
variables.tf:
variable "resource_group_name" { type = string default = "demo-resource-group" description = "Tên của Resource Group" } variable "resource_group_location" { type = string default = "East US" description = "Vị trí của Resource Group" }
outputs.tf:
output "resource_group_name" { description = "Tên Resource Group đã tạo" value = azurerm_resource_group.example.name } output "resource_group_location" { description = "Vị trí của Resource Group" value = azurerm_resource_group.example.location }
Tách biệt phần khai báo và gán giá trị biến trong Terraform là một thực hành quan trọng giúp dự án dễ mở rộng, bảo trì và triển khai cho nhiều môi trường. Bằng cách sử dụng hợp lý variables.tf, .tfvars và outputs.tf, bạn sẽ có một kiến trúc hạ tầng rõ ràng và chuyên nghiệp hơn.