sum / alltrue / anytrue — リスト集計関数

1. 概要

  • sum — リストの合計値を計算
  • alltrue — リストのすべての要素がtrueか確認
  • anytrue — リストのいずれかの要素がtrueか確認
  • 実際のユースケース

2. sum — リストの合計値

sum(list)は数値のリストの合計を返します。

# terraform consoleで確認
> sum([1, 2, 3, 4, 5])
15

> sum([10.5, 20.5, 30])
61

> sum([])
# エラー: 空リストは不可

ストレージ合計の計算

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"
}

variable "disk_sizes_gb" {
  description = "各インスタンスのディスクサイズ(GB)のリスト"
  type        = list(number)
  default     = [20, 50, 100, 20]
}

locals {
  total_storage_gb = sum(var.disk_sizes_gb)  # 190
  avg_storage_gb   = local.total_storage_gb / length(var.disk_sizes_gb)  # 47.5
}

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

resource "aws_instance" "workers" {
  count         = length(var.disk_sizes_gb)
  ami           = data.aws_ami.amazon_linux_2023.id
  instance_type = "t3.micro"

  root_block_device {
    volume_size = var.disk_sizes_gb[count.index]
    volume_type = "gp3"
    encrypted   = true
  }

  tags = {
    Name        = "${var.environment}-worker-${count.index + 1}"
    Environment = var.environment
    ManagedBy   = "terraform"
    DiskSizeGB  = tostring(var.disk_sizes_gb[count.index])
  }
}

output "total_storage_gb" {
  value = local.total_storage_gb
}

3. alltrue — すべての要素がtrueか

alltrue(list)はboolのリストのすべての要素がtrueの場合にtrueを返します。空リストはtrueを返します。

# terraform consoleで確認
> alltrue([true, true, true])
true

> alltrue([true, false, true])
false

> alltrue([])
true

すべてのディスクが暗号化されているか検証する

variable "volumes" {
  type = list(object({
    size      = number
    encrypted = bool
  }))
  default = [
    { size = 20, encrypted = true  },
    { size = 50, encrypted = true  },
    { size = 100, encrypted = true }
  ]
}

locals {
  all_encrypted = alltrue([for v in var.volumes : v.encrypted])
}

resource "terraform_data" "encryption_check" {
  lifecycle {
    precondition {
      condition     = local.all_encrypted
      error_message = "すべてのボリュームで暗号化を有効にしてください。"
    }
  }
}

4. anytrue — いずれかの要素がtrueか

anytrue(list)はboolのリストのいずれかの要素がtrueの場合にtrueを返します。空リストはfalseを返します。

# terraform consoleで確認
> anytrue([false, true, false])
true

> anytrue([false, false, false])
false

> anytrue([])
false

本番環境設定が含まれているか確認する

variable "environments" {
  description = "デプロイ対象の環境リスト"
  type        = list(string)
  default     = ["dev", "stg"]
}

locals {
  has_production = anytrue([
    for env in var.environments : contains(["prd", "production"], env)
  ])

  # 本番環境が含まれる場合はより厳密な設定を適用
  deletion_protection = local.has_production
}

resource "aws_s3_bucket" "app" {
  bucket        = "${var.environment}-app"

  tags = {
    Name        = "${var.environment}-app"
    Environment = var.environment
    ManagedBy   = "terraform"
    HasPrd      = tostring(local.has_production)
  }
}

5. 比較表

関数入力空リストの場合ユースケース
sum(list)数値リストエラーストレージ合計・コスト試算
alltrue(list)boolリストtrue全設定の検証・precondition
anytrue(list)boolリストfalse条件付き設定の適用

6. 関連記事


7. まとめ

  • sum(list) — 数値リストの合計。空リストはエラーになるので注意
  • alltrue(list) — すべての要素がtrueか確認。空リストはtrue
  • anytrue(list) — いずれかの要素がtrueか確認。空リストはfalse
  • for式と組み合わせることでリストの条件検査が簡潔に書ける

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