テンプレートファイルは*.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
この記事を書いた人
T.Nakamura
T.Nakamura
AIが大好きなクラウドエンジニア。IT業界歴10年以上。標準化と効率化を追求している。技術ネタを発信中。フォローお気軽にどうぞ!フォローはこちら

カテゴリ

タグ