main.tf 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. locals {
  2. name = var.name != "" ? var.name : var.hostname
  3. backend_name = var.backend_name != "" ? var.backend_name : "${var.hostname} - backend"
  4. ssl_hostname = var.ssl_hostname != "" ? var.ssl_hostname : var.hostname
  5. datadog_format = replace(file("${path.module}/logging/datadog.json"), "__service__", var.datadog_service)
  6. vcl_exoscale_forward = templatefile("${path.module}/vcl/exoscale_forward.vcl", { hostname = replace(var.app_hostname, ".", "-") })
  7. vcl_recv_various_fixups = file("${path.module}/vcl/recv_misc_fixups.vcl")
  8. vcl_remove_response_headers = file("${path.module}/vcl/remove_response_headers.vcl")
  9. vcl_segmented_caching = file("${path.module}/vcl/segmented_caching.vcl")
  10. vcl_cors_headers = file("${path.module}/vcl/add_cors_headers.vcl")
  11. }
  12. resource "fastly_service_vcl" "files_service" {
  13. name = local.name
  14. http3 = true
  15. stale_if_error = true
  16. domain {
  17. name = var.hostname
  18. }
  19. backend {
  20. name = local.backend_name
  21. address = var.backend_address
  22. keepalive_time = 0
  23. override_host = local.ssl_hostname
  24. port = 443
  25. shield = var.shield_region
  26. ssl_check_cert = var.backend_ssl_check
  27. ssl_cert_hostname = var.backend_ssl_check ? local.ssl_hostname : ""
  28. ssl_sni_hostname = local.ssl_hostname
  29. use_ssl = true
  30. }
  31. # Datadog logging
  32. dynamic "logging_datadog" {
  33. for_each = var.datadog ? [1] : []
  34. content {
  35. name = "Datadog ${var.datadog_region}"
  36. format = local.datadog_format
  37. token = var.datadog_token
  38. region = var.datadog_region
  39. }
  40. }
  41. # Set custom Fastly purge rules
  42. condition {
  43. name = "Purge"
  44. statement = "req.request == \"FASTLYPURGE\""
  45. type = "REQUEST"
  46. priority = 10
  47. }
  48. header {
  49. name = "Fastly Purge"
  50. action = "set"
  51. destination = "http.Fastly-Purge-Requires-Auth"
  52. type = "request"
  53. priority = 10
  54. request_condition = "Purge"
  55. source = "\"1\""
  56. }
  57. # Additional headers for content security & nosniff
  58. header {
  59. name = "Content security policy"
  60. action = "set"
  61. type = "request"
  62. priority = 10
  63. destination = "http.Content-Security-Policy"
  64. source = "\"default-src 'none'; form-action 'none'\""
  65. }
  66. header {
  67. name = "Nosniff"
  68. action = "set"
  69. type = "request"
  70. priority = 10
  71. destination = "http.X-Content-Type-Options"
  72. source = "\"nosniff\""
  73. }
  74. # Force TLS/HSTS settings
  75. # Creates similar objects to what the GUI switch creates.
  76. dynamic "request_setting" {
  77. for_each = var.force_tls_hsts ? [1] : []
  78. content {
  79. name = "Generated by force TLS and enable HSTS"
  80. bypass_busy_wait = false
  81. force_miss = false
  82. force_ssl = true
  83. max_stale_age = 0
  84. timer_support = false
  85. xff = ""
  86. }
  87. }
  88. dynamic "header" {
  89. for_each = var.force_tls_hsts ? [1] : []
  90. content {
  91. action = "set"
  92. destination = "http.Strict-Transport-Security"
  93. name = "Generated by force TLS and enable HSTS"
  94. type = "response"
  95. ignore_if_set = false
  96. priority = 100
  97. source = "\"max-age=${var.hsts_duration}\""
  98. }
  99. }
  100. # Custom VCL snippets
  101. snippet {
  102. name = "Enable segmented caching"
  103. content = local.vcl_segmented_caching
  104. type = "recv"
  105. priority = 100
  106. }
  107. snippet {
  108. name = "Recv Various Fixups"
  109. content = local.vcl_recv_various_fixups
  110. type = "recv"
  111. priority = 100
  112. }
  113. snippet {
  114. name = "Rewrite request to Exoscale"
  115. content = local.vcl_exoscale_forward
  116. type = "miss"
  117. priority = 100
  118. }
  119. snippet {
  120. name = "Remove headers from origin response"
  121. content = local.vcl_remove_response_headers
  122. type = "fetch"
  123. priority = 100
  124. }
  125. snippet {
  126. name = "Add CORS headers if necessary"
  127. content = local.vcl_cors_headers
  128. type = "deliver"
  129. priority = 100
  130. }
  131. # Additional products
  132. product_enablement {
  133. brotli_compression = var.product_enablement.brotli_compression
  134. domain_inspector = var.product_enablement.domain_inspector
  135. image_optimizer = var.product_enablement.image_optimizer
  136. origin_inspector = var.product_enablement.origin_inspector
  137. websockets = var.product_enablement.websockets
  138. }
  139. }