Closed6

terraform cloudの変数でHCLを利用する

not75743not75743

ドキュメントはこのあたりかな
https://developer.hashicorp.com/terraform/cloud-docs/workspaces/variables
https://developer.hashicorp.com/terraform/cloud-docs/workspaces/variables/managing-variables#variable-values-and-format

terraform変数の場合HCLが使えるみたいです

You can use HCL for Terraform variables, but not for environment variables. The same Terraform version that performs runs in the workspace will interpret the HCL.
Variable values are strings by default. To enter list or map values, click the variable’s HCL checkbox (visible when editing) and enter the value with the same HCL syntax you would use when writing Terraform code. For example:

not75743not75743

変数の設定

  • terraform変数のみHCLが利用可能
    • HCL欄にチェックを入れる
  • tfvarsで管理している場合はそのまま貼り付ければ大丈夫そう
    • hclを1行に修正するとかは不要
not75743not75743

Objectの場合

sqs.tf
resource "aws_sqs_queue" "main" {
  name          = var.sqs_settings.name
  delay_seconds = var.sqs_settings.delay_seconds
  tags          = var.sqs_settings.tags
}
variables.tf
variable "sqs_settings" {
  type = object({
    name           = string
    tags           = map(string)
    delay_seconds  = number
  })
}
TerraformVariables
{
  name = "test-sqs-queue"
  tags = {
    Environment = "Test"
    Project = "Terraform Practice"
  }
  delay_seconds = 10
}
not75743not75743

listの場合

for_eachで回してみる

sqs.tf
resource "aws_sqs_queue" "main" {
  for_each      = toset(var.sqs_settings)
  name          = each.value
}
variables.tf
variable "sqs_settings" {
  type = list(string)
}
TerraformVariables
["queue1", "queue2", "queue3"]
not75743not75743

mapの場合

こちらもfor_eachで

sqs.tf
resource "aws_sqs_queue" "main" {
  for_each      = var.sqs_settings
  name          = each.key
  delay_seconds = each.value
}
variables.tf
variable "sqs_settings" {
  type = map(any)
}
TerraformVariables
{
    "queue1" = 10
    "queue2" = 20
    "queue3" = 30
}
このスクラップは2023/12/29にクローズされました