LoginSignup
2

More than 5 years have passed since last update.

Backend変更(Initialization and setup)

Last updated at Posted at 2017-06-12

目的

  • backendをlocalからs3に変更 Untitled.png

設定

  • main.tfにBackendConfigurationを記述する
main.tf
// config
provider "aws" {
  region  = "ap-northeast-1"
  profile = "terraform-user"
}
terraform {
  backend "s3"  {
    bucket  = "<bucket-name>"
    key     = "<bucket-name>/<prefix>/terraform.tfstate"
    region  = "ap-northeast-1"
    profile = "terraform-user"
  }
}
  • 指定したバックエンドを使用するように設定
terraform_remote_state.tf
# !!!!!!上書き注意!!!!!!
data "terraform_remote_state" "<example>" {
  backend = "s3"
  config  {
    bucket  = "<bucket-name>"
    key     = "<bucket-name>/<prefix>/terraform.tfstate"
    region  = "ap-northeast-1"
    profile = "terraform-user"
  }
}

初期化&セットアップ

  • terraform initでバックエンド設定の変更を自動検出し、既存の状態を移行されます。

  • 「初期化せよ」とアナウンスがあります

$ terraform plan
Backend reinitialization required. Please run "terraform init".
Reason: Initial configuration of the requested backend "s3"

The "backend" is the interface that Terraform uses to store state,
perform operations, etc. If this message is showing up, it means that the
Terraform configuration you're using is using a custom configuration for
the Terraform backend.

Changes to backend configurations require reinitialization. This allows
Terraform to setup the new configuration, copy existing state, etc. This is
only done during "terraform init". Please run that command now then try again.

If the change reason above is incorrect, please verify your configuration
hasn't changed and try again. At this point, no changes to your existing
configuration or state have been made.

Failed to load backend: Initialization required. Please see the error message above.
  • 初期化
$ terraform init
Initializing the backend...
Do you want to copy state from "local" to "s3"?
  Pre-existing state was found in "local" while migrating to "s3". No existing
  state was found in "s3". Do you want to copy the state from "local" to
  "s3"? 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.

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 environment. If you forget, other
commands will detect it and remind you to do so if necessary.

構成

  • カレントに.terraformが掘られて、terraform.tfstateが置かれます
    • カレントにあったterraform.tfstateは内容が削除されます
.
├── .terraform
│   └── terraform.tfstate

所感

  • terraform remote commandsが削除され、tfファイルにBackendが設定できるようになったことは私にはとても刺さりました。

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
2