อัปเดตเมื่อ 2026 - Latest Guide

การติดตั้ง OpenTofu บน Ubuntu

คู่มือแบบละเอียด Step-by-Step สำหรับติดตั้งและเริ่มใช้งาน OpenTofu บน Ubuntu 22.04/24.04

v1.8.0+ Ubuntu 22.04/24.04 Community-Driven

บทนำ - OpenTofu คืออะไร?

OpenTofu เป็น Infrastructure as Code (IaC) tool ที่เกิดขึ้นจากชุมชนหลัง HashiCorp เปลี่ยนใบอนุญาตในปี 2024 จาก Mozilla Public License (MPL) ไปยัง Business Source License (BSL) ที่มีข้อจำกัดมากขึ้น

จุดเด่นของ OpenTofu

  • 100% Open Source - MPL 2.0 license
  • Terraform Compatible - syntax และ provider เหมือน Terraform
  • Community-Driven - พัฒนาโดยชุมชน ไม่มี vendor lock-in
  • State Compatibility - ใช้ state file เหมือน Terraform

ใช้เมื่อไหร่?

  • ต้องการ IaC tool แบบ Open Source แท้จริง
  • ใช้งาน Terraform อยู่แล้วและต้องการ switch
  • ต้องการ community support และ development
  • ไม่ใช้ Terraform Cloud features เฉพาะ

โครงสร้างระบบ - OpenTofu Architecture

Developer Ubuntu/Linux OpenTofu CLI tofu command AWS Provider Azure Provider GCP Provider Terraform Cloud Paid Features terraform config → execution plan

OpenTofu ทำงานเป็น bridge ระหว่าง configuration และ cloud providers

ทำไมต้องเลือก OpenTofu?

เปรียบเทียบ: OpenTofu vs Terraform

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

For DevOps Teams

  • ไม่มี vendor lock-in - ใช้งานได้อย่างอิสระ
  • Community-driven - พัฒนาแบบ transparent
  • Zero cost - ไม่มีค่าใช้จ่ายสำหรับการใช้งาน
  • State migration - ย้ายจาก Terraform ได้ง่าย

For Organizations

  • Open Governance - ไม่ขึ้นกับบริษัทเดียว
  • Security - ตรวจสอบ source code ได้เอง
  • Customization - ปรับแต่งได้ตามต้องการ
  • Long-term viability - อนาคตของ IaC tool

สิ่งที่ต้องเตรียม - Prerequisites

Hardware Requirements

CPU

2 vCPU minimum (4 recommended)

RAM

4 GB minimum (8 GB recommended)

Storage

20 GB free space

Software Requirements

Operating System

Ubuntu 22.04 LTS / Ubuntu 24.04 LTS

Permissions

sudo access required

Cloud Account

วิธีการติดตั้ง OpenTofu

1

วิธีที่ 1: ติดตั้งจาก Official Repository (Deb Package)

วิธีนี้แนะนำที่สุดสำหรับผู้ใช้ทั่วไป เพราะมีการอัปเดตอัตโนมัติและความเสถียรสูง

ขั้นตอนที่ 1: Import GPG Key

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

ขั้นตอนที่ 2: Add Repository

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

ขั้นตอนที่ 3: Update Package & Install

sudo apt update && sudo apt install opentofu

ตอบ Y เพื่อยืนยันการติดตั้ง

ขั้นตอนที่ 4: Verify Installation

tofu --version

ควรเห็น output ประมาณ: OpenTofu v1.8.0

ข้อดีของวิธีนี้

  • • การอัปเดตอัตโนมัติผ่าน sudo apt update && sudo apt upgrade
  • • ความเสถียรสูงเนื่องจากเป็น official repository
  • • ไม่ต้องจัดการ binary ด้วยตนเอง
  • • มี signing key verification
2

วิธีที่ 2: ติดตั้งจาก Snap Store

วิธีนี้เหมาะสำหรับ Ubuntu ที่มี snapd ติดตั้งอยู่แล้ว ติดตั้งง่ายและ rollback ได้ง่าย

