Terraform Cloud(Free Plan)でできること
はじめに
Terraform Cloudでできることが分かりづらい。触り始めえればすぐにわかるのだが、簡単に概要を紹介しておく。
結論
Terraform Cloud(Free Plan)では主に下記のようなことができる。
- マネージドな環境への
tfstateファイルの保存 - 保存した
tfstateファイルのバージョン管理 - マネージドな環境でのTerraform実行(
plan/apply/destroy) - Terraform実行のワークフロー構築
補足
Free PlanとPaid Planの機能比較はこのページが見やすい。
Terraform | HashiCorp | 取扱製品 | ネットワールド
公式情報の中だとこちらで概要がわかる。
HashiCorp Terraform: Enterprise Pricing, Packages & Features
tfstateの保存場所のみ手で作っていると環境が散らかりがちになるので、個人で作業する分にも意外と便利。ただしS3等にtfstateを保存している場合に比べて実行は遅め。
補足
そのうち網羅的に整理して再度まとめたい。
Terraformでtemplatefileを利用してcustom_dataにbase64encodeしたcloud-configファイルを渡す
はじめに
紹介するコードで実現できることはタイトルの通り。
templatefileとbase64encodeを併用しているサンプルがあまり見当たらなかったので、自分が書いたものを載せておく。
結論
コードのサンプルは以下。関連する箇所のみ抜粋してある。[...]は中略箇所を表す。
main.tf
resource "azurerm_linux_virtual_machine" "mylinuxvm" {
custom_data = base64encode(templatefile("./files/cloud-config.cfg", { computer_name = local.computer_name }))
[...]
}
locals.tf
locals {
computer_name = "mycomputername"
}
files/cloud-config.cfg
#cloud-config
runcmd:
- hostnamectl set-hostname ${computer_name}
provider.tf
terraform {
required_version = "~> 1.1.6"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=2.97.0"
}
}
}
backend "azurerm" {
[...]
}
provider "azurerm" {
features {}
}
補足
custom_dataにcloud-configファイルの内容を渡す場合、base64encodeする必要がある。加えてcloud-configファイルに変数を利用したいケースがあると思う。
参照するファイル内に変数を利用したいケースで以前から使用されているtemplate_fileは非推奨。0.12以降ではtemplatefileを使用する。
In Terraform 0.12 and later, the templatefile function offers a built-in mechanism for rendering a template from a file. Use that function instead, unless you are using Terraform 0.11 or earlier.