Outputs และ Locals
📖 text • 10 นาทีOutputs และ Locals
Output Values
Output ใช้แสดงข้อมูลหลัง terraform apply — เช่น IP address, URL, หรือ resource ID
ประกาศ Output
output "instance_ip" {
description = "Public IP ของ EC2 instance"
value = aws_instance.web.public_ip
}
output "web_url" {
description = "URL สำหรับเปิดเว็บ"
value = "http://${aws_instance.web.public_ip}"
}
หลัง apply:
Outputs:
instance_ip = "54.123.45.67"
web_url = "http://54.123.45.67"
Output Arguments
| Argument | หน้าที่ |
|---|---|
value |
ค่าที่จะแสดง (จำเป็น) |
description |
อธิบาย output |
sensitive |
ซ่อนค่าใน CLI output |
depends_on |
กำหนด dependency |
Sensitive Output
output "db_connection_string" {
value = "postgres://${aws_db_instance.main.endpoint}/${aws_db_instance.main.db_name}"
sensitive = true
}
Outputs:
db_connection_string = <sensitive>
ดูค่าจริง:
terraform output db_connection_string
ดู Output ทั้งหมด
# ดูทั้งหมด
terraform output
# ดูเฉพาะตัว
terraform output instance_ip
# ดูแบบ JSON (ใช้ใน script)
terraform output -json
Output ใช้ทำอะไรได้?
- แสดงข้อมูลให้ user — IP, URL, connection string
- ส่งต่อให้ module อื่น — module ส่ง output ให้ parent
- ใช้ใน remote state — โปรเจคอื่นอ่านผ่าน
terraform_remote_state - ใช้ใน CI/CD —
terraform output -jsonส่งค่าให้ pipeline
Local Values
Locals คือ "ตัวแปรภายใน" — คำนวณค่าแล้วใช้ซ้ำได้หลายที่
ประกาศ Locals
locals {
project = "myapp"
environment = var.environment
name_prefix = "${local.project}-${local.environment}"
common_tags = {
Project = local.project
Environment = local.environment
ManagedBy = "terraform"
}
}
ใช้งาน
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = var.instance_type
tags = merge(local.common_tags, {
Name = "${local.name_prefix}-web"
})
}
resource "aws_s3_bucket" "data" {
bucket = "${local.name_prefix}-data"
tags = local.common_tags
}
resource "aws_security_group" "web" {
name = "${local.name_prefix}-web-sg"
tags = local.common_tags
}
ทุก resource ได้ชื่อที่สอดคล้องกัน + tags เหมือนกัน โดยกำหนดที่เดียว
Computed Locals
locals {
# คำนวณจาก condition
instance_type = var.environment == "prod" ? "t3.large" : "t3.micro"
# สร้าง list จาก map
subnet_ids = [for s in aws_subnet.public : s.id]
# Flatten nested list
all_cidrs = flatten([var.private_cidrs, var.public_cidrs])
}
Variable vs Local vs Output
| Variable | Local | Output | |
|---|---|---|---|
| ทิศทาง | Input (เข้า) | Internal (ภายใน) | Output (ออก) |
| ใครกำหนดค่า | User / tfvars | Code (computed) | Terraform (จาก resource) |
| อ้างอิงด้วย | var.name |
local.name |
module.name.output |
| ใช้ทำอะไร | รับค่าจากภายนอก | คำนวณ/ลดซ้ำ | แสดง/ส่งต่อค่า |
Pattern: แยกไฟล์
โปรเจคที่เป็นระเบียบจะแยกไฟล์:
my-project/
├── main.tf # Resource definitions
├── variables.tf # Input variable declarations
├── outputs.tf # Output declarations
├── locals.tf # Local values
├── providers.tf # Provider configuration
└── terraform.tfvars # Variable values
variables.tf
variable "environment" {
description = "Deployment environment"
type = string
}
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t2.micro"
}
outputs.tf
output "web_url" {
description = "URL ของเว็บ"
value = "http://${aws_instance.web.public_ip}"
}
locals.tf
locals {
name_prefix = "myapp-${var.environment}"
common_tags = {
Environment = var.environment
ManagedBy = "terraform"
}
}
หมายเหตุ: Terraform อ่านทุกไฟล์
.tfใน directory เดียวกัน — การแยกไฟล์เป็นแค่เรื่อง organization ไม่มีผลกับการทำงาน
สรุป
| Concept | Keyword | หน้าที่ |
|---|---|---|
| Output | output |
แสดง/ส่งต่อค่าหลัง apply |
| Local | locals |
คำนวณค่าภายใน ลดการซ้ำ |
terraform output |
CLI | ดูค่า output |
| File organization | .tf files |
แยกไฟล์ตาม concern |
บทถัดไปเราจะเรียนรู้วิธีใช้ tfvars แยก environment