github/gitignore · Terraform.gitignore
Terraform .gitignore テンプレート解説
Terraform.gitignore が .terraform フォルダー、tfstate、tfvars、override ファイル、CLI 設定を無視する理由と、.terraform.lock.hcl をコミットすべき理由を説明します。
Terraform.gitignore は、インフラのコードを扱うリポジトリで状態と秘密が漏れないようにすることに重点を置いている。Terraform の状態ファイルにはリソースの属性が平文で保存され、データベースのパスワードやキーが含まれることがある。
.terraform.lock.hcl はこのテンプレートでは無視されない。HashiCorp はこの依存関係ロックファイルをバージョン管理に含めるよう案内している。
ルール別の解説
| パターン | 何を、なぜ無視するか |
|---|---|
.terraform/ | ローカル作業ディレクトリterraform init がダウンロードしたプロバイダーのバイナリとモジュール、バックエンドの設定が入る。OS ごとのバイナリなので共有できず、容量も大きい。 |
*.tfstate*.tfstate.*.terraform.tfstate.lock.info | 状態ファイル実際のインフラとコードの対応関係を記録したファイルである。機密情報が平文で入り、複数人がそれぞれコピーを持つと状態がずれる。S3・Terraform Cloud のようなリモートバックエンドに置いてロックをかける。 |
*.tfvars*.tfvars.json | 変数値ファイル環境ごとの値と秘密が入るファイルである。テンプレートのコメントも、パスワードや秘密鍵が入る可能性が高いと説明している。 |
override.tfoverride.tf.json*_override.tf*_override.tf.json | override ファイルローカルでリソース設定を一時的に上書きするファイルである。コミットするなら、テンプレートのコメントのように !example_override.tf の形で戻す。 |
crash.logcrash.*.log | クラッシュログTerraform が異常終了したときに残すログで、設定内容が含まれることがある。 |
.terraformrcterraform.rc | CLI 設定CLI の設定ファイルで、Terraform Cloud の認証トークンが入ることがある。 |
実務での注意点
.terraform.lock.hclはコミットする。全員が同じプロバイダーのバージョンとハッシュで初期化できるようになる。- 共有が必要な既定の変数は、
*.auto.tfvarsではなくvariables.tfのdefaultやサンプルファイル(terraform.tfvars.example)に置く。*.tfvarsルールは.exampleで終わるファイルを無視しない。 terraform plan -out=tfplanで作った計画ファイルも機密情報を含むため、テンプレートのコメントのように*tfplan*ルールを追加するのが安全である。- 状態ファイルがすでにコミットされている場合は、リポジトリから削除するとともに、その中にあった秘密をすべて差し替える。
元のテンプレート
Terraform.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# Ignore transient lock info files created by terraform apply.terraform.tfstate.lock.info# 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# Optional: ignore graph output files generated by `terraform graph`# *.dot# Optional: ignore plan files saved before destroying Terraform configuration# Uncomment the line below if you want to ignore planout files.# planout
テンプレートの出典: github/gitignore/Terraform.gitignore @356fd7b (2026-09-11) · CC0-1.0