Help us understand the problem. What is going on with this article?

Terraform v0.11でAmazon S3 backendを使ってtfstateファイルを管理する

More than 3 years have passed since last update.



どのようにstate file(いわゆる.tfファイル)を管理し適用するか定義するもの。
Backend typeとしてAmazon S3だけでなくAzure Storage、Google Cloud Storageなどのパブリッククラウドのオブジェクトストレージサービスやetcdやconsulなどの分散KVSとして使えるもの等が用意されている。

使うメリットとしては、単純にstate fileをリモートで一括管理できるというだけでなく、backendによってはリモートでの実行も可能にしてくれるらしい。




terraform {
  required_version = ">= 0.11.0"

  backend "s3" {
    bucket = "your-bucket-name"  //e.g. terraform-state-bucket
    key    = "" //この文字列でtfファイルが作られてS3に置かれる 
    region = "ap-northeast-1" //S3のリージョン

provider "aws" {      // ProviderはAWSに限らずなんでも使える
  access_key = "hoge"
  secret_key = "hogehoge"
  region     = "${var.region}"

resource "hoge" "fuga" {


terraform init するとmoduleやpluginと共にbackendの設定も行われる。

-> % terraform init
Initializing modules...
- module.xxxxx

Initializing the 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
- Downloading plugin for provider "aws" (1.6.0)...

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.

* version = "~> 1.6"

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.

問題なくbackendにS3が設定されると、terraform planapplyshowなどの実行の際にはS3に保存してあるtfファイルを見てくれる。


Screen Shot 2018-01-01 at 23.04.15.png




Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away