1. 概要
parseint— 文字列を整数に変換signum— 数値の符号を返すlog— 対数を計算する- 実際のユースケース
2. parseint — 文字列を整数に変換
parseint(string, base)は指定した基数(2〜16)の文字列を整数に変換します。tonumber()は10進数のみですが、parseint()は2進数・8進数・16進数も扱えます。
# terraform consoleで確認
> parseint("42", 10)
42
> parseint("ff", 16)
255
> parseint("11111111", 2)
255
> parseint("377", 8)
255
16進数のポート番号を変換する
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"
}
locals {
# 16進数表記のポート番号を10進数に変換
# 0x01BB = 443 (HTTPS)
https_port = parseint("01BB", 16) # → 443
# マスクビットの計算(2進数)
# /24 サブネットのホストビット数を計算
host_bits = 32 - parseint("11111111111111111111111100000000", 2) # 複雑なので実際はcidrsubnetsを使う
}
data "aws_ami" "amazon_linux_2023" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["al2023-ami-*-x86_64"]
}
}
resource "aws_security_group" "web" {
name = "${var.environment}-web"
description = "${var.environment} web security group"
ingress {
from_port = local.https_port # 443
to_port = local.https_port
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "HTTPS"
}
tags = {
Name = "${var.environment}-web"
Environment = var.environment
ManagedBy = "terraform"
}
}
resource "aws_instance" "web" {
ami = data.aws_ami.amazon_linux_2023.id
instance_type = "t3.micro"
vpc_security_group_ids = [aws_security_group.web.id]
root_block_device {
volume_size = 20
volume_type = "gp3"
encrypted = true
}
tags = {
Name = "${var.environment}-web"
Environment = var.environment
ManagedBy = "terraform"
}
}
3. signum — 数値の符号を返す
signum(number)は正の数なら1、負の数なら-1、ゼロなら0を返します。
# terraform consoleで確認
> signum(10)
1
> signum(-5)
-1
> signum(0)
0
locals {
# インスタンス数の変化量から増減方向を判定
current_count = 5
desired_count = 3
diff = local.desired_count - local.current_count # -2
change_direction = signum(local.diff) # -1 (減少)
# 増加/減少/変化なしに応じたアクション
action = local.change_direction == 1 ? "scale-out" : (
local.change_direction == -1 ? "scale-in" : "no-change")
}
4. log — 対数を計算する
log(number, base)はnumberのbaseを底とする対数を返します。
# terraform consoleで確認
> log(8, 2)
3 # 2^3 = 8
> log(100, 10)
2 # 10^2 = 100
> log(1024, 2)
10 # 2^10 = 1024
locals {
# ストレージ容量から必要なビット数を計算
# 1TB = 2^40 bytes → 40ビット必要
storage_bytes = 1099511627776 # 1TB in bytes
bits_required = ceil(log(local.storage_bytes, 2)) # → 40
}
5. parseintとtonumberの違い
# tonumber: 10進数の文字列のみ
> tonumber("42")
42
> tonumber("ff")
# エラー: 16進数は変換できない
# parseint: 任意の基数
> parseint("ff", 16)
255
> parseint("42", 10)
42
6. 関連記事
- abs / ceil / floor / max / min / pow — 基本的な数値関数
- sum / alltrue / anytrue — 集計関数
- cidrsubnets / cidrhost / cidrnetmask — CIDR操作関数
- 条件式(三項演算子)— 使い方
- for式 — リスト・mapを変換・フィルタリング
- 型システム入門 — string/number/bool/list/map/object
7. まとめ
parseint(str, base)— 2〜16進数の文字列を10進数の整数に変換。tonumber()は10進数のみsignum(n)— 正なら1、負なら-1、0なら0を返す。増減方向の判定に使うlog(n, base)— 対数の計算。ストレージサイズの指数計算などに使う- これらはニッチな関数で使う場面は限られるが、数値計算が必要なモジュールで役立つ
動作確認バージョン: Terraform >= 1.9 公式ドキュメント: https://developer.hashicorp.com/terraform/language/functions/parseint