filebase64 / filesha256 — ファイルハッシュ・エンコード関数

1. 概要

  • filebase64 — ファイルをBase64エンコードして文字列で返す
  • filesha256 — ファイルのSHA-256ハッシュ(hex)を返す
  • filesha512 / filemd5 — その他のファイルハッシュ
  • 実際のユースケース(Lambda更新検出・ユーザーデータ)

2. filebase64 — ファイルをBase64エンコード

filebase64(path)はファイルの内容をBase64エンコードした文字列を返します。バイナリファイルもそのまま扱えます。

# terraform consoleで確認(テキストファイルの場合)
> filebase64("hello.txt")
"aGVsbG8gd29ybGQ="

EC2ユーザーデータに使う

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

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 = "t3.micro"

  # スクリプトファイルをBase64エンコードしてuser_dataに渡す
  user_data = filebase64("${path.module}/scripts/setup.sh")

  root_block_device {
    volume_size = 20
    volume_type = "gp3"
    encrypted   = true
  }

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

3. filesha256 — ファイルのSHA-256ハッシュ

filesha256(path)はファイルのSHA-256ハッシュを16進数文字列で返します。Lambda・ECSなどのコード変更検出に定番です。

# ファイルが変わるとハッシュが変わり、source_code_hashが更新されてLambdaが再デプロイされる
resource "aws_lambda_function" "api" {
  function_name = "${var.environment}-api"
  role          = aws_iam_role.lambda.arn
  handler       = "index.handler"
  runtime       = "nodejs20.x"
  filename      = "${path.module}/lambda/function.zip"

  # filebase64sha256: ZIPのBase64エンコードしたSHA-256(Lambdaに渡す形式)
  source_code_hash = filebase64sha256("${path.module}/lambda/function.zip")

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

filebase64sha256はSHA-256ハッシュをBase64エンコードした形式で返します(Lambdaのsource_code_hashに必要な形式)。


4. filesha256 でS3オブジェクトの変更を検出する

resource "aws_s3_object" "config" {
  bucket = "${var.environment}-config"
  key    = "app-config.json"
  source = "${path.module}/configs/app-config.json"

  # ファイルのhashをetagに設定することでファイル変更時に更新される
  etag = filemd5("${path.module}/configs/app-config.json")

  tags = {
    Environment = var.environment
    ManagedBy   = "terraform"
  }
}

5. ファイルハッシュ関数の比較

関数出力形式主な用途
filebase64(path)Base64エンコード文字列user_data・バイナリ転送
filesha256(path)16進数SHA-256ハッシュファイル変更検出(hex)
filebase64sha256(path)Base64エンコードSHA-256Lambda source_code_hash
filesha512(path)16進数SHA-512ハッシュ高セキュリティなハッシュ
filemd5(path)16進数MD5ハッシュS3オブジェクトのetag

6. path.moduleとの組み合わせ

# path.moduleはTerraformファイルが置かれているディレクトリ
# モジュール内でファイルを参照するときは必ずpath.moduleを使う

resource "aws_instance" "web" {
  ami       = data.aws_ami.amazon_linux_2023.id
  instance_type = "t3.micro"

  # ✅ path.moduleで相対パスを解決
  user_data = filebase64("${path.module}/scripts/setup.sh")

  root_block_device {
    volume_size = 20
    volume_type = "gp3"
    encrypted   = true
  }

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

7. 関連記事


8. まとめ

  • filebase64(path) — ファイルをBase64エンコードして返す。user_dataへの直接渡しに使う
  • filesha256(path) — ファイルのSHA-256ハッシュ(hex)。ファイル変更検出に使う
  • filebase64sha256(path) — LambdaのsourceCodeHashに必要な形式(Base64エンコードSHA-256)
  • filemd5(path) — S3オブジェクトのetagに使うMD5ハッシュ
  • モジュール内では必ず${path.module}/を付けて相対パスを解決する

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