Terraform vs OpenTofu 2026 — Chọn đúng công cụ Infrastructure as Code sau cuộc chia tách lịch sử
Posted on: 4/22/2026 8:14:05 AM
Table of contents
- 1. Bối cảnh — Từ một codebase đến hai con đường
- 2. License — Vấn đề cốt lõi
- 3. So sánh tính năng chuyên sâu 2026
- 4. Bảng so sánh tổng hợp
- 5. Kiến trúc IaC Pipeline thực tế
- 6. Quản lý Multi-Cloud: AWS + Azure + Cloudflare
- 7. Migration Guide — Chuyển đổi giữa Terraform và OpenTofu
- 8. Chọn công cụ nào? Decision Framework
- 9. Observability cho IaC Pipeline
- 10. Kết luận
Tháng 8/2023, HashiCorp thay đổi license Terraform từ MPL 2.0 sang BSL 1.1 — và thế giới Infrastructure as Code bị chia đôi. OpenTofu ra đời như một fork mã nguồn mở dưới sự bảo trợ của Linux Foundation. Hai năm rưỡi sau, cả hai dự án đã phân tách đáng kể: Terraform hướng đến Agentic Infrastructure với Project Infragraph và MCP servers, trong khi OpenTofu tập trung vào tính mở và tính năng community-driven — state encryption, OCI registry, provider for_each. Bài viết này phân tích sâu cả hai hướng đi, giúp bạn chọn đúng công cụ IaC cho team năm 2026.
1. Bối cảnh — Từ một codebase đến hai con đường
Để hiểu tại sao câu hỏi "Terraform hay OpenTofu?" quan trọng, cần nhìn lại timeline sự kiện:
2. License — Vấn đề cốt lõi
Mọi quyết định kỹ thuật đều bắt đầu từ câu hỏi license. Đây là điểm phân tách không thể điều hoà giữa hai dự án:
| Tiêu chí | Terraform (BSL 1.1) | OpenTofu (MPL 2.0) |
|---|---|---|
| Loại license | Source-available, không phải open-source theo định nghĩa OSI | Open-source theo chuẩn OSI, được CNCF và Linux Foundation bảo trợ |
| Sử dụng nội bộ | Không giới hạn — dùng trong công ty thoải mái | Không giới hạn |
| Cung cấp dịch vụ hosted | Bị cấm nếu cạnh tranh với HCP Terraform | Hoàn toàn tự do |
| Fork / modify | Được phép dùng nội bộ, không được phân phối bản sửa đổi dưới dạng dịch vụ cạnh tranh | Tự do fork, modify, redistribute |
| Rủi ro pháp lý | Ranh giới "competing offering" chưa rõ ràng trong một số trường hợp biên | Không có rủi ro — license MPL đã được kiểm chứng hàng thập kỷ |
Khi nào license BSL thực sự ảnh hưởng?
Nếu bạn chỉ dùng Terraform CLI để quản lý infrastructure nội bộ (dù là startup hay enterprise), BSL 1.1 không ảnh hưởng gì. Vấn đề phát sinh khi bạn: (1) xây dựng managed IaC platform bán cho khách hàng, (2) tích hợp Terraform vào SaaS offering nơi IaC là một phần giá trị sản phẩm, hoặc (3) cần đảm bảo supply chain compliance yêu cầu toàn bộ stack phải OSI-approved. Trong các trường hợp này, OpenTofu là lựa chọn an toàn hơn.
3. So sánh tính năng chuyên sâu 2026
Sau hai năm rưỡi phát triển độc lập, hai dự án đã có những divergence đáng kể. Dưới đây là phân tích chi tiết từng mảng:
3.1. State Management
Đây là nơi OpenTofu vượt trội rõ nhất. Từ phiên bản 1.7 (2024), OpenTofu hỗ trợ native state encryption — mã hoá file state tại chỗ mà không cần công cụ bên thứ ba.
# OpenTofu — State Encryption với AWS KMS
terraform {
encryption {
key_provider "aws_kms" "main" {
kms_key_id = "arn:aws:kms:ap-southeast-1:123456789:key/abcd-1234"
region = "ap-southeast-1"
}
method "aes_gcm" "encrypt" {
keys = key_provider.aws_kms.main
}
state {
method = method.aes_gcm.encrypt
}
plan {
method = method.aes_gcm.encrypt
}
}
}
Terraform không có tính năng tương đương trong CLI. Để mã hoá state, bạn phải dựa vào backend (S3 server-side encryption, Azure Storage encryption) hoặc sử dụng HCP Terraform.
Ngoài ra, OpenTofu 1.10 bổ sung native S3 state locking — không cần DynamoDB table riêng để lock state nữa, tiết kiệm chi phí và đơn giản hoá setup:
# OpenTofu 1.10 — S3 native locking (không cần DynamoDB)
terraform {
backend "s3" {
bucket = "my-tofu-state"
key = "prod/terraform.tfstate"
region = "ap-southeast-1"
use_lockfile = true # Native S3 conditional writes
}
}
# So sánh: Terraform vẫn cần DynamoDB
terraform {
backend "s3" {
bucket = "my-tf-state"
key = "prod/terraform.tfstate"
region = "ap-southeast-1"
dynamodb_table = "terraform-locks" # Phải tạo và quản lý riêng
}
}
3.2. Provider & Module Registry
graph LR
subgraph Terraform
TR["registry.terraform.io"] --> TP["2,000+ Providers"]
TR --> TM["15,000+ Modules"]
HCP["HCP Terraform"] --> PMC["Private Module Catalog"]
end
subgraph OpenTofu
OTR["registry.opentofu.org"] --> OTP["2,000+ Providers (mirror)"]
OTR --> OTM["Modules (mirror)"]
OCI["OCI Registry (1.10+)"] --> OCIPR["Providers via Docker Hub / GHCR"]
OCI --> OCIM["Modules via OCI artifacts"]
end
style TR fill:#5C4EE5,stroke:#fff,color:#fff
style OTR fill:#FFDA18,stroke:#333,color:#333
style OCI fill:#4CAF50,stroke:#fff,color:#fff
style HCP fill:#5C4EE5,stroke:#fff,color:#fff
Hình 1: Hệ sinh thái Registry — Terraform dựa vào registry trung tâm, OpenTofu bổ sung OCI Registry phi tập trung
OpenTofu 1.10 giới thiệu OCI Registry Support — cho phép phân phối providers và modules qua container registries (Docker Hub, GitHub Container Registry, AWS ECR). Đây là bước tiến lớn cho enterprise muốn air-gapped deployment hoặc kiểm soát supply chain chặt chẽ hơn:
# OpenTofu 1.10 — Lấy provider từ OCI registry
terraform {
required_providers {
aws = {
source = "oci://ghcr.io/my-org/providers/aws"
version = "~> 5.80"
}
}
}
3.3. Provider for_each — Multi-region đơn giản hơn
Một trong những pain points lớn nhất của Terraform: quản lý multi-region infrastructure đòi hỏi khai báo mỗi provider alias riêng biệt. OpenTofu 1.9 giải quyết triệt để với for_each trên provider blocks:
# OpenTofu 1.9+ — Provider for_each
variable "regions" {
default = ["ap-southeast-1", "us-east-1", "eu-west-1"]
}
provider "aws" "multi" {
for_each = toset(var.regions)
region = each.value
}
resource "aws_s3_bucket" "regional" {
for_each = toset(var.regions)
provider = aws.multi[each.value]
bucket = "my-app-${each.value}"
}
# So sánh: Terraform yêu cầu khai báo từng alias
provider "aws" {
alias = "ap_southeast_1"
region = "ap-southeast-1"
}
provider "aws" {
alias = "us_east_1"
region = "us-east-1"
}
provider "aws" {
alias = "eu_west_1"
region = "eu-west-1"
}
# ... lặp lại cho mỗi resource
3.4. Terraform Stacks — Quản lý multi-component deployments
Ngược lại, Terraform có Terraform Stacks (GA từ 2025) — tính năng mà OpenTofu chưa có tương đương. Stacks cho phép tổ chức nhiều Terraform configurations thành một đơn vị triển khai duy nhất, giải quyết bài toán orchestration giữa các components phụ thuộc nhau:
# Terraform Stack — tfstack.hcl
component "networking" {
source = "./modules/networking"
inputs = {
vpc_cidr = var.vpc_cidr
}
}
component "database" {
source = "./modules/database"
inputs = {
subnet_ids = component.networking.private_subnet_ids
# Tự động resolve dependency order
}
}
component "application" {
source = "./modules/application"
inputs = {
db_endpoint = component.database.endpoint
vpc_id = component.networking.vpc_id
}
}
# Deploy toàn bộ stack: networking → database → application
# Rollback: application → database → networking
Khi nào Stacks thực sự hữu ích?
Stacks phát huy tác dụng khi bạn có nhiều Terraform workspaces phụ thuộc nhau và hiện tại đang dùng Terragrunt, custom scripts, hoặc CI pipeline để orchestrate. Với team nhỏ chỉ có 1-2 configurations, Stacks thêm complexity không cần thiết. Lưu ý: Stacks hiện chỉ khả dụng trên HCP Terraform (cloud), không có trong CLI open-source.
3.5. Project Infragraph & MCP Servers — AI-driven Infrastructure
Đây là chiến lược dài hạn lớn nhất của HashiCorp/IBM cho Terraform. Project Infragraph (private beta 12/2025) là một infrastructure graph thời gian thực kết nối resources, applications, services và ownership metadata:
graph TB
subgraph "Project Infragraph"
IG["Infrastructure Graph
(Real-time)"]
IG --> RES["Resources
EC2, RDS, S3..."]
IG --> SVC["Services
APIs, Workers..."]
IG --> OWN["Ownership
Teams, RBAC..."]
IG --> POL["Policies
Sentinel, OPA..."]
end
subgraph "AI Agents"
MCP["MCP Server
(Terraform)"] --> IG
VMCP["MCP Server
(Vault)"] --> IG
AGENT["AI Agent
(Claude, GPT...)"] --> MCP
AGENT --> VMCP
end
AGENT -->|"Ngôn ngữ tự nhiên"| Q[""Scale web tier to 10 instances
in ap-southeast-1""]
MCP -->|"Plan + Apply"| RES
style IG fill:#5C4EE5,stroke:#fff,color:#fff
style MCP fill:#e94560,stroke:#fff,color:#fff
style AGENT fill:#4CAF50,stroke:#fff,color:#fff
Hình 2: Project Infragraph + MCP Servers — AI agents quản lý infrastructure qua ngôn ngữ tự nhiên
HCP Terraform MCP Server (beta) cho phép AI agents tương tác với infrastructure bằng ngôn ngữ tự nhiên — trigger workspace runs, query resource states, và nhận insights từ registries. Đây là hướng đi Agentic Infrastructure mà IBM/HashiCorp đặt cược lớn.
OpenTofu 1.10 cũng có MCP server riêng, nhưng tập trung vào việc cho phép AI agents viết và validate HCL code, không phải orchestrate infrastructure graph.
4. Bảng so sánh tổng hợp
| Tính năng | Terraform 1.10+ (BSL) | OpenTofu 1.10 (MPL) |
|---|---|---|
| License | BSL 1.1 (source-available) | MPL 2.0 (OSI open-source) |
| Governance | IBM/HashiCorp | Linux Foundation / CNCF |
| State encryption | Chỉ qua backend hoặc HCP | Native (AWS KMS, GCP KMS, PBKDF2, Vault) |
| S3 state locking | Cần DynamoDB table | Native S3 conditional writes |
| Provider for_each | Chưa hỗ trợ | Có (từ v1.9) |
| OCI Registry | Chưa hỗ trợ | Có (từ v1.10) |
| Terraform Stacks | GA (HCP only) | Không có tương đương |
| Project Infragraph | Private beta | Không có |
| MCP Servers | Terraform + Vault + Vault Radar | Community MCP server |
| OpenTelemetry | Không native | Native tracing (từ v1.10) |
| Early variable eval | Không | Có (từ v1.8) |
| Cloud platform | HCP Terraform (free tier mạnh) | Spacelift, Scalr, env0, Terramate |
| Enterprise support | IBM SC2 — 6 năm lifecycle | Các vendor bên thứ ba |
5. Kiến trúc IaC Pipeline thực tế
Dù chọn Terraform hay OpenTofu, một IaC pipeline production cần các thành phần sau. Sơ đồ dưới đây minh hoạ kiến trúc tham chiếu áp dụng được cho cả hai:
graph LR
DEV["Developer"] -->|"git push"| GIT["Git Repository"]
GIT -->|"webhook"| CI["CI Pipeline
(GitHub Actions)"]
CI --> LINT["tflint / trivy
Static Analysis"]
LINT --> PLAN["tofu plan / tf plan"]
PLAN --> REVIEW["Plan Review
(PR Comment)"]
REVIEW -->|"approve"| APPLY["tofu apply / tf apply"]
APPLY --> STATE["State Backend
(S3 + Encryption)"]
APPLY --> CLOUD["Cloud Resources
(AWS/Azure/CF)"]
STATE --> DRIFT["Drift Detection
(Scheduled)"]
DRIFT -->|"drift found"| ALERT["Alert → Slack/Teams"]
style CI fill:#e94560,stroke:#fff,color:#fff
style PLAN fill:#ff9800,stroke:#fff,color:#fff
style APPLY fill:#4CAF50,stroke:#fff,color:#fff
style STATE fill:#2c3e50,stroke:#fff,color:#fff
Hình 3: IaC Pipeline chuẩn — lint → plan → review → apply → drift detection
# GitHub Actions — IaC Pipeline (tương thích cả Terraform và OpenTofu)
name: Infrastructure Deploy
on:
pull_request:
paths: ['infra/**']
push:
branches: [main]
paths: ['infra/**']
permissions:
id-token: write # OIDC authentication
contents: read
pull-requests: write
jobs:
plan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup OpenTofu
uses: opentofu/setup-opentofu@v1
with:
tofu_version: "1.10.1"
- name: Configure AWS Credentials (OIDC)
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789:role/github-oidc
aws-region: ap-southeast-1
- name: Init
run: tofu init
working-directory: infra/
- name: Plan
id: plan
run: tofu plan -no-color -out=tfplan
working-directory: infra/
- name: Comment Plan on PR
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const output = `${{ steps.plan.outputs.stdout }}`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '#### OpenTofu Plan\n```\n' + output + '\n```'
});
apply:
needs: plan
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- uses: opentofu/setup-opentofu@v1
- run: tofu init && tofu apply -auto-approve
working-directory: infra/
6. Quản lý Multi-Cloud: AWS + Azure + Cloudflare
Một trong những sức mạnh lớn nhất của IaC là quản lý multi-cloud từ một codebase duy nhất. Ví dụ thực tế: website trên Cloudflare Workers, database trên AWS RDS, authentication trên Azure AD:
# Multi-cloud configuration — hoạt động giống nhau trên Terraform và OpenTofu
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.80"
}
azurerm = {
source = "hashicorp/azurerm"
version = "~> 4.20"
}
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 5.0"
}
}
}
# AWS — Database
resource "aws_db_instance" "main" {
engine = "postgres"
engine_version = "18.1"
instance_class = "db.t4g.medium"
db_name = "myapp"
# Free tier eligible
allocated_storage = 20
storage_type = "gp3"
}
# Azure — Authentication (Azure AD B2C)
resource "azurerm_aadb2c_directory" "auth" {
country_code = "VN"
data_residency_location = "Asia Pacific"
display_name = "MyApp Auth"
domain_name = "myappauth.onmicrosoft.com"
sku_name = "PremiumP1"
}
# Cloudflare — Edge delivery (Workers + DNS)
resource "cloudflare_workers_script" "api" {
account_id = var.cloudflare_account_id
name = "api-worker"
content = file("workers/api.js")
module = true
}
resource "cloudflare_workers_route" "api" {
zone_id = var.cloudflare_zone_id
pattern = "api.myapp.com/*"
script_name = cloudflare_workers_script.api.name
}
Chi phí multi-cloud IaC
Cả Terraform CLI lẫn OpenTofu đều miễn phí cho việc quản lý multi-cloud. Chi phí chỉ phát sinh từ: (1) cloud resources thực tế bạn provision, (2) state storage (S3 bucket ~$0.02/GB/tháng), (3) CI/CD compute (GitHub Actions free tier: 2,000 phút/tháng). Một startup có thể quản lý toàn bộ infrastructure AWS + Cloudflare với tổng chi phí IaC tooling gần bằng 0.
7. Migration Guide — Chuyển đổi giữa Terraform và OpenTofu
7.1. Terraform → OpenTofu
Với các phiên bản Terraform ≤ 1.5.x (trước BSL), migration là binary swap — gần như thay thế trực tiếp:
# Bước 1: Backup state file
cp terraform.tfstate terraform.tfstate.backup
# Bước 2: Cài OpenTofu
curl -fsSL https://get.opentofu.org/install-opentofu.sh | sh
# Bước 3: Init lại (không cần thay đổi code)
tofu init -upgrade
# Bước 4: Plan để verify
tofu plan
# Output phải hiển thị: "No changes. Your infrastructure matches the configuration."
# Bước 5: Alias cho team quen dần
echo 'alias tf="tofu"' >> ~/.bashrc
Lưu ý quan trọng về state compatibility
Sau khi chạy tofu apply với OpenTofu 1.7+, state file có thể chứa metadata hoặc encryption markers khiến nó không đọc được bởi Terraform CLI nữa. Đây là one-way migration nếu bạn bật state encryption. Hãy đảm bảo team đã thống nhất quyết định trước khi apply.
7.2. OpenTofu → Terraform
Chiều ngược lại phức tạp hơn nếu bạn đã dùng các tính năng exclusive của OpenTofu:
| Tính năng OpenTofu | Tương đương Terraform | Mức độ effort migration |
|---|---|---|
| State encryption | Tắt encryption, rely on backend encryption | Thấp — tofu state pull ra plaintext rồi terraform state push |
| Provider for_each | Khai báo alias thủ công | Trung bình — refactor code |
| OCI Registry sources | Đổi về registry.terraform.io | Thấp — sửa source URLs |
| S3 native locking | Thêm DynamoDB table | Thấp — tạo table + cập nhật backend config |
| Early variable eval | Dùng workaround (locals, -var flag) | Trung bình |
8. Chọn công cụ nào? Decision Framework
graph TD
START["Bạn cần IaC tool"] --> Q1{"Xây dựng managed
IaC platform/SaaS?"}
Q1 -->|"Có"| OT["→ OpenTofu
(License an toàn)"]
Q1 -->|"Không"| Q2{"Cần state encryption
native trong CLI?"}
Q2 -->|"Có"| OT
Q2 -->|"Không"| Q3{"Cần Terraform Stacks
hoặc Project Infragraph?"}
Q3 -->|"Có"| TF["→ Terraform + HCP
(Hệ sinh thái mạnh)"]
Q3 -->|"Không"| Q4{"Team size?"}
Q4 -->|"< 20 devs"| Q5{"Budget cho
HCP Terraform?"}
Q4 -->|"≥ 20 devs"| Q6{"Ưu tiên vendor-neutral
hay enterprise support?"}
Q5 -->|"Có"| TF
Q5 -->|"Không"| OT
Q6 -->|"Vendor-neutral"| OT
Q6 -->|"Enterprise support"| TF
style OT fill:#FFDA18,stroke:#333,color:#333
style TF fill:#5C4EE5,stroke:#fff,color:#fff
style START fill:#e94560,stroke:#fff,color:#fff
Hình 4: Decision tree — chọn Terraform hay OpenTofu dựa trên nhu cầu thực tế
Khuyến nghị thực tế cho team Việt Nam 2026
Startup / Team nhỏ (< 10 người): Chọn OpenTofu. Miễn phí hoàn toàn, state encryption sẵn, không lo license. Kết hợp với GitHub Actions free tier và S3 backend.
Enterprise đang dùng Terraform: Không cần vội chuyển. Đánh giá xem có cần state encryption hay provider for_each không. Nếu đã đầu tư vào HCP Terraform, hệ sinh thái Stacks + Infragraph + MCP là lợi thế lớn.
Team xây dựng Internal Developer Platform: OpenTofu + Spacelift/Scalr là combo mạnh và hoàn toàn open-source friendly.
9. Observability cho IaC Pipeline
OpenTofu 1.10 tích hợp sẵn OpenTelemetry tracing — mỗi tofu plan và tofu apply tạo ra spans có thể gửi đến collector. Điều này cho phép đo:
- Plan duration theo module và provider — phát hiện bottleneck
- Apply duration cho từng resource — tìm resource chậm nhất
- Error rate theo provider — AWS API throttling hay Azure rate limits
- State operation latency — lock contention hay S3 slowness
# Bật OpenTelemetry cho OpenTofu
export OTEL_TRACES_EXPORTER=otlp
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
tofu plan # Traces tự động gửi đến collector
tofu apply # Spans cho mỗi resource operation
Terraform CLI không có OpenTelemetry native. Bạn cần wrap commands trong CI pipeline và tự instrument bằng external tools.
10. Kết luận
Cuộc chia tách Terraform/OpenTofu năm 2023 đã cho ra đời hai sản phẩm với triết lý khác nhau rõ rệt vào 2026:
- Terraform dưới IBM hướng đến enterprise platform play — Stacks, Infragraph, MCP servers, FinOps integration. Sức mạnh nằm ở hệ sinh thái HCP và vision Agentic Infrastructure. Trade-off: license BSL, nhiều tính năng hay nhất chỉ có trên cloud platform.
- OpenTofu dưới Linux Foundation hướng đến community-driven innovation — state encryption, OCI registry, provider for_each, OpenTelemetry native. Sức mạnh nằm ở tính mở, tốc độ ship tính năng, và vendor neutrality. Trade-off: thiếu enterprise platform tích hợp, ecosystem nhỏ hơn về commercial support.
Tin tốt: HCL code vẫn tương thích cao giữa hai tool. Bạn có thể bắt đầu với một trong hai và migration khi cần — miễn là chưa dùng các tính năng exclusive. Hãy đưa ra quyết định dựa trên nhu cầu license, tính năng cần thiết và chiến lược platform của team, thay vì chạy theo trend.
Nguồn tham khảo
- OpenTofu 1.10.0 Release — opentofu.org
- HashiCorp Year in Review 2025 — hashicorp.com
- Project Infragraph Announcement — IBM Newsroom
- OpenTofu vs Terraform Key Differences — Spacelift
- OpenTofu vs Terraform in 2026: Is the Fork Finally Worth It? — DEV Community
- Terraform vs OpenTofu in 2026: Should You Stay or Switch? — Medium
- OpenTofu 1.10 Released with OCI Registry Support — InfoQ
- Terraform Features at HashiConf 2025 — hashicorp.com
TypeScript 6.0: Type System thông minh hơn, Decorator Metadata và hiệu năng đột phá
Shared Dictionaries — Khi trình duyệt chỉ tải những gì thực sự thay đổi
Disclaimer: The opinions expressed in this blog are solely my own and do not reflect the views or opinions of my employer or any affiliated organizations. The content provided is for informational and educational purposes only and should not be taken as professional advice. While I strive to provide accurate and up-to-date information, I make no warranties or guarantees about the completeness, reliability, or accuracy of the content. Readers are encouraged to verify the information and seek independent advice as needed. I disclaim any liability for decisions or actions taken based on the content of this blog.