実現したいこと
現在、TerraformにてAWSに環境を作成しています。
複数の環境を用意する必要があるのですが、なるべくTerraform側のソースを共通化したいと考えております。
- 環境はディレクトリ単位で切り替える
- ディレクトリ単位でterraform initでterraformの環境を準備する
- CloudFrontなどのAWSリソースを定義するTerraformのソースは別ディレクトリに置いて、環境ごとのディレクトリにはそのファイルをシンボリックリンクを貼る
- 各ディレクトリにterraform.tfvarsファイルを置き、環境ごとの差異がある分を定義する
- Terraformのソース内では、できるだけTerraform Registroyで公開されているModuleを利用して、resource文で定義する箇所を減らしたい
- 質問したいモジュールとしてはこちらになります https://registry.terraform.io/modules/terraform-aws-modules/cloudfront/aws/latest
CloudFrontそのものを作成する・しないは、create_distribution の true / falseにteraform.tfvars内で定義したvaliable変数を渡して制御しています。
環境ごとにCloudFront Functionが必要な場合と不要な場合があります。
発生している問題・分からないこと
以下のように記述してCloudFront Functionが必要な場合は問題なく
hcl
1 function_association = { 2 viewer-request = { 3 function_arn = var.cloudfront_viewer_request_arn 4 } 5 }
function_arn の部分を function_arn = "" にすると、以下のようなエラーになります。
text
1module.cloudfront[0].aws_cloudfront_distribution.this[0]: Modifying... [id=E19I5NMMR4CRHM] 2╷ 3│ Error: updating CloudFront Distribution (〓ディストリビューションID〓): operation error CloudFront: UpdateDistribution, https response error StatusCode: 400, RequestID: 〓リクエストID〓, api error MalformedXML: 1 validation error detected: Value '' at 'distributionConfig.defaultCacheBehavior.functionAssociations.items.1.member.functionARN' failed to satisfy constraint: Member must satisfy regular expression pattern: arn:aws:cloudfront::[0-9]{12}:function\/[a-zA-Z0-9-_]{1,64} 4│ 5│ with module.cloudfront[0].aws_cloudfront_distribution.this[0], 6│ on .terraform\modules\cloudfront\main.tf line 27, in resource "aws_cloudfront_distribution" "this": 7│ 27: resource "aws_cloudfront_distribution" "this" { 8│
function_arn の部分を function_arn = false にすると、以下のようなエラーになります。
text
1Planning failed. Terraform encountered an error while generating this plan. 2 3╷ 4│ Error: "default_cache_behavior.0.function_association.0.function_arn" (false) is an invalid ARN: arn: invalid prefix 5│ 6│ with module.cloudfront[0].aws_cloudfront_distribution.this[0], 7│ on .terraform\modules\cloudfront\main.tf line 27, in resource "aws_cloudfront_distribution" "this": 8│ 27: resource "aws_cloudfront_distribution" "this" { 9│ 10╵
function_arn の部分を function_arn = null にすると、以下のようなエラーになります。
text
1Planning failed. Terraform encountered an error while generating this plan. 2 3╷ 4│ Error: Missing required argument 5│ 6│ with module.cloudfront[0].aws_cloudfront_distribution.this[0], 7│ on .terraform\modules\cloudfront\main.tf line 27, in resource "aws_cloudfront_distribution" "this": 8│ 27: resource "aws_cloudfront_distribution" "this" { 9│ 10│ The argument "default_cache_behavior.0.function_association.0.function_arn" is required, but no definition was found. 11╵
こういった動作をさせることは可能でしょうか。
可能な場合、どのように記述すべきでしょうか。
以上、よろしくお願いいたします。
該当のソースコード
特になし
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
Webを検索してもマッチした情報を見つけることができませんでした。
補足
環境は以下の通りです。
Terraform v1.9.2
provider registry.terraform.io/hashicorp/aws v5.58.0
CoudFrontモジュール terraform-aws-modules/cloudfront/aws 3.4.0

回答2件
あなたの回答
tips
プレビュー