terraform output

도은호·2025년 9월 17일

terraform

목록 보기
8/32

output모듈/루트에서 바깥으로 보여줄 결과.
예: 인스턴스 IP, ALB DNS, 서브넷 ID 같은 “다음 단계에서 필요할 값”.

  • 정의: output "<이름>" { value = <표현식> }
  • 보기: terraform output, terraform output -raw name, -json
  • 모듈 연결: 부모에서 module.<모듈명>.<output명>
  • 비밀: sensitive = true로 콘솔 표시만 숨김(상태파일엔 저장됨)

1) 기본 예제

# outputs.tf
output "public_ip" {
  description = "웹서버 퍼블릭 IP"
  value       = aws_instance.web.public_ip
}

터미널:

terraform apply
terraform output            # 모든 출력
terraform output public_ip  # 특정 출력

2) 모듈에서 값 꺼내 쓰기

# modules/vpc/outputs.tf (자식)
output "public_subnet_ids" {
  value = aws_subnet.public[*].id
}

# root/main.tf (부모)
module "vpc" {
  source = "./modules/vpc"
}

resource "aws_instance" "web" {
  subnet_id = module.vpc.public_subnet_ids[0]
}

3) 여러 값을 한 번에(객체로 묶기 추천)

output "alb" {
  description = "ALB 식별자 묶음"
  value = {
    dns = aws_lb.app.dns_name
    arn = aws_lb.app.arn
    sg  = aws_security_group.alb.id
  }
}

4) 민감한 값 숨기기(콘솔만 가림)

variable "db_password" { type = string; sensitive = true }

output "db_password" {
  value     = var.db_password
  sensitive = true   # 콘솔에서만 숨김, state에는 저장됨
}
# 자동화에선:
# terraform output -raw db_password   # 기계가 읽도록

5) 자주 하는 실수

  • 필요한 값을 output 안 함 → ✅ “다음 단계에서 쓸 ID/DNS는 꼭 output”
  • 비밀을 평문으로 출력 → ✅ sensitive = true + 상태 백엔드(암호화/IAM) 보안
  • 이름/형식 자주 변경 → ✅ 모듈의 “공개 API”라고 생각하고 안정적으로 유지

6) 템플릿

# outputs.tf (루트 또는 모듈)
output "ids" {
  description = "필요 식별자 모음"
  value = {
    vpc_id       = aws_vpc.main.id
    public_subnet= aws_subnet.public.id
    web_ip       = aws_instance.web.public_ip
  }
}

output "secrets" {
  value     = { db_password = var.db_password }
  sensitive = true
}

한 줄 결론: output = 다음 사람이/다음 스택이 볼 값.
꼭 필요한 것만, 깔끔한 이름으로, 비밀은 가리고!

profile
`•.¸¸.•´´¯`••._.• 🎀 𝒸𝓇𝒶𝓏𝓎 𝓅𝓈𝓎𝒸𝒽💞𝓅𝒶𝓉𝒽 🎀 •._.••`¯´´•.¸¸.•`

0개의 댓글