「Backend configuration changed」エラーの解決方法

1. 概要

  • 「Backend configuration changed」エラーとは
  • terraform init -reconfigure-migrate-state の違い
  • stateを安全に移行する手順
  • よくある発生シナリオ

バックエンド(stateの保存場所)の設定を変更した後にterraform initを実行すると、Terraformは既存のstateファイルをどう扱うかを確認します。適切な対応をしないとstateが失われる危険があります。


2. エラー・確認メッセージ

│ Error: Backend configuration changed
│
│ A change in the backend configuration has been detected, which may require
│ migrating existing state.
│
│ If you wish to attempt automatic migration of the state, use "terraform init -migrate-state".
│ If you wish to store the current configuration with no changes to the state, use "terraform init -reconfigure".

3. よくある発生シナリオ

シナリオ1: ローカルからS3バックエンドへ移行

# 変更前: ローカルstateを使用(デフォルト)
terraform {
  required_version = ">= 1.9"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
  # backend ブロックなし → ローカルに terraform.tfstate が生成される
}
# 変更後: S3バックエンドに移行
terraform {
  required_version = ">= 1.9"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
  backend "s3" {
    bucket         = "my-company-tfstate"
    key            = "dev/terraform.tfstate"
    region         = "ap-northeast-1"
    dynamodb_table = "terraform-lock"
    encrypt        = true
  }
}

provider "aws" {
  region = "ap-northeast-1"
}

variable "environment" {
  description = "環境名"
  type        = string
  default     = "dev"
}

4. 解決方法: -migrate-stateでstateを移行(推奨)

# 既存のstateをS3に移行する
terraform init -migrate-state

実行すると対話的に確認が求められます:

Initializing the backend...
Do you want to copy existing state to the new backend?
  Pre-existing state was found while migrating the previous "local" backend to the
  newly configured "s3" backend. No existing state was found in the newly
  configured "s3" backend. Do you want to copy this state to the new "s3"
  backend? Enter "yes" to copy and "no" to start with an empty state.

  Enter a value: yes

yesと入力するとstateがS3にコピーされます。


5. -reconfigureはstateを移行しない

# ❌ -reconfigure: stateを移行せず、新しいバックエンドで空のstateから始まる
terraform init -reconfigure

# 既存のリソースがstateから消え、次のplanで全リソースが「新規作成」になる
# → 実際には既存リソースがあるのにplanが「作成」を提案する危険な状態

-reconfigureは「既存のstateを捨てて新しいバックエンドで始める」ときだけ使います。


6. S3からS3へのstateキー変更

# バックエンドのkeyを変更した場合も同じ手順
terraform init -migrate-state

この場合、古いkeyのstateが新しいkeyにコピーされます。


7. 安全なバックエンド移行の手順

# 1. 移行前にstateのバックアップを取る
terraform state pull > backup_$(date +%Y%m%d).tfstate

# 2. backend設定を変更する
# (backend "s3" { ... } を追記)

# 3. migrate-stateで移行
terraform init -migrate-state

# 4. 移行後にplanで差分がないことを確認
terraform plan
# → No changes. と表示されれば成功

8. 関連記事


9. まとめ

  • バックエンド変更後のterraform initでstateの移行確認が求められる
  • -migrate-state — 既存stateを新しいバックエンドにコピーする(ほぼ常にこれを使う
  • -reconfigure — stateを移行せず新しいバックエンドで空から始める(stateが消える危険)
  • 移行前にterraform state pull > backup.tfstateでバックアップを取ること
  • 移行後にterraform planでNo changesになるか必ず確認

動作確認バージョン: Terraform >= 1.9 公式ドキュメント: https://developer.hashicorp.com/terraform/language/settings/backends/configuration#changing-configuration