Closed10

tfstateをローカルからs3に持っていく方法を模索する

not75743not75743

初期設定

main.tfを用意して

main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.65.0"
    }
  }
}

provider "aws" {
  region = "ap-northeast-1"
  default_tags {
    tags = {
      env       = "test"
      provision = "terraform"
    }
  }
}

resource "aws_s3_bucket" "s3" {
}

init,plan,applyします

$ terraform init
$ terraform plan
$ terraform apply

terraform.tfstateがあればOK

※ここで作成しているs3はただの題材であり、tfstateを格納しません。

not75743not75743

バージョン

terraformとawsプロバイダーのバージョンをあげよう!

$ terraform --version
Terraform v1.4.6
on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v4.65.0

Your version of Terraform is out of date! The latest version
is 1.5.0. You can update by downloading from https://www.terraform.io/downloads.html
not75743not75743

tfstateを格納するs3を作る

作ります

backendをs3にする

ローカルからs3にします

main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.65.0"
    }
  }
+  backend "s3" {
+    bucket = "<作成したバケット>"
+    key    = "<パス>"
+    region = "ap-northeast-1"
+  }
}

ここまでが下準備で、コマンド群を試していきます

not75743not75743

-migrate-state

既存の状態を保ちながら、バックエンドを更新する。
対話型プロンプトが表示され、yesで移行された

$ 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


Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.

// 略

not75743not75743

ローカルにも戻せる-migrate-state

s3のバックエンド設定を消せばローカルに戻せます

main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.65.0"
    }
  }
-  backend "s3" {
-    bucket = "<作成したバケット>"
-    key    = "<パス>"
-    region = "ap-northeast-1"
-  }
}
$ terraform init -migrate-state

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

  Enter a value: yes



Successfully unset the backend "s3". Terraform will now operate locally.

// 略

ここでs3のtfstateは削除します。reconfigureのため

not75743not75743

-reconfigure

既存の状態を保持せずバックエンドを更新する
?なんのこっちゃ

$ terraform init -reconfigure

Initializing the backend...

Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.

// 略

$ terraform plan

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
  + create

Terraform will perform the following actions:

  # aws_s3_bucket.s3 will be created
// 略

あーなるほど、いままでのローカルのtfstateを無視してリソースの再生成が走りそう
これはあかんな

このスクラップは2023/06/17にクローズされました