2019/05〜ごろに発表された Terraform CloudにRemote State Management(stateファイルのためのストレージ)という機能が出たので、今回 tfstate(backend)
をSaaS版に移行してみました!今まではAWSのS3を使って管理していましたが、 差分(下記画像より)や、apply時のロック(Enhancedタイプ)されるなど、神機能となりますね!
詳しくは以下を参考に!
https://blog.lorentzca.me/manage-state-files-on-terraform-cloud/
従来のtfstateファイルの管理方法は以下を参考に。
■準備
https://app.terraform.io/signup?utm_source=blog&utm_campaign=intro_tf_cloud_remote
- Sign Up
https://www.hashicorp.com/blog/introducing-terraform-cloud-remote-state-management
適当にアカウント作ったら Create a New Organization
をクリックして作成してください。
- tokenの作成
このtokenをメモったらホームディレクトリに以下のファイルを作成しましょう。
※今回はDigitalOceanです
1 2 3 4 |
$ cat ~/.terraformrc credentials "app.terraform.io" { token = "xxxxxxxxxxxxx" # メモったtokenをぶちこむ } |
- backend.tf
1 2 3 4 5 6 7 8 9 10 |
terraform { backend "remote" { hostname = "app.terraform.io" organization = "adachin-server-labo" # 作ったOrganization名 workspaces { name = "prd" # わかりやすいように } } } |
■Terraform
- terraform init
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
$ terraform init Initializing the backend... Backend configuration changed! Terraform has detected that the configuration specified for the backend has changed. Terraform will now check for existing state in the backends. Terraform detected that the backend type changed from "s3" to "remote". Acquiring state lock. This may take a few moments... 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 "remote" backend. No existing state was found in the newly configured "remote" backend. Do you want to copy this state to the new "remote" backend? Enter "yes" to copy and "no" to start with an empty state. Enter a value: yes Successfully configured the backend "remote"! Terraform will automatically use this backend unless the backend configuration changes. Initializing provider plugins... 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.digitalocean: version = "~> 1.5" 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 Cloudにシンクされているはずなので見てみましょう!
おおおお!!
下記のように terraform apply
すると差分も見れるので、Githubぽい!
■まとめ
DigitalOceanの場合はAPIを発行していましたが、不要になりました。今後はTerraform Cloudで管理するべきですね。修正箇所もチームで共有できるし。S3は使わないので消します!次のTerraform Cloudのアップデートも期待!
0件のコメント