ขั้นตอนที่ 1: ติดตั้ง Snapd (ถ้ายังไม่มี)

sudo apt update && sudo apt install snapd

ขั้นตอนที่ 2: ติดตั้ง OpenTofu

sudo snap install opentofu --classic

ใช้ flag --classic เพราะ OpenTofu ต้องการ access ระบบหลายส่วน

ขั้นตอนที่ 3: Verify Installation

tofu --version

ข้อดีของวิธีนี้

  • • ไม่ต้องจัดการ repository เอง
  • • Rollback ง่ายด้วยคำสั่ง sudo snap revert opentofu
  • • ติดตั้งในหลาย distributionได้
3

วิธีที่ 3: Download Binary ด้วยตนเอง

วิธีนี้เหมาะสำหรับผู้ที่ต้องการ control เวอร์ชันอย่างละเอียด หรือใน environment ที่ไม่มี internet

ขั้นตอนที่ 1: Download Binary

wget https://github.com/opentofu/opentofu/releases/download/v1.8.0/opentofu_1.8.0_linux_amd64.zip

เปลี่ยนเวอร์ชันเป็นเวอร์ชันล่าสุดจาก Releases page

ขั้นตอนที่ 2: Extract the Archive

unzip opentofu_1.8.0_linux_amd64.zip

ขั้นตอนที่ 3: Move to/bin Directory

sudo mv tofu /usr/local/bin/

ขั้นตอนที่ 4: Make Executable

sudo chmod +x /usr/local/bin/tofu

ขั้นตอนที่ 5: Verify Installation

tofu --version

ข้อดีของวิธีนี้

  • • Control เวอร์ชันได้เอง
  • • ใช้งานได้แม้ไม่มี internet
  • • ไม่ได้ติดตั้ง package manager มาก่อน
4

วิธีที่ 4: คอมไพล์จาก Source Code (สำหรับผู้เชี่ยวชาญ)

วิธีนี้เหมาะสำหรับผู้ที่ต้องการ modify source code หรือ test feature ใหม่ๆ ก่อน release

ขั้นตอนที่ 1: Install Go

sudo apt update && sudo apt install golang-go

ขั้นตอนที่ 2: Clone the Repository

git clone https://github.com/opentofu/opentofu.git && cd opentofu

ขั้นตอนที่ 3: Build from Source

make bootstrap && make dev

ใช้ time ประมาณ 5-10 นาทีในการ build

ขั้นตอนที่ 4: Verify Installation

./bin/tofu --version

ตัวอย่าง First Configuration

ให้สร้างไฟล์ชื่อ main.tf และเริ่มต้นเขียน configuration

ตัวอย่างที่ 1: Hello World - Local File

ตัวอย่างง่ายๆ ที่สุด - สร้างไฟล์ 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)

ตัวอย่างที่ 2: Create AWS EC2 Instance

ตัวอย่างการสร้าง 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 credentials ก่อน: aws configure
  • • AMI ID ต้องเปลี่ยนตาม region ที่ใช้งาน
  • • Instance type ที่ใช้ (t2.micro) อยู่ใน Free Tier (ต้อง check ว่า account อยู่ใน eligible)

ตัวอย่างที่ 3: DigitalOcean Droplet

ตัวอย่างสำหรับ 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 (Remote State)

Backend คือที่ที่ OpenTofu เก็บ state file ซึ่งจำเป็นสำหรับการทำงานแบบทีม การใช้ remote backend ช่วยให้ several developers สามารถทำงานร่วมกันได้

Local Backend (Default)

เก็บ state ไว้ในไฟล์ terraform.tfstate บน local machine

terraform.tfstate (local)

เหมาะสำหรับ: Development หรือ single developer

Remote Backend

เก็บ state ไว้บน cloud storage พร้อม locking

Amazon S3 + DynamoDB
Google Cloud Storage
Azure Blob Storage

