From e044db16ec361f187ff867d5137c5b54bf9f6a7f Mon Sep 17 00:00:00 2001 From: djpragmaticcoders <88495925+djpragmaticcoders@users.noreply.github.com> Date: Thu, 30 Jun 2022 20:03:10 +0200 Subject: [PATCH 1/5] cloudfront file --- cloudfront/cloudfront.tf | 161 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 cloudfront/cloudfront.tf diff --git a/cloudfront/cloudfront.tf b/cloudfront/cloudfront.tf new file mode 100644 index 0000000..04cd629 --- /dev/null +++ b/cloudfront/cloudfront.tf @@ -0,0 +1,161 @@ +resource "aws_cloudfront_origin_access_identity" "website" { + comment = var.domain_name +} + +resource "aws_cloudfront_distribution" "website" { + origin { + domain_name = var.domain_name + origin_id = "s3" + + s3_origin_config { + origin_access_identity = aws_cloudfront_origin_access_identity.website.cloudfront_access_identity_path + } + } + + dynamic "origin" { + for_each = var.cloudfront_origin_custom + content { + domain_name = origin.value["domain_name"] + origin_id = origin.key + custom_origin_config { + http_port = origin.value["http_port"] + https_port = origin.value["https_port"] + origin_protocol_policy = origin.value["origin_protocol_policy"] + origin_ssl_protocols = origin.value["origin_ssl_protocols"] + } + } + } + + dynamic "origin" { + for_each = var.cloudfront_origin_s3 + content { + domain_name = origin.value["domain_name"] + origin_id = origin.key + s3_origin_config { + origin_access_identity = aws_cloudfront_origin_access_identity.website.cloudfront_access_identity_path + } + } + } + + aliases = var.additional_alias !="" ? flatten([var.domain_name, var.additional_alias]) : [for a in [ + var.domain_name, + var.skip_www ? "" : format("%s%s", "www.", var.domain_name) + ] : a if a != ""] + + enabled = true + price_class = "PriceClass_100" + retain_on_delete = var.retain_on_delete + default_root_object = var.index_document + is_ipv6_enabled = "true" + + default_cache_behavior { + allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"] + cached_methods = ["GET", "HEAD"] + target_origin_id = "s3" + viewer_protocol_policy = "redirect-to-https" + min_ttl = var.min_ttl + default_ttl = var.default_ttl + max_ttl = var.max_ttl + + forwarded_values { + query_string = false + + cookies { + forward = "none" + } + } + + dynamic "lambda_function_association" { + for_each = var.lambda_viewer_request != "" ? [var.lambda_viewer_request] : [] + + content { + event_type = "viewer-request" + lambda_arn = lambda_function_association.value + } + } + } + + dynamic "ordered_cache_behavior" { + for_each = var.cloudfront_ordered_cache_behavior + content { + allowed_methods = ordered_cache_behavior.value["allowed_methods"] + cached_methods = ordered_cache_behavior.value["cached_methods"] + path_pattern = ordered_cache_behavior.key + target_origin_id = ordered_cache_behavior.value["target_origin_id"] + viewer_protocol_policy = ordered_cache_behavior.value["viewer_protocol_policy"] + forwarded_values { + query_string = ordered_cache_behavior.value["forwarded_values_query_string"] + cookies { + forward = ordered_cache_behavior.value["forwarded_values_cookies_forward"] + } + headers = ordered_cache_behavior.value["forwarded_values_headers"] + } + } + } + + viewer_certificate { + acm_certificate_arn = var.certificate_arn + ssl_support_method = "sni-only" + minimum_protocol_version = "TLSv1.2_2019" + } + + restrictions { + geo_restriction { + restriction_type = "none" + } + } + + custom_error_response { + error_caching_min_ttl = 0 + error_code = 500 + } + + custom_error_response { + error_caching_min_ttl = 0 + error_code = 501 + } + + custom_error_response { + error_caching_min_ttl = 0 + error_code = 502 + } + + custom_error_response { + error_caching_min_ttl = 0 + error_code = 503 + } + + custom_error_response { + error_caching_min_ttl = 0 + error_code = 504 + } + + custom_error_response { + error_caching_min_ttl = 0 + error_code = 400 + } + + custom_error_response { + error_caching_min_ttl = 0 + error_code = 403 + response_code = var.index_document_on_404 ? 200 : null + response_page_path = var.index_document_on_404 ? "/${var.index_document}" : null + } + + custom_error_response { + error_caching_min_ttl = 0 + error_code = 404 + response_code = var.index_document_on_404 ? 200 : null + response_page_path = var.index_document_on_404 ? "/${var.index_document}" : null + } + + custom_error_response { + error_caching_min_ttl = 0 + error_code = 405 + } + + custom_error_response { + error_caching_min_ttl = 0 + error_code = 414 + } +} From 295d1c6ac6b55db6a724069c00132103517be58e Mon Sep 17 00:00:00 2001 From: djpragmaticcoders <88495925+djpragmaticcoders@users.noreply.github.com> Date: Thu, 30 Jun 2022 20:04:05 +0200 Subject: [PATCH 2/5] Create outputs.tf --- cloudfront/outputs.tf | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 cloudfront/outputs.tf diff --git a/cloudfront/outputs.tf b/cloudfront/outputs.tf new file mode 100644 index 0000000..d66e0bf --- /dev/null +++ b/cloudfront/outputs.tf @@ -0,0 +1,19 @@ +output "cloudfront_hosted_zone_id" { + value = aws_cloudfront_distribution.website.hosted_zone_id +} + +output "cloudfront_domain_name" { + value = aws_cloudfront_distribution.website.domain_name +} + +output "url" { + value = "https://${var.domain_name}" +} + +output "domain_name" { + value = var.domain_name +} + +output "cloudfront_origin_access_identity_arn" { + value = aws_cloudfront_origin_access_identity.website.iam_arn +} From d8e1012c6cc7783b6571b1985d25d2be1425b235 Mon Sep 17 00:00:00 2001 From: djpragmaticcoders <88495925+djpragmaticcoders@users.noreply.github.com> Date: Thu, 30 Jun 2022 20:04:30 +0200 Subject: [PATCH 3/5] Create variables.tf --- cloudfront/variables.tf | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 cloudfront/variables.tf diff --git a/cloudfront/variables.tf b/cloudfront/variables.tf new file mode 100644 index 0000000..28ee0bc --- /dev/null +++ b/cloudfront/variables.tf @@ -0,0 +1,27 @@ +# CLOUDFRONT + +variable "domain_name" {} +variable "additional_alias" {default = ""} +variable "dns_zone_id" { default = "" } +variable "certificate_arn" {} +variable "skip_www" { default = false } +variable "create_dns_record" { default = true } +variable "index_document_on_404" { default = false } +variable "index_document" { default = "index.html" } +variable "min_ttl" { default = 0 } +variable "default_ttl" { default = 1800 } +variable "max_ttl" { default = 3600 } +variable "retain_on_delete" { default = false } +variable "lambda_viewer_request" { default = "" } +variable "cloudfront_ordered_cache_behavior" { + default = {} + description = "Ordered cache behavior dynamic pool" +} +variable "cloudfront_origin_custom" { + default = {} + description = "Definition of origins with custom origin config" +} +variable "cloudfront_origin_s3" { + default = {} + description = "Definition of origins based on s3 buckets" +} From 7061a6ea08c8f2f024cf08bee8fd6f52e41b9009 Mon Sep 17 00:00:00 2001 From: Arkadiusz Tomala Date: Mon, 4 Jul 2022 14:16:54 +0200 Subject: [PATCH 4/5] change varibale --- cloudfront/cloudfront.tf | 5 +++-- cloudfront/variables.tf | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/cloudfront/cloudfront.tf b/cloudfront/cloudfront.tf index 04cd629..907d91c 100644 --- a/cloudfront/cloudfront.tf +++ b/cloudfront/cloudfront.tf @@ -43,7 +43,7 @@ resource "aws_cloudfront_distribution" "website" { ] : a if a != ""] enabled = true - price_class = "PriceClass_100" + price_class = var.priceclass retain_on_delete = var.retain_on_delete default_root_object = var.index_document is_ipv6_enabled = "true" @@ -101,7 +101,8 @@ resource "aws_cloudfront_distribution" "website" { restrictions { geo_restriction { - restriction_type = "none" + restriction_type = var.restriction_type + locations = [var.locations] } } diff --git a/cloudfront/variables.tf b/cloudfront/variables.tf index 28ee0bc..a8b002a 100644 --- a/cloudfront/variables.tf +++ b/cloudfront/variables.tf @@ -25,3 +25,12 @@ variable "cloudfront_origin_s3" { default = {} description = "Definition of origins based on s3 buckets" } +variable "locations" {} +variable "restriction_type" { + type = string + default = "none" +} +variable "priceclass" { + type = string + default = "PriceClass_100" +} \ No newline at end of file From 2421454cd172916b217b9eb3298ede999f66fafa Mon Sep 17 00:00:00 2001 From: Arkadiusz Tomala Date: Mon, 4 Jul 2022 15:13:06 +0200 Subject: [PATCH 5/5] change CMS in container lambda --- cloudfront/variables.tf | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cloudfront/variables.tf b/cloudfront/variables.tf index a8b002a..3241afe 100644 --- a/cloudfront/variables.tf +++ b/cloudfront/variables.tf @@ -25,7 +25,10 @@ variable "cloudfront_origin_s3" { default = {} description = "Definition of origins based on s3 buckets" } -variable "locations" {} +variable "locations" { + type = string + default = "" +} variable "restriction_type" { type = string default = "none"