terraform cloudについて
公式tfstateのリモート管理やterraformリモート実行などができるterraformのcloudサービスっぽいやつ。
無料プランでできることを試す。
tfstate管理
- 下記のような感じで更新履歴見れて、差分も見やすい。


実行管理
- 画面からぽちっとでplan/applyできる。
- 過去の実行履歴も見れる
- VCS連携してPR作成、マージをトリガーにplan/applyとか可能。
- ローカルから実行することも可能
お試し
実際に使ってみる。sample用のリポジトリ作成
ディレクトリ構成は下記のような感じで一旦適当に作っておく。
backend.tfの内容がterraform cloudで実行するために必要な設定
vpc ├── backend.tf ├── main.tf ├── provider.tf └── variable.tf
terraform { backend "remote" { hostname = "app.terraform.io" organization = "megun-labo" workspaces { name = "terraform-cloud-sample-vpc" } } }
module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "2.64.0" name = "terraform-cloud-vpc" cidr = "10.0.0.0/16" azs = ["ap-northeast-1a", "ap-northeast-1c", "ap-northeast-1d"] public_subnets = var.public_subnets private_subnets = var.private_subnets enable_nat_gateway = false enable_vpn_gateway = false tags = { Name = "terraform-cloud-vpc" Terraform = "true" Environment = "dev" } }
provider "aws" { region = "ap-northeast-1" }
variable "public_subnets" { default = ["10.0.0.0/20", "10.0.16.0/20", "10.0.32.0/20"] } variable "private_subnets" { default = ["10.0.48.0/20", "10.0.64.0/20", "10.0.80.0/20"] }
terraform cloudアカウント作成
下記画面あたりからアカウント作成する。
organization作成
まずはorganizationを作成する必要がある。organization単位でユーザ参加・退会とかさせる模様。
適当に作る。

workspace設定
organization作成したらworkspace作成する。workspace単位でterraform実行(tfstate管理)する模様 VCS連携試したいので
version control workflow
を選択する







環境変数設定
terraform実行用のIAMキー(事前に作成しておく)を環境変数に登録する。
その際は、必ずSensitiveで登録する

画面からの実行
queue plan するとplanが実行される。


ローカルからの実行
terraform cloudで使用するapiトークンの作成と設定を行うローカルでplanしてみる$ terraform login Terraform will request an API token for app.terraform.io using your browser. If login is successful, Terraform will store the token in plain text in the following file for use by subsequent commands: /home/xxxx/.terraform.d/credentials.tfrc.json Do you want to proceed? Only 'yes' will be accepted to confirm. Enter a value: yes --------------------------------------------------------------------------------- Open the following URL to access the tokens page for app.terraform.io: https://app.terraform.io/app/settings/tokens?source=terraform-login --------------------------------------------------------------------------------- Generate a token using your browser, and copy-paste it into this prompt. Terraform will store the token in plain text in the following file for use by subsequent commands: /home/xxxx/.terraform.d/credentials.tfrc.json Token for app.terraform.io: Enter a value: Retrieved token for user megun --------------------------------------------------------------------------------- Success! Terraform has obtained and saved an API token. The new API token will be used for any future Terraform command that must make authenticated requests to app.terraform.io.
ローカルで実行してるように見えて、リモート(terraform cloud)で実行した結果を表示してるっぽい。(plan結果のURLから結果見ることできる。)$ terraform plan Running plan in the remote backend. Output will stream here. Pressing Ctrl-C will stop streaming the logs, but will not stop the plan running remotely. Preparing the remote plan... The remote workspace is configured to work with configuration at vpc relative to the target repository. Terraform will upload the contents of the following directory, excluding files or directories as defined by a .terraformignore file at /home/xxxx/terraform-cloud-sample/.terraformignore (if it is present), in order to capture the filesystem context the remote workspace expects: /home/xxxx/terraform-cloud-sample To view this run in a browser, visit: https://app.terraform.io/app/megun-labo/terraform-cloud-sample-vpc/runs/run-fZSqqBeAb1e9Z8cd Waiting for the plan to start... Terraform v0.14.4 Configuring remote state backend... Initializing Terraform configuration... ^~ 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, no actions need to be performed.
PullRequestからの実行
PR作成すると自動でcheck追加(terraform cloudでplan)してくれる