เหมาะสำหรับ: Production หรือ team collaboration

ตัวอย่าง: AWS S3 + DynamoDB Backend

ตัวอย่างการตั้งค่า 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"
  }
}

คำสั่งการย้าย State ไป Remote Backend

1.ย้าย state file ไปยัง S3:

tofu init -migrate-state

คำสั่งนี้จะถามว่าอยาก migrate หรือไม่ - ตอบ yes

2.ตรวจสอบ state หลัง migrate:

tofu state list

ตัวอย่าง: Local Backend แบบ Remote (HTTP)

สำหรับ 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"
  }
}

คำสั่งที่ใช้บ่อย - Command Comparison

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

ดีข่าวดี! การย้ายจาก Terraform ไป OpenTofu ทำได้ง่ายมาก เพราะ state file format และ syntax เหมือนกัน 100%

การย้ายง่ายๆ (No Changes)

1

Download and Install OpenTofu

ติดตั้ง OpenTofu ตามวิธีการที่ 1-3

2

Change Command

เปลี่ยน terraform เป็น tofu

3

Verify

tofu plan เพื่อตรวจสอบ state

4

Apply

การย้ายด้วย AWS IAM Role

ถ้าใช้ 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"

เช็คสถานะก่อน migrate

ตรวจสอบ Terraform version:

terraform version

ตรวจสอบสถานะ state:

terraform state list

แก้ไขปัญหาที่พบบ่อย - FAQ

Q: ได้ error "tofu: command not found" ต้องทำอย่างไร?

A: ให้ตรวจสอบว่า path ของ tofu ถูกต้องหรือไม่

# ตรวจสอบ path which tofu # หรือ echo $PATH

หาก tofu ไม่อยู่ใน path, เพิ่มด้วย: export PATH=$PATH:/usr/local/bin หรือแก้ไขผ่าน ~/.bashrc หรือ ~/.zshrc

Q: ได้ error "provider registry registry.terraform.io is not available"?

A: OpenTofu ยังไม่รองรับ registry ตรงๆ - ต้องระบุ provider source อย่างละเอียด

แก้ไข terraform config:

# ไม่ถูกต้อง
terraform {
  required_providers {
    aws = {
      version = "~> 5.0"
    }
  }
}

# ถูกต้อง
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

Q: ได้ error "GPG key retrieval failed" เมื่อเพิ่ม repository?

A: HashiCorp enterprise GPG key ใช้กับ OpenTofu ได้ - หรือใช้วิธีอื่นแทน

ลองใช้วิธี Download Binary แทน:

wget https://github.com/opentofu/opentofu/releases/latest/download/opentofu_linux_amd64.zip

Q: ต้องการ rollback กลับไปใช้ Terraform ต้องทำอย่างไร?

A: ง่ายมาก - เพียงแค่ uninstall OpenTofu และติดตั้ง Terraform ใหม่

Uninstall OpenTofu (from deb):

sudo apt remove opentofu && sudo apt autoremove

ติดตั้ง Terraform:

sudo apt install terraform

หมายเหตุ: state file ยังคงอยู่เดิม - Terraform สามารถอ่านได้

Q: ได้ error "Backend initialization required" เมื่อเปลี่ยน backend?

A: ต้อง init ใหม่ด้วย flag -reconfigure

tofu init -reconfigure

หรือเลือก backend ใหม่: tofu init -backend-config=backend.hcl

สรุป - คุณพร้อมใช้งาน OpenTofu แล้ว!

ติดตั้งเรียบร้อย

OpenTofu บน Ubuntu

First Config

Hello World หรือ AWS EC2

Deploy!

สร้าง resources บน cloud

What's Next?

  • อ่าน documentation of OpenTofu Official Docs
  • ลองสร้าง infrastructure จริงบน AWS/Azure/GCP
  • ตั้งค่า CI/CD pipeline ด้วย GitLab CI หรือ GitHub Actions
  • join community ผ่าน GitHub และ Community
  • ติดตาม update ที่ Releases