Skip to main content

Installing Terraform

ติดตั้ง Terraform บน macOS / Linux / Windows + verify การติดตั้ง — ใช้เวลาประมาณ 5 นาที

วิธีติดตั้งตาม OS

macOS (Homebrew — แนะนำ)

brew tap hashicorp/tap
brew install hashicorp/tap/terraform

macOS / Linux (manual download)

# 1. Download
curl -O https://releases.hashicorp.com/terraform/1.9.8/terraform_1.9.8_darwin_arm64.zip

# 2. Unzip
unzip terraform_1.9.8_darwin_arm64.zip

# 3. Move to PATH
sudo mv terraform /usr/local/bin/

เปลี่ยน darwin_arm64 เป็น OS+arch ของคุณ:

  • linux_amd64 (Linux x86_64)
  • linux_arm64 (Linux ARM)
  • darwin_amd64 (macOS Intel)
  • windows_amd64 (Windows)

ดู version ทั้งหมดที่ releases.hashicorp.com

Linux (apt — Ubuntu/Debian)

wget -O- https://apt.releases.hashicorp.com/gpg | \
sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list

sudo apt update && sudo apt install terraform

Linux (yum — RHEL/CentOS)

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum install terraform

Windows (Chocolatey)

choco install terraform

หรือ Scoop:

scoop install terraform

Verify การติดตั้ง

terraform version

ควรเห็น:

Terraform v1.9.8
on darwin_arm64
ใช้ tfenv จัดการหลาย version

ถ้าต้อง maintain หลาย project ที่ใช้ Terraform คนละเวอร์ชัน → ใช้ tfenv

brew install tfenv
tfenv install 1.5.7
tfenv install 1.9.8
tfenv use 1.9.8

ใส่ version ใน .terraform-version ของแต่ละ project แล้ว tfenv จะ auto switch ให้

ตั้งค่า AWS Credentials (สำหรับ tutorial)

ส่วนใหญ่จะลอง deploy บน AWS ก่อน — ต้องมี AWS account + credentials

1. ติดตั้ง AWS CLI

# macOS
brew install awscli

# Linux
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip && sudo ./aws/install

2. Configure credentials

aws configure

กรอก:

AWS Access Key ID:     AKIA...
AWS Secret Access Key: ****
Default region name: ap-southeast-1
Default output format: json
Security
  • ห้าม commit credentials เข้า Git
  • ใช้ IAM User ที่มีสิทธิ์เท่าที่จำเป็นเท่านั้น
  • production ใช้ IAM Role + AssumeRole หรือ AWS SSO จะปลอดภัยกว่า

3. Verify

aws sts get-caller-identity

ควรเห็น account info ของคุณ

Hello World — ลองใช้จริง

สร้าง folder ใหม่:

mkdir terraform-hello && cd terraform-hello

สร้างไฟล์ main.tf:

main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}

provider "aws" {
region = "ap-southeast-1"
}

# Random suffix กันชื่อชน (S3 bucket name unique ทั่ว AWS)
resource "random_id" "suffix" {
byte_length = 4
}

resource "aws_s3_bucket" "hello" {
bucket = "tf-hello-${random_id.suffix.hex}"
}

output "bucket_name" {
value = aws_s3_bucket.hello.id
}

รัน:

terraform init      # Download AWS provider (~30 วินาที)
terraform plan # Preview สิ่งที่จะสร้าง
terraform apply # พิมพ์ "yes" เพื่อยืนยัน

→ ได้ S3 bucket จริงบน AWS! 🎉

ลบทิ้ง:

terraform destroy

Editor Setup (ทำให้ DX ดีขึ้น)

VS Code

ติดตั้ง extension: HashiCorp Terraform (id: HashiCorp.terraform)

ได้ syntax highlighting + autocomplete + format on save

Vim/Neovim

Plug 'hashivim/vim-terraform'

IntelliJ / GoLand

ติดตั้ง HashiCorp Terraform plugin

สรุป

  • ติดตั้ง Terraform: brew install terraform (macOS) หรือ download zip
  • Verify: terraform version
  • AWS: aws configure + IAM credentials
  • Hello world: terraform init && terraform apply
  • Editor: VS Code + HashiCorp Terraform extension

ต่อไป → Section 2: HCL Language