Skip to content

Commit

Permalink
Separate cache durations; conditional query forwarding; update variab…
Browse files Browse the repository at this point in the history
…le docs
  • Loading branch information
KyleKotowick committed Mar 31, 2022
1 parent 51fa8fb commit a9b607a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
19 changes: 10 additions & 9 deletions cloudfront.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
4 changes: 4 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ module "assert_region" {
resource "random_id" "module_id" {
byte_length = 8
}

locals {
is_static_redirect = var.redirect_type == "STATIC_PATH"
}
4 changes: 2 additions & 2 deletions redirect-function.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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" ? <<EOF2
${local.is_static_redirect ? <<EOF2
var new_uri = protocol + "://${var.domain_to}/" + ${module.jsonencoded_static_redirect_path.encoded};
EOF2
: <<EOF2
Expand Down
24 changes: 18 additions & 6 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ variable "domain_to" {
}

variable "redirect_type" {
description = "The type of redirect to perform. Options are `KEEP_PATH` or `STATIC_PATH`. If `STATIC_PATH` is used, the `static_path` variable must also be provided. Defaults to `KEEP_PATH`."
description = "The type of redirect to perform. Options are `KEEP_PATH` or `STATIC_PATH`."
type = string
default = "KEEP_PATH"
validation {
Expand All @@ -22,7 +22,7 @@ variable "redirect_type" {
}
}

variable "static_path" {
variable "redirect_static_path" {
description = "The fixed, static path to redirect all requests to. It is only used if the `redirect_type` variable is set to `STATIC_PATH`."
type = string
default = "/"
Expand All @@ -39,7 +39,7 @@ EOF
}

variable "redirect_code" {
description = "The HTTP code to use for the redirect. Options are `301` (moved permanently) or `302` (moved temporarily). Defaults to `301`."
description = "The HTTP code to use for the redirect. Options are `301` (moved permanently) or `302` (moved temporarily)."
type = number
default = 301
validation {
Expand All @@ -49,7 +49,7 @@ variable "redirect_code" {
}

variable "lambda_log_retention_days" {
description = "The number of days to keep the Lambda@Edge (redirect generation function) logs for. Defaults to `14`."
description = "The number of days to keep the Lambda@Edge (redirect generation function) logs for."
type = number
default = 14
}
Expand All @@ -64,8 +64,20 @@ variable "logging_config" {
default = null
}

variable "cache_duration" {
description = "The number of seconds to use for the CloudFront cache TTL (min, default, and max)."
variable "cache_duration_min" {
description = "The minimum number of seconds to use for the CloudFront cache TTL."
type = number
default = 86400
}

variable "cache_duration_default" {
description = "The default number of seconds to use for the CloudFront cache TTL."
type = number
default = 86400
}

variable "cache_duration_max" {
description = "The maximum number of seconds to use for the CloudFront cache TTL."
type = number
default = 86400
}
Expand Down

0 comments on commit a9b607a

Please sign in to comment.