Skip to content

IaC Quality Gates: Operating Infracost, TFLint, and Terragrunt

Interactive Diagram

새 탭 ↗
# Local .terraform directories
**/.terraform/*
# .tfstate files
*.tfstate
*.tfstate.*
# Crash log files
crash.log
crash.*.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 in
override.tf
override.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
.terraformrc
terraform.rc
# Do not ignore .terraform.lock.hcl (commit it)
# 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

Two entries in this list need a clear intent.

  • Commit .terraform.lock.hcl. It pins provider versions and checksums, so ignoring it lets each runner plan with a different provider version. HashiCorp also recommends keeping this file in version control. If runners use several operating systems, record checksums for each platform with terraform providers lock -platform=linux_amd64 -platform=darwin_arm64.
  • Ignoring every *.tfvars only fits when secrets are mixed in. Non-secret per-environment values such as SKU, region, and CIDR should be committed so they can be reviewed, and secrets should be injected from Key Vault or pipeline secrets. In that case, exclude only the secret file by name, such as secrets.auto.tfvars.

An open-source tool that provides an API for looking up estimated cloud infra costs: Infracost

Cost estimation pays off at the PR stage, not right before apply. Running infracost diff on the plan output and posting the monthly cost difference as a PR comment lets reviewers judge the cost impact of SKU or instance-count changes on the same screen. Estimates can’t accurately reflect usage-based charges (traffic, transactions), so treat them as a review signal rather than an approval criterion.

To enforce code linting, we configure a pre-commit hook running tflint in each developer’s local environment. This example omits --force to block commits on lint findings. --force returns exit status 0 even when issues are found, so use it only in a report-only hook.

Add a .pre-commit-config.yaml at the root of the Terraform project.

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_validate

Installing the pre-commit tool:

Terminal window
# install pre-commit
pip install pre-commit
# set up the pre-commit hook from the config file
pre-commit install
# manually trigger the pre-commit hooks
pre-commit run -a

Installing tflint:

Terminal window
# AMD64 Linux
curl -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 is a wrapper project that makes Terraform more convenient to use, including easier remote-state-file management (and supports OpenTofu). Its core philosophy is DRY (Don’t Repeat Yourself): it keeps your Terraform code, backend configuration, and CLI arguments DRY, lets you run Terraform code spread across multiple modules all at once, and provides before/after hooks.

Decide whether to adopt it by how much repetition you have. With two or three environments and only a few root modules, plain Terraform with separate directories and *.tfvars is simpler. Once combinations of environments, regions, and subscriptions grow and backend settings and provider blocks are copied into dozens of places, Terragrunt consolidates that repetition in one place. In exchange, the whole team must understand Terragrunt’s directory conventions and run-all execution order, and pipelines such as CommentOps need to be realigned around Terragrunt commands.

Terragrunt remote backend structure

Terragrunt-based resource management directory structure

This article covered the practical flow of Infra Provisioning, from IaC/Terraform fundamentals, through comparing TACOS tools and building a CommentOps pipeline, to the actual Network/Managed Resource deployment code and IaC utilities. The DevOps Tool Chain (CI/CD, Registry, GitOps integration) for building and deploying applications on top of this infrastructure continues from Git and Release Governance.

  1. Terraform-based Infrastructure Provisioning: Principles, State, and Repository Structure
  2. A CommentOps-based Terraform Pipeline: From Plan to Apply
  3. Azure Resource Deployment Patterns with Terraform
  4. IaC Quality Gates: Operating Infracost, TFLint, and Terragrunt