diff --git a/cloudfront.tf b/cloudfront.tf index 37065bf..94b3ea2 100644 --- a/cloudfront.tf +++ b/cloudfront.tf @@ -39,20 +39,21 @@ resource "aws_cloudfront_distribution" "redirect" { // Cache everything as long as possible, since they all get a redirect default_cache_behavior { - allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"] - cached_methods = ["HEAD", "GET", "OPTIONS"] - //target_origin_id = "S3-Origin" + allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"] + cached_methods = ["HEAD", "GET", "OPTIONS"] target_origin_id = "dummy" viewer_protocol_policy = "allow-all" - min_ttl = var.cache_duration - default_ttl = var.cache_duration - max_ttl = var.cache_duration + min_ttl = var.cache_duration_min + default_ttl = var.cache_duration_default + max_ttl = var.cache_duration_max compress = false forwarded_values { - // Allow caching based on protocol (http vs https) - headers = ["CloudFront-Forwarded-Proto"] - query_string = true + // Forward the protocol header so we can redirect using the same protocol + headers = ["CloudFront-Forwarded-Proto"] + // If it's not a static redirect, forward the query string so we can + // include it in the redirected URL + query_string = !local.is_static_redirect cookies { forward = "none" } diff --git a/main.tf b/main.tf index 702773b..e05ce1c 100644 --- a/main.tf +++ b/main.tf @@ -13,3 +13,7 @@ module "assert_region" { resource "random_id" "module_id" { byte_length = 8 } + +locals { + is_static_redirect = var.redirect_type == "STATIC_PATH" +} diff --git a/redirect-function.tf b/redirect-function.tf index de54456..424f948 100644 --- a/redirect-function.tf +++ b/redirect-function.tf @@ -2,7 +2,7 @@ module "jsonencoded_static_redirect_path" { source = "Invicton-Labs/jsonencode-no-replacements/null" version = "~> 0.1.1" // This trims all leading forward slashes - object = regex("^/*(.*)$", var.static_path == null ? "/" : var.static_path)[0] + object = regex("^/*(.*)$", var.redirect_static_path == null ? "/" : var.redirect_static_path)[0] } module "jsonencoded_redirect_path_prefix" { source = "Invicton-Labs/jsonencode-no-replacements/null" @@ -31,7 +31,7 @@ function trimLeft(string, charToRemove) { function handler(event) { var protocol = event.request.headers['cloudfront-forwarded-proto']['value']; - ${var.redirect_type == "STATIC_PATH" ? <