Skip to content

Commit

Permalink
Add condition to create function
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinpashok committed Apr 29, 2024
1 parent 8ec2206 commit ec5ba58
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 2 additions & 0 deletions cloudfront.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

9 changes: 4 additions & 5 deletions locals.tf
Original file line number Diff line number Diff line change
@@ -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
}
} : {}
}

6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ec5ba58

Please sign in to comment.