型制約の詳細 — object / tuple / optional / any の完全解説

1. 概要

  • 基本型(string / number / bool)
  • コレクション型(list / map / set)
  • 構造体型(object / tuple)
  • optional() — オプション属性
  • any — 任意の型
  • 実際のユースケース

Terraformの型制約を正しく設定することで、変数への誤った値の渡し忘れや型ミスマッチを早期に検出できます。


2. 基本型

variable "instance_name" {
  description = "インスタンス名"
  type        = string
}

variable "instance_count" {
  description = "インスタンス数"
  type        = number
  default     = 1
}

variable "enable_monitoring" {
  description = "詳細モニタリングを有効にするか"
  type        = bool
  default     = false
}

3. コレクション型

# list(element_type) — 順序付きリスト(同じ型の要素)
variable "availability_zones" {
  description = "使用するアベイラビリティゾーン"
  type        = list(string)
  default     = ["ap-northeast-1a", "ap-northeast-1c"]
}

# map(element_type) — キーと値のマップ(値は同じ型)
variable "instance_types" {
  description = "環境ごとのインスタンスタイプ"
  type        = map(string)
  default = {
    dev = "t3.micro"
    stg = "t3.small"
    prd = "t3.medium"
  }
}

# set(element_type) — 重複なしの集合
variable "allowed_cidrs" {
  description = "許可するCIDRブロックの集合"
  type        = set(string)
  default     = ["10.0.0.0/8", "172.16.0.0/12"]
}

4. object型 — 異なる型の属性を持つ構造体

object()は複数の属性(それぞれが異なる型を持てる)をまとめた構造体型です。

terraform {
  required_version = ">= 1.9"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = "ap-northeast-1"
}

variable "environment" {
  description = "環境名"
  type        = string
  default     = "dev"
}

# object型: 異なる型の属性をまとめる
variable "server_config" {
  description = "サーバーの設定"
  type = object({
    instance_type = string
    disk_size_gb  = number
    encrypted     = bool
    tags          = map(string)
  })
  default = {
    instance_type = "t3.micro"
    disk_size_gb  = 20
    encrypted     = true
    tags          = { Role = "web" }
  }
}

data "aws_ami" "amazon_linux_2023" {
  most_recent = true
  owners      = ["amazon"]
  filter {
    name   = "name"
    values = ["al2023-ami-*-x86_64"]
  }
}

resource "aws_instance" "web" {
  ami           = data.aws_ami.amazon_linux_2023.id
  instance_type = var.server_config.instance_type

  root_block_device {
    volume_size = var.server_config.disk_size_gb
    volume_type = "gp3"
    encrypted   = var.server_config.encrypted
  }

  tags = merge(var.server_config.tags, {
    Name        = "${var.environment}-web"
    Environment = var.environment
    ManagedBy   = "terraform"
  })
}

5. optional() — オプション属性(Terraform 1.3+)

optional(type, default)を使うと、object型の属性を省略可能にできます。

variable "instance_config" {
  description = "インスタンス設定"
  type = object({
    instance_type = string
    # optional: 省略した場合はデフォルト値が使われる
    disk_size_gb  = optional(number, 20)
    encrypted     = optional(bool, true)
    monitoring    = optional(bool, false)
  })
  # disk_size_gb・encrypted・monitoringは省略可能
  default = {
    instance_type = "t3.micro"
  }
}

resource "aws_instance" "web2" {
  ami           = data.aws_ami.amazon_linux_2023.id
  instance_type = var.instance_config.instance_type
  monitoring    = var.instance_config.monitoring

  root_block_device {
    volume_size = var.instance_config.disk_size_gb  # 省略時は20
    volume_type = "gp3"
    encrypted   = var.instance_config.encrypted     # 省略時はtrue
  }

  tags = {
    Name        = "${var.environment}-web2"
    Environment = var.environment
    ManagedBy   = "terraform"
  }
}

6. tuple型 — 順序付きの異型要素

tuple()は各インデックスに型を指定できる固定長のリストです。使用頻度は低いです。

variable "cidr_range" {
  description = "CIDRの範囲 [開始, 終了]"
  type        = tuple([string, string])
  default     = ["10.0.0.0/24", "10.0.10.0/24"]
}

7. any — 任意の型

anyはどの型でも受け付けます。型チェックをスキップしたい場合に使いますが、できるだけ具体的な型を指定することを推奨します。

variable "tags" {
  description = "タグ(値の型は問わない)"
  type        = map(any)
  default     = {}
}

8. 関連記事


9. まとめ

  • 基本型: string / number / bool
  • コレクション型: list(型) / map(型) / set(型) — 同じ型の要素を扱う
  • object({attr = type, ...}) — 複数の異なる型の属性をまとめる
  • optional(type, default) — object型の属性を省略可能にする(Terraform 1.3+)
  • tuple([type1, type2, ...]) — 各インデックスに型を指定する固定長リスト
  • any — 任意の型を受け付ける(できるだけ使わない)

動作確認バージョン: Terraform >= 1.9 公式ドキュメント: https://developer.hashicorp.com/terraform/language/expressions/type-constraints