Lab: สร้าง Resource แรกด้วย Terraform
📖 text • 20 นาทีLab: สร้าง Resource แรกด้วย Terraform
สิ่งที่ต้องมี: Terraform ติดตั้งแล้ว + AWS Credentials ตั้งค่าแล้ว
เป้าหมาย
สร้าง S3 Bucket บน AWS ด้วย Terraform แล้วลบทิ้ง — เพื่อเข้าใจ workflow ของ init → plan → apply → destroy
Step 1: สร้างโปรเจค
mkdir terraform-first-lab && cd terraform-first-lab
Step 2: เขียน Configuration
สร้างไฟล์ main.tf:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
required_version = ">= 1.0"
}
provider "aws" {
region = "ap-southeast-1"
}
resource "aws_s3_bucket" "my_first_bucket" {
bucket = "addskills-tf-lab-${formatdate("YYYYMMDDhhmmss", timestamp())}"
tags = {
Name = "My First Terraform Bucket"
Environment = "lab"
ManagedBy = "terraform"
}
}
หมายเหตุ: ชื่อ S3 bucket ต้องไม่ซ้ำกันทั่วโลก เราใช้ timestamp เพื่อให้ชื่อไม่ซ้ำ
อธิบาย Code
| Block | หน้าที่ |
|---|---|
terraform {} |
ตั้งค่า Terraform เอง — กำหนด provider version |
provider "aws" |
บอกว่าจะใช้ AWS ที่ region ไหน |
resource "aws_s3_bucket" |
สร้าง S3 Bucket |
tags {} |
ติด tag เพื่อจัดกลุ่ม resource |
Step 3: Initialize
terraform init
ผลลัพธ์:
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 5.0"...
- Installing hashicorp/aws v5.xx.x...
- Installed hashicorp/aws v5.xx.x (signed by HashiCorp)
Terraform has been successfully initialized!
terraform init ทำอะไร?
- ดาวน์โหลด provider plugin (AWS) ไปไว้ใน
.terraform/ - สร้าง
.terraform.lock.hclเพื่อ lock version
Step 4: Plan
terraform plan
ผลลัพธ์:
Terraform will perform the following actions:
# aws_s3_bucket.my_first_bucket will be created
+ resource "aws_s3_bucket" "my_first_bucket" {
+ arn = (known after apply)
+ bucket = (known after apply)
+ id = (known after apply)
+ tags = {
+ "Environment" = "lab"
+ "ManagedBy" = "terraform"
+ "Name" = "My First Terraform Bucket"
}
...
}
Plan: 1 to add, 0 to change, 0 to destroy.
+= จะสร้างใหม่(known after apply)= ค่าที่จะรู้หลัง apply (เช่น ARN ที่ AWS generate ให้)
Step 5: Apply
terraform apply
Terraform จะแสดง plan อีกครั้ง แล้วถามว่า:
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
พิมพ์ yes แล้ว Enter
aws_s3_bucket.my_first_bucket: Creating...
aws_s3_bucket.my_first_bucket: Creation complete after 2s
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Step 6: ตรวจสอบ
ตรวจผ่าน AWS CLI
aws s3 ls | grep addskills-tf-lab
ดู State
terraform show
จะเห็น resource ที่ Terraform สร้าง พร้อมรายละเอียดทั้งหมด
ดู State แบบ List
terraform state list
aws_s3_bucket.my_first_bucket
Step 7: ลอง Modify
เพิ่ม tag ใน main.tf:
tags = {
Name = "My First Terraform Bucket"
Environment = "lab"
ManagedBy = "terraform"
Course = "terraform-fundamentals" # เพิ่มบรรทัดนี้
}
รัน plan ดู:
terraform plan
# aws_s3_bucket.my_first_bucket will be updated in-place
~ resource "aws_s3_bucket" "my_first_bucket" {
~ tags = {
"Environment" = "lab"
"ManagedBy" = "terraform"
"Name" = "My First Terraform Bucket"
+ "Course" = "terraform-fundamentals"
}
}
Plan: 0 to add, 1 to change, 0 to destroy.
~= จะแก้ไข (update in-place)+ข้างหน้า tag = จะเพิ่ม tag ใหม่
Apply:
terraform apply -auto-approve
-auto-approve= ข้ามการถาม yes/no (ใช้ใน lab เท่านั้น อย่าใช้ใน production!)
Step 8: Destroy
เสร็จ lab แล้ว ลบ resource ทิ้ง:
terraform destroy
# aws_s3_bucket.my_first_bucket will be destroyed
- resource "aws_s3_bucket" "my_first_bucket" {
- arn = "arn:aws:s3:::addskills-tf-lab-..."
- bucket = "addskills-tf-lab-..."
...
}
Plan: 0 to add, 0 to change, 1 to destroy.
Do you really want to destroy all resources?
Enter a value: yes
Destroy complete! Resources: 1 destroyed.
Cleanup
cd .. && rm -rf terraform-first-lab
สิ่งที่ได้เรียนรู้
| Command | หน้าที่ |
|---|---|
terraform init |
ดาวน์โหลด provider, เตรียมโปรเจค |
terraform plan |
Preview การเปลี่ยนแปลง |
terraform apply |
สร้าง/แก้ไข resource จริง |
terraform show |
ดู state ปัจจุบัน |
terraform state list |
ดู resource ทั้งหมดใน state |
terraform destroy |
ลบ resource ทั้งหมด |
สรุป
คุณเพิ่งผ่าน full lifecycle ของ Terraform: init → plan → apply → modify → destroy
นี่คือ workflow ที่ใช้ทุกวันในการทำงานจริง บทถัดไปเราจะเจาะลึกเรื่อง HCL syntax เพื่อเขียน configuration ที่ซับซ้อนขึ้น