IaC가 필요한 이유
클릭으로 만든 인프라는 재현이 불가능합니다. Terraform으로 인프라를 코드로 정의하면 버전 관리, 코드 리뷰, 자동화가 가능해집니다.
프로젝트 디렉터리 구조
terraform/
├── environments/
│ ├── prod/
│ │ ├── main.tf
│ │ └── terraform.tfvars
│ └── staging/
└── modules/
├── vpc/
├── eks/
└── rds/모듈 설계
# modules/vpc/main.tf
resource "aws_vpc" "main" {
cidr_block = var.cidr_block
enable_dns_hostnames = true
tags = merge(var.common_tags, {
Name = "${var.project}-${var.environment}-vpc"
})
}
variable "cidr_block" {
type = string
default = "10.0.0.0/16"
validation {
condition = can(cidrhost(var.cidr_block, 0))
error_message = "유효한 CIDR 형식이어야 합니다."
}
}상태(State) 원격 관리
terraform {
backend "s3" {
bucket = "my-company-terraform-state"
key = "prod/terraform.tfstate"
region = "ap-northeast-2"
encrypt = true
dynamodb_table = "terraform-state-lock"
}
}# State 잠금 테이블 생성
aws dynamodb create-table \
--table-name terraform-state-lock \
--attribute-definitions AttributeName=LockID,AttributeType=S \
--key-schema AttributeName=LockID,KeyType=HASH \
--billing-mode PAY_PER_REQUESTCI/CD 통합
name: Terraform
on:
pull_request:
paths: ['terraform/**']
jobs:
plan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
- run: terraform init
- run: terraform fmt -check -recursive
- run: terraform validate
- run: terraform plan -out=tfplan실전 팁
tfstate 파일 절대 git에 커밋 금지 (.gitignore)
*.tfstate
*.tfstate.backup
.terraform/sensitive 변수 마스킹
variable "db_password" {
type = string
sensitive = true
}Terraform은 단순한 도구가 아닙니다. 팀의 인프라 운영 방식 자체를 바꾸는 문화적 전환입니다.
이 글은 AI 에이전트가 1차 초안을 작성한 뒤, 사람 편집자가 사실관계·출처·톤과 맥락을 검토하여 발행했습니다. 오류나 부정확한 내용이 확인되면 24시간 이내에 정정합니다.
댓글
불러오는 중...