日頃の行い

個人的な日頃の行いをつらつら書いてます\\\\ ٩( 'ω' )و ////

terraform v0.8.8からv0.10.3に移行した時remote state周りでやったことメモ

気がついたらterraformのバージョンが上がってました。
使ってた時は0.8.xだった気がしたけど、
この前見たら0.10.xになってて、なるほど〜となりました。

細かいCHANGE LOGは terraform/CHANGELOG.md at master · hashicorp/terraform · GitHub をみるとして、
0.9.x から terraform remote hogehoge がなくなっていたので
0.10.x のversionでresourceのremote管理する方法を確認してみました。

確認は v0.10.3terraform plan したら出てきたURLを参考にしました。

$terraform --version
Terraform v0.10.3

$terraform plan
Deprecation warning: This environment is configured to use legacy remote state.
Remote state changed significantly in Terraform 0.9. Please update your remote
state configuration to use the new 'backend' settings. For now, Terraform
will continue to use your existing settings. Legacy remote state support
will be removed in Terraform 0.11.

You can find a guide for upgrading here:

https://www.terraform.io/docs/backends/legacy-0-8.html

www.terraform.io

↑のページの流れに沿って、移行の手順を書いてみました。

1. terraform v0.8.x で remote state を持ってくる

With the older Terraform version (version 0.8.x), run terraform remote pull. This will cache the latest legacy remote state data locally. We’ll use this for a backup in case things go wrong.

古いversionのterraformでローカルに最新の状態を持ってきて、
古いバージョンはここからダウンロードできました。

Terraform Versions | HashiCorp Releases

2. .terraform/terraform.tfstate のバックアップ

Backup your .terraform/terraform.tfstate file. This contains the cache we just pulled. Please copy this file to a location outside of your Terraform module.

何かあった時用にバックアップをとっておきましょう。
私はとりあえず /tmp に置きました。

3. backendの設定

Configure your backend in your Terraform configuration. The backend type is the same backend type as you used with your legacy remote state. The configuration should be setup to match the same configuration you used with remote state.

www.terraform.io

backendの設定を↑ページを参考に追加しましょう。
backendの設定がファイルになったんですね。
設定ファイルはこんな感じになりました。

backend.tf

terraform {
    backend "s3" {
        bucket = "terraform-sample-tfstate"
        key = "sample.tfstate"
        region = "ap-northeast-1"
    }
}

もしくはこのように最低限の設定ファイルを書いて、
initコマンド時にoptionで指定することもできます。

terraform {
    backend "s3" {}
}
$terraform init \
    -backend-config="bucket=terraform-sample-tfstate" \
    -backend-config="key=sample.tfstate" \
    -backend-config="region=ap-northeast-1" 
# backend configオプションにはファイルを指定するのかと思ったら違いました(ヘルプ読まないせい

(backend configの使い方はここで気が付きました。
Terraform backend init: settings in -backend-config file are ignored · Issue #13552 · hashicorp/terraform · GitHub

4. initコマンドの実行

Run the init command. This is an interactive process that will guide you through migrating your existing remote state to the new backend system. During this step, Terraform may ask if you want to copy your old remote state into the newly configured backend. If you configured the identical backend location, you may say no since it should already be there.

あとはinitコマンドを打つだけです。

$terraform init \
        -backend=true \
        -force-copy \
        -get=true \
        -input=false

Initializing the backend...
New backend configuration detected with legacy remote state!

Terraform has detected that you're attempting to configure a new backend.
At the same time, legacy remote state configuration was found. Terraform will
first configure the new backend, and then ask if you'd like to migrate
your remote state to the new backend.



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

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "aws" (0.1.4)...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.aws: version = "~> 0.1"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

# terraform {
#    backend "s3" {}
# }
# backend.tfが↑のときは↓のようなコマンドになります

$terraform init \
        -backend-config="bucket=terraform-sample-tfstate" \
        -backend-config="key=sample.tfstate" \
        -backend-config="region=ap-northeast-1" \
        -backend=true \
        -force-copy \
        -get=true \
        -input=false

5. terraform planを実行してstateが正しいか確認

Verify your state looks good by running terraform plan and seeing if it detects your infrastructure. Advanced users may run terraform state pull which will output the raw contents of your state file to your console. You can compare this with the file you saved. There may be slight differences in the serial number and version data, but the raw data should be almost identical.

terraform planを実行してみて、問題なさそうか確認します。

$terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

aws_s3_bucket.terraform-sample-tfstate: Refreshing state... (ID: terraform-sample-tfstate)
No changes. Infrastructure is up-to-date.

This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, Terraform
doesn't need to do anything.

up-to-dateみたいなので問題なさそうです。
init時にbackendの設定がうまくいっていない場合、
はじめにplanしたときみたいに Deprecation warning みたいなのが出ます。
その時はbackendの設定がうまく言ってないはずなので、
initを頑張るといい感じになるかなと思います・・・

最終的にできたレポジトリはこんな感じでした。
S3は確か名前の重複ができない気がしたのでそのままでは試せないかもしれないですが、
参考になれば幸いです。
terraformガンガン進んでいって追いつくの大変だけど、とても便利なので使っていきたいと思います。

github.com