◾️はじめに
https://dk521123.hatenablog.com/entry/2023/12/24/173633
の続き。 最近、Terraform の variable を使って、学びが多かったので まとめておく
目次
【1】variable 【2】構文 【3】Hello world 【4】値の設定の仕方 1)コマンドの引数で直接設定「-var option」 2)ファイルで設定「.tfvars files」 3)環境変数で設定「TF_VAR_XXX」
【1】variable
* 変数の定義 => variable セクションで定義
https://developer.hashicorp.com/terraform/language/values/variables
【2】構文
* 詳細は以下を参照。 => ざっくりわかる例を載せておく
Terraform ~ variable / 基本編 ~
https://dk521123.hatenablog.com/entry/2025/09/20/002058
variable "env" { description = "Environmental" type = string default = "dev" validation { condition = contains(["dev", "stage", "prod"], env) error_message = "Only dev / stage / prod" } }
【3】Hello world
variables.tf (変数の定義)
variable content { description = <<-EOD (Optional) This value is an explanation EOD type = string default = "hello world!!!?" } variable filename { description = <<-EOD (Required) This value is an output file name (e.g. hello.txt) EOD type = string }
main.tf (呼び出し元)
# var.<変数名>で参照 resource "local_file" "helloworld" { content = "[Result] ${var.content}" filename = var.filename }
実行例
$ terraform init # terraform plan -var "【Key】=【Value】" # terraform plan -var "content=Hi, World" $ terraform apply -var "content=Hi, World" var.filename (Required) This value is an output file name (e.g. hello.txt) Enter a value: output.txt << 必須となっている出力ファイル名を入力 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 <<デプロイしてよければ「yes」を入力 # 確認 $ more output.txt [Result] Hi, World
【4】値の設定の仕方
* 以下の公式ドキュメントに記載されている
* 以下のTerraformコードを使って、呼び出してみる
main.tf
variable "content" { type = string } variable "filename" { type = string } resource "local_file" "helloworld" { content = "[Result] ${var.content}" filename = var.filename }
1)コマンドの引数で直接設定「-var option」
https://developer.hashicorp.com/terraform/language/values/variables#variables-on-the-command-line
# -var="【Key】=【Value】" # 複数の場合は、『-var="【Key1】=【Value1】" --var="【Key2】=【Value2】" ...』 terraform apply -var="content=Hello world" -var="filename=result.txt" # 他にもこんな感じでできる terraform apply -var='image_id_list=["ami-abc123","ami-def456"]' -var="instance_type=t2.micro" terraform apply -var='image_id_map={"us-east-1":"ami-abc123","us-east-2":"ami-def456"}'
2)ファイルで設定「.tfvars files」
* ファイルの拡張子は、「.tfvars」 or 「.tfvars.json」 * 環境ごとに変数を切り替えるのに使えそう
# var-file="【ファイル名】.tfvars" でファイル設定 terraform apply -var-file="dev.tfvars"
dev.tfvars
content="Hello world!!!" filename="result.txt"
3)環境変数で設定「TF_VAR_XXX」
https://developer.hashicorp.com/terraform/language/values/variables#environment-variables
# Linux # export TF_VAR_content=Hello world!!? # export TF_VAR_filename=result.txt # Windows set TF_VAR_content=Hello world!!? set TF_VAR_filename=result.txt terraform plan
関連記事
Terraform ~ 環境構築編 ~
https://dk521123.hatenablog.com/entry/2023/04/05/000224
Terraform ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/12/09/222057
Terraform ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2023/05/03/000000
Terraform ~ local ~
https://dk521123.hatenablog.com/entry/2023/12/24/173633
Terraform ~ variable / 基本編 ~
https://dk521123.hatenablog.com/entry/2025/09/20/002058
Terraform ~ 複数環境へデプロイすることを考える ~
https://dk521123.hatenablog.com/entry/2023/05/06/003645