IaC Quality Gate: Infracost, TFLint와 Terragrunt 운영
IaC 유틸리티
섹션 제목: “IaC 유틸리티”.gitignore
섹션 제목: “.gitignore”# Local .terraform directories**/.terraform/*# .tfstate files*.tfstate*.tfstate.*
# Crash log filescrash.logcrash.*.log
# Exclude all .tfvars files, which are likely to contain sensitive data, such as# password, private keys, and other secrets. These should not be part of version# control as they are data points which are potentially sensitive and subject# to change depending on the environment.*.tfvars*.tfvars.json
# Ignore override files as they are usually used to override resources locally and so# are not checked inoverride.tfoverride.tf.json*_override.tf*_override.tf.json
# Include override files you do wish to add to version control using negated pattern# !example_override.tf
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan# example: *tfplan*
# Ignore CLI configuration files.terraformrcterraform.rc
# .terraform.lock.hcl은 무시하지 않는다(commit 대상)
# Ignore any .pem files (SSH keys)*.pem
# Ignore any .log files*.log
# Ignore .DS_Store files on macOS.DS_Store
# Ignore any backup files*.bak
# Ignore any .env files that might contain sensitive information*.env위 목록에서 두 가지는 의도를 분명히 해 둔다.
.terraform.lock.hcl은 commit한다. provider 버전과 checksum을 고정하는 파일이라, 무시하면 runner마다 다른 provider 버전으로 plan이 나올 수 있다. HashiCorp도 이 파일을 version control에 포함하라고 안내한다. 여러 OS의 runner를 쓰면terraform providers lock -platform=linux_amd64 -platform=darwin_arm64로 각 platform의 checksum을 함께 기록한다.*.tfvars전체 무시는 secret이 섞이는 경우에만 맞다. SKU·region·CIDR처럼 secret이 아닌 환경별 값은 repository에서 리뷰해야 하므로 commit하고, secret은 Key Vault나 pipeline secret으로 주입한다. 이 경우secrets.auto.tfvars처럼 secret 파일만 이름으로 제외한다.
Cost Estimation: Infracost
섹션 제목: “Cost Estimation: Infracost”Cloud Infra 예상 비용을 조회할 수 있는 API를 제공하는 OSS 도구다: Infracost
비용 추정은 apply 직전이 아니라 PR 단계에 둘 때 효과가 있다. plan 결과로 infracost diff를 실행해 변경 전후 월 비용 차이를 PR comment로 남기면, 리뷰어가 SKU나 instance 수 변경의 비용 영향을 같은 화면에서 판단할 수 있다. 추정치는 사용량 기반 과금(트래픽, 트랜잭션)을 정확히 반영하지 못하므로 승인 기준이 아니라 검토 신호로 쓴다.
Terraform Lint: tflint + pre-commit
섹션 제목: “Terraform Lint: tflint + pre-commit”코드 Lint 적용을 위해 개발자 각자의 환경에 pre-commit hook으로 tflint를 구성한다. 이 예시는 lint 위반으로 commit을 차단하도록 --force를 제외한다. --force는 위반이 있어도 종료 코드 0을 반환하므로 보고 전용 hook에서만 사용한다.
Terraform 프로젝트 최상단에 .pre-commit-config.yaml을 작성한다.
repos: # ------------------------------ # Default # ------------------------------ - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.4.0 hooks: - name: ❯ (default) trailing-whitespace id: trailing-whitespace stages: [commit] - name: ❯ (default) EOF fixer id: end-of-file-fixer exclude: /secrets stages: [commit] - name: ❯ (default) Check Large files id: check-added-large-files stages: [commit] - name: ❯ (default) Check yaml id: check-yaml args: - '--allow-multiple-documents' exclude: /templates|/secrets stages: [commit] - name: ❯ (default) Check json id: check-json stages: [commit] - name: ❯ (default) Check executable scripts id: check-shebang-scripts-are-executable stages: [commit] # ------------------------------ # TF Coding convention # ------------------------------ - repo: https://github.com/antonbabenko/pre-commit-terraform rev: v1.88.3 hooks: - name: ❯ (tf-coding-convention) terraform_fmt id: terraform_fmt
- name: ❯ (tf-coding-convention) terraform_tflint id: terraform_tflint args: - --args=--call-module-type=all - --args=--enable-rule=terraform_documented_variables
- name: ❯ (coding-convention) terraform_validate id: terraform_validatepre-commit 도구 설치:
# Pre-commit 설치pip install pre-commit
# Config 파일 기반한 pre-commit hook 구성pre-commit install
# pre-commit hook 임의 발생pre-commit run -atflint 설치:
# AMD64 Linuxcurl -L "$(curl -s https://api.github.com/repos/terraform-linters/tflint/releases/latest | grep -o -E -m 1 "https://.+?_linux_amd64.zip")" > tflint.zip && unzip tflint.zip && rm tflint.zip && sudo mv tflint /usr/local/bin/
# ARM64 macOS (Apple Silicon)curl -L "$(curl -s https://api.github.com/repos/terraform-linters/tflint/releases/latest | grep -o -E -m 1 "https://.+?_darwin_arm64.zip")" > tflint.zip && unzip tflint.zip && rm tflint.zip && sudo mv tflint /usr/local/bin/Terragrunt
섹션 제목: “Terragrunt”Remote State file 관리를 좀 더 쉽게 해주는 등 Terraform 사용 편의성을 높여주는 Wrapper 프로젝트다(OpenTofu 지원). DRY(Don’t Repeat Yourself) 원칙이 핵심 사상이며, Terraform 코드/Backend 구성/CLI Arguments를 DRY 상태로 유지하고, 여러 모듈에 분산된 Terraform 코드를 한 번에 실행하며, Before/After Hook을 제공한다.
도입 여부는 반복의 양으로 판단한다. 환경이 2~3개이고 root module이 몇 개뿐이라면 Terraform만으로 디렉토리와 *.tfvars를 나누는 편이 단순하다. 환경·region·구독 조합이 늘어 backend 설정과 provider 블록이 수십 곳에 복사되기 시작하면 Terragrunt가 그 반복을 한 곳으로 모아 준다. 대신 팀 전체가 Terragrunt의 디렉토리 규칙과 run-all 실행 순서를 이해해야 하고, CommentOps 같은 pipeline도 Terragrunt 명령을 기준으로 다시 맞춰야 한다.


마치며
섹션 제목: “마치며”이 글에서는 IaC/Terraform 기본기부터 TACOS 도구 비교, CommentOps Pipeline, 실제 Network/Managed Resource 배포 코드, IaC 유틸리티까지 Infra Provisioning의 실무적인 흐름을 정리했다. 이렇게 구성한 인프라 위에서 애플리케이션을 빌드하고 배포하는 DevOps Tool Chain(CI/CD, Registry, GitOps 연계)은 Git과 Release Governance부터 이어진다.