From ec5ba589495cd16b2dcc1ae904b25a77a6e071e6 Mon Sep 17 00:00:00 2001 From: franklinpashok Date: Mon, 29 Apr 2024 19:30:57 +0800 Subject: [PATCH] Add condition to create function --- cloudfront.tf | 2 ++ locals.tf | 9 ++++----- variables.tf | 6 ++++++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/cloudfront.tf b/cloudfront.tf index 536bf65..c61319a 100644 --- a/cloudfront.tf +++ b/cloudfront.tf @@ -71,8 +71,10 @@ module "cdn" { } resource "aws_cloudfront_function" "viewer_request" { + count = var.associate_function ? 1 : 0 name = var.default_index_function_name runtime = "cloudfront-js-1.0" publish = true code = var.override_default_index_function_code == "" ? templatefile("${path.module}/templates/viewer-request-default.js", { default_root_object = var.default_root_object }) : var.override_default_index_function_code } + diff --git a/locals.tf b/locals.tf index b082151..5973222 100644 --- a/locals.tf +++ b/locals.tf @@ -1,9 +1,8 @@ locals { - default_function_association = length(var.default_cache_behavior) == 0 ? { - function_association = { - viewer-request = { - function_arn = aws_cloudfront_function.viewer_request.arn - } + function_association = (var.associate_function && length(var.default_cache_behavior) == 0) ? { + viewer-request = { + function_arn = aws_cloudfront_function.viewer_request.arn } } : {} } + diff --git a/variables.tf b/variables.tf index da89c8f..60ab7e2 100644 --- a/variables.tf +++ b/variables.tf @@ -283,6 +283,12 @@ variable "web_acl_id" { default = "" } +variable "associate_function" { + description = "If the CloudFront function should be associated with the default cache behavior." + type = bool + default = false +} + variable "default_index_function_name" { description = "Name of the CloudFront Function to create for index page redirection" type = string