LoginSignup
22
13

More than 5 years have passed since last update.

Terraformでremote stateを別のremote stateに移動する方法

Posted at

Terraformでremote stateを別のremote stateに移動する方法

課題

  • Terraformであるremote stateにあるresourceやmoduleを別のremote stateに移動する時ってあると思います。tarraform state rm した後に、terraform import をしても良いけれども、terraform import はすべてのresourceに対応しているわけじゃないのでそんな時に使えたりします。

手順

1. 対象のstateをlocalに移動する

terraform state mv --state-out=local.tfstate \ 
                   module.sample module.sample
  • --state-out オプションで指定したpathにstateを出力する。

2. 移動先のstateのdirに移動して、remote stateを取得する。

terraform state pull > remote.tfstate
  • terraform state pull でremote stateをcopyすることが出来るのでファイルに出力する

3. localに出力したstateに移動させたいmoduleのstateを出力する

 terraform state mv -state=local.tfstate \
                   --state-out=remote.tfstate \
                   module.sample module.sample 
  • -state オプションは指定したstateを元に読み込む、--state-out オプションはmvしたstateを出力して、既存のstate fileを指定すると差分をmergeして出力してくれる。

4. 修正したstateをremote stateにpushする

terraform state push remote.tfstate
  • terraform state push FILENAME は指定したファイルをremote stateに反映させることが出来る。

おわりに

  • あれー?出来たような気がするんだけどなーと思いながらも、意外とやり方書いてあるのが見当たらなかった。
22
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
13