LoginSignup
1
1

More than 3 years have passed since last update.

Terraform Cloud(Terraform Cloud Free Tier) を使ってみた

Last updated at Posted at 2019-06-06

始まりはある気づきから

知人から 「Terraform Cloud ってもう見ました?」というSlackが・・・。(この導きに感謝!!)

で、Terraformの公式を見ると・・・あれ?

スクリーンショット 2019-06-06 15.43.50.png

Introducing Terraform Cloud Remote State Management

こんなメッセージさっきまであったっけか・・・なんだろう。

(どうやら触っていて気づいたのですが Terraform Cloud Free Tier というらしいですね。
 Enterpriseのフリー版ということなんでしょうかね。こちら が公式)

早速サインアップしてみました。

サインインするとこんな画面になりました。

何をすればいのかもよくわかりませんが、まずは Create New Organization を選択。

スクリーンショット 2019-06-06 15.47.29.png

するとこんな画面になったので、 kei1 という名前でorganizationを作って登録。

スクリーンショット 2019-06-06 15.49.32.png

左上にorganization名が出てますがworkspaceが存在しないような・・・。まぁあとで分かるのでしょう。

スクリーンショット 2019-06-06 15.50.31.png

右上に Get Started というボタンがあるので押してみます。
するとこんなモーダルが。

スクリーンショット 2019-06-06 15.51.21.png

以下をやる必要があるようで、一部はリンクが貼られていたりもします。

  • Download Terraform 0.11.13 or later ⇢ クリア済み
  • Create your user access token -> これはやる必要あり
  • Configure the Terraform Enterprise remote backend -> !? Enterpriseとな!?
  • Re-initialize your local Terraform workspace with terraform init -> remote stateに切り替えたら terraform init というのは当然なのでそれはそれでやる

Create your user access token

https://app.terraform.io/app/settings/tokens
から作れるようです。こんな画面になります。

スクリーンショット 2019-06-06 15.54.32.png

DESCRIPTION欄を入力して Generate token 押下。

tokenが生成されるのでそれをコピー。
ここでコピーしておかないと二度と表示されないので注意です。

Configure the Terraform Enterprise remote backend

リンク先に飛ぶとここでした。

Note: We recommend using Terraform v0.11.13 or newer with this backend. This backend requires either a Terraform Enterprise account on app.terraform.io or a private instance of Terraform Enterprise (version v201809-1 or newer).

either a Terraform Enterprise account on app.terraform.io or a private instance of Terraform Enterprise

今回作業をしているのがまさに app.terraform.io なので、条件は満たしているのではないだろうか。
Terraform Enterprise remote backend とか言われて え!?Enterprise必須なんすか? とか思ったけどいける気がする。

あと Get Started な画面の末尾にここへのリンクがありますもんね。
https://www.terraform.io/docs/enterprise/free/

ということで、backendの設定はこうするらしいです。

まず、terraformブロックを以下のように書く。

terraform.tf というファイル名にしました。

terraform {
  backend "remote" {
    hostname = "app.terraform.io"
    organization = "kei1" // <-- ここはいいとして・・・

    workspaces {
      name = "default" // <-- workspaceなんてまだ作ってないんだからdefaultでいいんかな・・・
    }
  }
}

さらに、自身のホームディレクトリに、 .terraformrc というファイルを作り、そこに以下の設定を記載します。

.terraformrc

credentials "app.terraform.io" {
  token = "Zc37BBcXCC(略)"
}

Re-initialize your local Terraform workspace
with terraform init

この状態で、 terraform init します。

% terraform init

Initializing the backend...

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

Initializing provider plugins...

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.

どうやらうまく行ったっぽいですね。

ちょっと画面に戻ってみますが、

スクリーンショット 2019-06-06 16.17.37.png

なるほど。 workspace名に何を書けば良いのだろう・・・と悩みましたが、そうじゃなかった。
書いたworkspaceが作成される・・・だったのですね。
(terraform workspace コマンドとの関連性はどうなるのやら。それは調査しよう)

2019/6/7 追記

workspaceはサポートされないようですね、このケースだと。

% terraform workspace list
workspaces not supported

なので、terraformブロックで宣言した、本来とは異なる意味合いのworkspaceというものに対して、常に操作をするということになりますね。
なので、従来と同じようなことをしたいなら、何かしらの条件によってterraformブロック内のworkspace名を3項演算子とかで切り替えていく形にでもするしかないのかなと思ったのですが、 terraformブロックのworkspaces内でvarが使えないようですね・・・。

実際に terraform apply してみる

ではapplyしてみます。

ちなみに main.tf として以下を書いてます。

キーペアをいっこだけつくるtf

resource "ecl_compute_keypair_v2" "keypair_1" {
  name = "keypair_1"
}
% terraform apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # ecl_compute_keypair_v2.keypair_1 will be created
  + resource "ecl_compute_keypair_v2" "keypair_1" {
      + fingerprint = (known after apply)
      + id          = (known after apply)
      + name        = "keypair_1"
      + private_key = (known after apply)
      + public_key  = (known after apply)
      + region      = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

ecl_compute_keypair_v2.keypair_1: Creating...
ecl_compute_keypair_v2.keypair_1: Creation complete after 1s [id=keypair_1]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

おー、キーペアしか作っていませんが、無事作成できました :tada:

画面を追いかけていくと、ちゃんとstateが見えますね。

スクリーンショット 2019-06-06 16.19.19.png

では、ちょっとupdateをかけてみましょうか。

main.tf を以下のようにします。


resource "ecl_compute_keypair_v2" "keypair_1" {
  name = "keypair_1"
}

resource "ecl_compute_keypair_v2" "keypair_2" {
  name = "keypair_2"
}

キーペアを足しました。

では、applyします。

% terraform apply                                                            (git)-[master]
ecl_compute_keypair_v2.keypair_1: Refreshing state... [id=keypair_1]

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # ecl_compute_keypair_v2.keypair_2 will be created
  + resource "ecl_compute_keypair_v2" "keypair_2" {
      + fingerprint = (known after apply)
      + id          = (known after apply)
      + name        = "keypair_2"
      + private_key = (known after apply)
      + public_key  = (known after apply)
      + region      = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

ecl_compute_keypair_v2.keypair_2: Creating...
ecl_compute_keypair_v2.keypair_2: Creation complete after 1s [id=keypair_2]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

無事成功したようです。

stateはどうなっているだろう。

スクリーンショット 2019-06-06 16.21.47.png

おー、stateの履歴が見える・・・。

しかも差分だけがハイライトされたりもするようですね。

スクリーンショット 2019-06-06 16.28.56.png

destroyもしてみましたが、同じ使用感で無事リソースの削除まで行けました。

あら、すごいわ。

まとめ

あれ、これPulumiいらないんじゃw? :sweat_smile:
Terraform単体でリモートステートを履歴含めて管理できるようになったってことですし。
かつチーム管理もできるようですし。

以上、ざっと使ってみた感想でした。

2019/6/7 追記

Gitとかの連携まではできないっぽいですね。

Team/Memberについては検証した結果をコメントに残してみました。

1
1
2

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
1
1