"terraform state mv" の備忘録
はじめに
今回はTerraformを利用していると必要となってくる stateファイルを変更する場合の備忘録になります。
環境
- Terraformのバージョン
- Terraform: v1.0.1
- リモートのTerraform環境
- terraform-cloud
state mv を実行する
今回は以下の箇所を変更します。
resource "aws_ecs_task_definition" "service" を "services
" に変更する
resource "aws_ecs_task_definition" "service" ↓↓↓ resource "aws_ecs_task_definition" "services"
変更する前に設定を確認する。
$ terraform state list
$ terraform state list module.ecs_fargate.aws_ecs_task_definition.service module.ecs_fargate.aws_ecs_task_definition.service["systemA"] module.ecs_fargate.aws_ecs_task_definition.service["systemB"]
作業手順は以下の通り。
- "remote" のtfstateファイルを取得する
- "backend" を "remote" から "local" に変更する
- "backend"を変更したので
-reconfigure
する - "local" のtfstateファイルを
state mv
する - "local" のtfstateファイルを指定して
terraforn plan
して変更が無い事を確認する - "backend" を "local" から "remote" に変更する
- "backend" を変更したので
-reconfigure
する - "remote" のtfstateファイルに反映する
- 不要になった "local" のstateファイルを削除する
- 変更したソースコードをpushする
■ 1. "remote" のtfstateファイルを取得する
$ terraform state pull > temp.tfstate
■ 2. "backend" を "remote" から "local" に変更する
- providers.tf (変更前)
terraform { required_version = ">=1.0" required_providers { aws = { source = "hashicorp/aws" version = "~> 3.0" } } backend "remote" { hostname = "app.terraform.io" organization = "myorg" workspaces { name = "aws-dev" } } }
- providers.tf (変更後)
terraform { required_version = ">=1.0" required_providers { aws = { source = "hashicorp/aws" version = "~> 3.0" } } backend "local" { } }
■ 3. "backend"を変更したので -reconfigure
する
-reconfigure
のオプション
-reconfigure: Reconfigure the backend, ignoring any saved configuration.
$ terraform init -reconfigure Initializing modules... Initializing the backend... Successfully configured the backend "local"! Terraform will automatically use this backend unless the backend configuration changes.
→ local に切り替わった事を確認
■ 4. "local" のtfstateファイルを state mv
する
-state
で stateファイルを指定する$ terraform state mv -state=${STATE_FILE} <変更前> <変更後>
$ terraform state mv -state=tmp.tfstate module.ecs_fargate.aws_ecs_task_definition.service module.ecs_fargate.aws_ecs_task_definition.services Move "module.ecs_fargate.aws_ecs_task_definition.service" to "module.ecs_fargate.aws_ecs_task_definition.services" Successfully moved 1 object(s).
■ 5."local" のtfstateファイルを指定して terraforn plan
して変更が無い事を確認する
$ aws-vault exec my-dev -- terraform plan -state=temp.tfstate > No changes. Your infrastructure matches the configuration.
*) aws-vaultを利用しているためこのコマンドになります。
■ 6. "backend" を "local" から "remote" に変更する
最初に変更した providers.tfの設定を元に戻す
- providers.tf (変更前)
terraform { required_version = ">=1.0" required_providers { aws = { source = "hashicorp/aws" version = "~> 3.0" } } backend "local" { } }
- providers.tf (変更後)
terraform { required_version = ">=1.0" required_providers { aws = { source = "hashicorp/aws" version = "~> 3.0" } } backend "remote" { hostname = "app.terraform.io" organization = "myorg" workspaces { name = "aws-dev" } } }
■ 7. "backend" を変更したので -reconfigure
する
❯ terraform init -reconfigure Initializing modules... Initializing the backend... Successfully configured the backend "remote"! Terraform will automatically use this backend unless the backend configuration changes.
→ remote に切り替わった事を確認
■ 8. "remote" のtfstateファイルに反映する
$ terraform state push ${STATE_FILE}
$ terraform state push temp.tfstate Acquiring state lock. This may take a few moments... Releasing state lock. This may take a few moments...
■ "remote" のstate情報を確認
$ terraform state list
$ terraform state list module.ecs_fargate.aws_ecs_task_definition.services module.ecs_fargate.aws_ecs_task_definition.services["systemA"] module.ecs_fargate.aws_ecs_task_definition.services["systemB"]
remote stateファイルが aws_ecs_task_definition.service
が .aws_ecs_task_definition.services
に変更できた事が確認できました。
■ 9. 不要になった "local" のstateファイルを削除する
rm -f temp.tfstate*
■ 10. 変更したソースコードをpushする 変更したコードをpushして差分を取り込む