Day 2 - 리소스·변수·State
variables · outputs · state · lifecycle
목차
학습 목표
variable/output작성- state의 역할과 원격 state 필요성
tfvars로 환경 분리
변수와 출력
variables.tf:
variable "project_name" {
type = string
description = "Project name prefix"
default = "demo"
}
main.tf:
resource "local_file" "app" {
content = "app=${var.project_name}"
filename = "${path.module}/${var.project_name}.txt"
}
outputs.tf:
output "file_path" {
value = local_file.app.filename
}
terraform apply -var="project_name=pista"
# 또는
echo 'project_name = "pista"' > terraform.tfvars
terraform apply
terraform output
.gitignore 예:
.terraform/
*.tfstate
*.tfstate.*
.terraform.lock.hcl
*.tfvars
!example.tfvars
State
- 실제 인프라와 코드 매핑을 저장
- 로컬
terraform.tfstate→ 팀 작업 시 원격 backend (S3 + DynamoDB, GCS 등) terraform state list/terraform state show <addr>
실습 팁
terraform plan -out=tfplan
terraform apply tfplan
민감 출력:
output "secret" {
value = "..."
sensitive = true
}