คู่มือแบบละเอียด Step-by-Step สำหรับติดตั้งและเริ่มใช้งาน OpenTofu บน Ubuntu 22.04/24.04
OpenTofu เป็น Infrastructure as Code (IaC) tool ที่เกิดขึ้นจากชุมชนหลัง HashiCorp เปลี่ยนใบอนุญาตในปี 2024 จาก Mozilla Public License (MPL) ไปยัง Business Source License (BSL) ที่มีข้อจำกัดมากขึ้น
OpenTofu ทำงานเป็น bridge ระหว่าง configuration และ cloud providers
| Features | OpenTofu | Terraform (2025) |
|---|---|---|
| License Type | MPL 2.0 (Open Source) | BSL (Restricted) |
| Cost for Self-Hosted | Free | Free (basic) |
| Terraform Cloud | Not included | Paid features |
| Provider Support | 100% Compatible | 100% Native |
| State File Format | Identical | Native |
| Community Development | Active & Open | HashiCorp Controlled |
| Vendor Lock-in | None | High (via Cloud) |
| CLI Command | tofu |
terraform |
CPU
2 vCPU minimum (4 recommended)
RAM
4 GB minimum (8 GB recommended)
Storage
20 GB free space
Operating System
Ubuntu 22.04 LTS / Ubuntu 24.04 LTS
Permissions
sudo access required
Cloud Account
วิธีนี้แนะนำที่สุดสำหรับผู้ใช้ทั่วไป เพราะมีการอัปเดตอัตโนมัติและความเสถียรสูง
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/opentofu-archive-keyring.gpg
คำอธิบาย: นำ enterprise GPG key ของ HashiCorp (ใช้กับ OpenTofu ด้วย) มา importing
echo "deb [signed-by=/usr/share/keyrings/opentofu-archive-keyring.gpg] https://apt.opentofu.org $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/opentofu.list
คำอธิบาย: เพิ่ม OpenTofu repository ลงใน source list
sudo apt update && sudo apt install opentofu
ตอบ Y เพื่อยืนยันการติดตั้ง
tofu --version
ควรเห็น output ประมาณ: OpenTofu v1.8.0
sudo apt update && sudo apt upgradeวิธีนี้เหมาะสำหรับ Ubuntu ที่มี snapd ติดตั้งอยู่แล้ว ติดตั้งง่ายและ rollback ได้ง่าย
sudo apt update && sudo apt install snapd
sudo snap install opentofu --classic
ใช้ flag --classic เพราะ OpenTofu ต้องการ access ระบบหลายส่วน
tofu --version
sudo snap revert opentofuวิธีนี้เหมาะสำหรับผู้ที่ต้องการ control เวอร์ชันอย่างละเอียด หรือใน environment ที่ไม่มี internet
wget https://github.com/opentofu/opentofu/releases/download/v1.8.0/opentofu_1.8.0_linux_amd64.zip
เปลี่ยนเวอร์ชันเป็นเวอร์ชันล่าสุดจาก Releases page
unzip opentofu_1.8.0_linux_amd64.zip
sudo mv tofu /usr/local/bin/
sudo chmod +x /usr/local/bin/tofu
tofu --version
วิธีนี้เหมาะสำหรับผู้ที่ต้องการ modify source code หรือ test feature ใหม่ๆ ก่อน release
sudo apt update && sudo apt install golang-go
git clone https://github.com/opentofu/opentofu.git && cd opentofu
make bootstrap && make dev
ใช้ time ประมาณ 5-10 นาทีในการ build
./bin/tofu --version
ให้สร้างไฟล์ชื่อ main.tf และเริ่มต้นเขียน configuration
ตัวอย่างง่ายๆ ที่สุด - สร้างไฟล์ hello.txt บน local machine
# main.tf - Hello World Example
terraform {
required_version = ">= 1.0.0"
required_providers {
local = {
source = "hashicorp/local"
version = "2.4.0"
}
}
}
provider "local" {
# Configuration (optional)
}
resource "local_file" "hello" {
content = "Hello OpenTofu! This is my first IaC deployment."
filename = "${path.module}/hello.txt"
}
tofu init
Initialize the working directory
tofu plan
Show execution plan
tofu apply
Apply changes (type 'yes' when prompted)
ตัวอย่างการสร้าง EC2 instance บน AWS (ต้องตั้งค่า credentials ก่อน)
# main.tf - AWS EC2 Example
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "ap-southeast-1" # Asia Pacific (Singapore)
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "opentofu-ec2-vpc"
}
}
resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "ap-southeast-1a"
map_public_ip_on_launch = true
tags = {
Name = "opentofu-ec2-subnet"
}
}
resource "aws_security_group" "web" {
name = "web-sg"
description = "Allow SSH and HTTP"
vpc_id = aws_vpc.main.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "web-sg"
}
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0" # Ubuntu 22.04 LTS (adjust per region)
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.web.id]
subnet_id = aws_subnet.main.id
tags = {
Name = "opentofu-ec2-instance"
}
user_data = <<-EOF
#!/bin/bash
apt update -y
apt install nginx -y
echo "Hello from OpenTofu EC2!" > /var/www/html/index.html
service nginx start
EOF
}
aws configureตัวอย่างสำหรับ DigitalOcean โดยใช้ environment variables
# main.tf - DigitalOcean Example
terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0"
}
}
}
provider "digitalocean" {
token = var.do_token
}
variable "do_token" {
description = "DigitalOcean API Token"
type = string
sensitive = true
}
resource "digitalocean_droplet" "web" {
image = "ubuntu-22-04-x64"
name = "opentofu-droplet"
region = "sgp1"
size = "s-1vcpu-1gb"
tags = ["opentofu", "web"]
provisioner "local-exec" {
command = "echo Droplet IP: ${self.ipv4_address}"
}
}
ตั้งค่า environment variable:
export DO_TOKEN="your_token_here"
Backend คือที่ที่ OpenTofu เก็บ state file ซึ่งจำเป็นสำหรับการทำงานแบบทีม การใช้ remote backend ช่วยให้ several developers สามารถทำงานร่วมกันได้
เก็บ state ไว้ในไฟล์ terraform.tfstate บน local machine
terraform.tfstate (local)
เหมาะสำหรับ: Development หรือ single developer
เก็บ state ไว้บน cloud storage พร้อม locking
เหมาะสำหรับ: Production หรือ team collaboration
ตัวอย่างการตั้งค่า remote backend ด้วย AWS S3 และ DynamoDB for state locking
# backend.tf - Remote Backend Configuration
terraform {
backend "s3" {
# แก้ไขค่าเหล่านี้ตาม bucket ของคุณ
bucket = "my-terraform-state-bucket"
key = "prod/terraform.tfstate"
region = "ap-southeast-1"
# DynamoDB table สำหรับ state locking
dynamodb_table = "terraform-state-locks"
# ให้ lock state ด้วย customer master key
encrypt = true
kms_key_id = "alias/terraform-state"
}
}
1.ย้าย state file ไปยัง S3:
tofu init -migrate-state
คำสั่งนี้จะถามว่าอยาก migrate หรือไม่ - ตอบ yes
2.ตรวจสอบ state หลัง migrate:
tofu state list
สำหรับ testing หรือ private server ที่ไม่ใช่ cloud
# backend.tf - HTTP Backend
terraform {
backend "http" {
address = "https://terraform.yourcompany.com/state/prod"
lock_address = "https://terraform.yourcompany.com/lock/prod"
unlock_address = "https://terraform.yourcompany.com/lock/prod"
username = "your_username"
password = "your_password"
}
}
| Operation | Terraform | OpenTofu | Description |
|---|---|---|---|
| Initialize | terraform init |
tofu init |
Initialize working directory |
| Validate | terraform validate |
tofu validate |
Validate configuration |
| Plan | terraform plan |
tofu plan |
Show execution plan |
| Apply | terraform apply |
tofu apply |
Apply changes |
| Destroy | terraform destroy |
tofu destroy |
Destroy resources |
| State List | terraform state list |
tofu state list |
List state resources |
| State Show | terraform state show |
tofu state show |
Show resource state |
| Output | terraform output |
tofu output |
Show output values |
| .providers | terraform providers |
tofu providers |
Show providers |
| Version | terraform version |
tofu version |
Show version |
สรุป: คำสั่งทั้งหมดเหมือนกัน ต่างกันแค่เปลี่ยนจาก terraform เป็น tofu
ดีข่าวดี! การย้ายจาก Terraform ไป OpenTofu ทำได้ง่ายมาก เพราะ state file format และ syntax เหมือนกัน 100%
Download and Install OpenTofu
ติดตั้ง OpenTofu ตามวิธีการที่ 1-3
Change Command
เปลี่ยน terraform เป็น tofu
Verify
tofu plan เพื่อตรวจสอบ state
Apply
ถ้าใช้ AWS IAM Role แทน access keys:
1.สร้าง IAM policy:
s3:GetObject, s3:PutObject, dynamodb:GetItem, dynamodb:PutItem
2.ตั้งค่า AWS CLI profile:
aws configure --profile opentofu
3.ใช้ profile ใน config:
profile = "opentofu"
ตรวจสอบ Terraform version:
terraform version
ตรวจสอบสถานะ state:
terraform state list
A: ให้ตรวจสอบว่า path ของ tofu ถูกต้องหรือไม่
# ตรวจสอบ path
which tofu
# หรือ
echo $PATH
หาก tofu ไม่อยู่ใน path, เพิ่มด้วย:
export PATH=$PATH:/usr/local/bin
หรือแก้ไขผ่าน ~/.bashrc หรือ ~/.zshrc
A: OpenTofu ยังไม่รองรับ registry ตรงๆ - ต้องระบุ provider source อย่างละเอียด
แก้ไข terraform config:
# ไม่ถูกต้อง
terraform {
required_providers {
aws = {
version = "~> 5.0"
}
}
}
# ถูกต้อง
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
A: HashiCorp enterprise GPG key ใช้กับ OpenTofu ได้ - หรือใช้วิธีอื่นแทน
ลองใช้วิธี Download Binary แทน:
wget https://github.com/opentofu/opentofu/releases/latest/download/opentofu_linux_amd64.zip
A: ง่ายมาก - เพียงแค่ uninstall OpenTofu และติดตั้ง Terraform ใหม่
Uninstall OpenTofu (from deb):
sudo apt remove opentofu && sudo apt autoremove
ติดตั้ง Terraform:
sudo apt install terraform
หมายเหตุ: state file ยังคงอยู่เดิม - Terraform สามารถอ่านได้
A: ต้อง init ใหม่ด้วย flag -reconfigure
tofu init -reconfigure
หรือเลือก backend ใหม่:
tofu init -backend-config=backend.hcl
ติดตั้งเรียบร้อย
OpenTofu บน Ubuntu
First Config
Hello World หรือ AWS EC2
Deploy!
สร้าง resources บน cloud