テンプレートファイルは*.tftpl形式でtemplates/ディレクトリに配置する
ルール
*Save template files read by Terraform’s templatefile function as .tftpl in the caller’s templates/ (e.g., ./modules/api/templates/s3_bucket_policy.tftpl)
(Terraformのtemplatefileファンクションで読み込むテンプレートファイルは、*.tftpl形式で呼び出し元のtemplates/に保存する)
解説
Terraformのtemplatefile()で呼び出すテンプレートファイルは、templates/ディレクトリに、.tftpl拡張子を使って保存します。
決めの問題なのでAWSの推奨に従っています。
サンプルコード
# modules/api/main.tf
resource "aws_s3_bucket_policy" "main" {
bucket = aws_s3_bucket.main.id
# 同じモジュール内のtemplates/からテンプレートを読み込む
policy = templatefile("${path.module}/templates/s3_bucket_policy.tftpl", {
bucket_name = aws_s3_bucket.main.id
account_id = data.aws_caller_identity.current.account_id
})
}
# ディレクトリ構造:
# modules/
# api/
# main.tf
# templates/
# s3_bucket_policy.tftpl # S3バケットポリシーのテンプレート
# iam_policy.tftpl # IAMポリシーのテンプレート
参考リンク
Show Text to Shareテンプレートファイルは*.tftpl形式でtemplates/ディレクトリに配置する https://www.tricrow.com/infrastructure/development-guidline/repository_structure.standard.templates.html

