diff --git a/edge-terraform-router/modules/service-vcl/variables.tf b/edge-terraform-router/modules/service-vcl/variables.tf index a4611c5..7396edb 100644 --- a/edge-terraform-router/modules/service-vcl/variables.tf +++ b/edge-terraform-router/modules/service-vcl/variables.tf @@ -5,4 +5,14 @@ variable "fastly_routes" { origin_name = string })) default = [] +} + +variable "add_header_based_on_path" { + description = "A list of objects representing path-based header additions. If the request matches the path, the specified header is added." + type = list(object({ + path = string + header_name = string + header_value = string + })) + default = [] } \ No newline at end of file diff --git a/edge-terraform-router/modules/service-vcl/vcl-service.tf b/edge-terraform-router/modules/service-vcl/vcl-service.tf index c2f3be7..7789a01 100644 --- a/edge-terraform-router/modules/service-vcl/vcl-service.tf +++ b/edge-terraform-router/modules/service-vcl/vcl-service.tf @@ -42,5 +42,33 @@ resource "fastly_service_vcl" "edge-terraform-router-demo" { } } + # Iterates over the array to create path-based request conditions + dynamic "condition" { + for_each = var.add_header_based_on_path + content { + # Creates a unique condition name based on the path (e.g., "path-condition-anything-foo") + name = "path-condition-${replace(trimprefix(condition.value.path, "/"), "/", "-")}" + priority = 10 + # Evaluates the incoming request path against the path variable + statement = "req.url.path == \"${condition.value.path}\"" + type = "REQUEST" + } + } + + # Iterates over the array to add request headers based on path conditions + dynamic "header" { + for_each = var.add_header_based_on_path + content { + # Creates a unique header action name based on the header name and path + name = "header-${header.value.header_name}-${replace(trimprefix(header.value.path, "/"), "/", "-")}" + action = "set" + type = "request" + destination = "http.${header.value.header_name}" + source = "\"${header.value.header_value}\"" + # Links this header action to the corresponding path condition above + request_condition = "path-condition-${replace(trimprefix(header.value.path, "/"), "/", "-")}" + } + } + force_destroy = true } \ No newline at end of file diff --git a/edge-terraform-router/prod-delivery/main.tf b/edge-terraform-router/prod-delivery/main.tf index 260110f..d7ad562 100644 --- a/edge-terraform-router/prod-delivery/main.tf +++ b/edge-terraform-router/prod-delivery/main.tf @@ -1,6 +1,7 @@ module "service_vcl" { source = "../modules/service-vcl" fastly_routes = var.fastly_routes + add_header_based_on_path = var.add_header_based_on_path providers = { fastly = fastly diff --git a/edge-terraform-router/prod-delivery/variables.tf b/edge-terraform-router/prod-delivery/variables.tf index a4611c5..7396edb 100644 --- a/edge-terraform-router/prod-delivery/variables.tf +++ b/edge-terraform-router/prod-delivery/variables.tf @@ -5,4 +5,14 @@ variable "fastly_routes" { origin_name = string })) default = [] +} + +variable "add_header_based_on_path" { + description = "A list of objects representing path-based header additions. If the request matches the path, the specified header is added." + type = list(object({ + path = string + header_name = string + header_value = string + })) + default = [] } \ No newline at end of file