Project Initialization
terraform initคือคำสั่งแรกของทุก Terraform project — มาดูว่ามันทำอะไร และทำไมต้องรัน
โครงสร้าง Terraform Project
my-project/
├── main.tf # resource หลัก
├── variables.tf # input variables
├── outputs.tf # outputs
├── providers.tf # provider config
├── terraform.tfvars # variable values
└── .terraform/ # ← ถูกสร้างหลัง terraform init (ห้าม commit!)
├── providers/
└── modules/
.terraform/ folder
ห้าม commit .terraform/ เข้า Git — เป็น cache ที่ใหญ่มาก (provider plugins) ใส่ใน .gitignore:
.gitignore
.terraform/
*.tfstate
*.tfstate.backup
.terraform.lock.hcl # ← optional, ดูข้างล่าง
terraform init ทำอะไรบ้าง?
terraform init
ใต้ผิวมัน:
- ✅ อ่าน
terraformblock เพื่อรู้ว่าใช้ provider ไหน + version อะไร - ✅ Download provider plugins จาก Terraform Registry
- ✅ Download modules ที่อ้างอิง (ถ้ามี)
- ✅ Initialize backend (state file ที่จะเก็บ — local หรือ remote)
- ✅ สร้าง
.terraform.lock.hcllock provider versions
ตัวอย่าง
main.tf
terraform {
required_version = ">= 1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.5"
}
}
}
provider "aws" {
region = "ap-southeast-1"
}
รัน:
$ terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 5.0"...
- Finding hashicorp/random versions matching "~> 3.5"...
- Installing hashicorp/aws v5.70.0...
- Installed hashicorp/aws v5.70.0 (signed by HashiCorp)
- Installing hashicorp/random v3.6.3...
- Installed hashicorp/random v3.6.3 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
.terraform.lock.hcl คืออะไร?
ไฟล์ที่ Terraform สร้างหลัง init — ล็อค version ของ provider ที่ใช้
.terraform.lock.hcl
provider "registry.terraform.io/hashicorp/aws" {
version = "5.70.0"
constraints = "~> 5.0"
hashes = [
"h1:pIG...",
"zh:0a...",
]
}
ควร commit หรือไม่?
ควร commit — ทำให้ทีมทุกคนใช้ provider version เดียวกัน → ไม่มี surprise ตอน apply
ถ้าอยาก upgrade:
terraform init -upgrade
Options ของ terraform init
# Upgrade provider ไป version ใหม่ที่สุด (ที่ตรง constraint)
terraform init -upgrade
# Reconfigure backend (เปลี่ยน backend จาก local → S3)
terraform init -reconfigure
# Migrate state จาก backend เก่าไปใหม่
terraform init -migrate-state
# Skip backend initialization
terraform init -backend=false
# Pass backend config ผ่าน CLI
terraform init \
-backend-config="bucket=my-tfstate" \
-backend-config="key=prod/terraform.tfstate"
เมื่อไหร่ต้องรัน terraform init ซ้ำ?
| เมื่อ | ต้องรันใหม่ไหม |
|---|---|
| Clone project ใหม่ | ✅ ครั้งแรก |
| เพิ่ม/ลบ provider ใน config | ✅ |
| เปลี่ยน provider version constraint | ✅ |
| เพิ่ม/ลบ module | ✅ |
| เปลี่ยน backend | ✅ |
| แก้แค่ resource | ❌ ไม่ต้อง |
git pull แล้วเพื่อนเพิ่ม provider | ✅ |
Auto-init
ใช้ pre-commit hook ให้รัน terraform init อัตโนมัติเวลา clone หรือ pull
Common Errors
Error: Failed to install provider
Error: Failed to install provider
Error while installing hashicorp/aws v5.70.0: ...
แก้: เช็ค internet, proxy, หรือ disk space
Error: Inconsistent dependency lock file
Error: Inconsistent dependency lock file
The following dependency selections recorded in the lock file are inconsistent...
แก้: terraform init -upgrade
Error: Module not installed
Error: Module not installed
This module is not yet installed. Run "terraform init" to install all modules required by this configuration.
แก้: รัน terraform init
สรุป
terraform init= คำสั่งแรกของทุก project — ทำ download providers + init backend.terraform/= cache (ห้าม commit).terraform.lock.hcl= lock provider version (ควร commit)- รันใหม่เมื่อเปลี่ยน provider/module/backend
- ใช้
-upgradeตอนอยาก update version
ต่อไป → Terraform Registry