diff --git a/examples/with-existing-cloudfront/main.tf b/examples/with-existing-cloudfront/main.tf index 4547cef..dcd0531 100644 --- a/examples/with-existing-cloudfront/main.tf +++ b/examples/with-existing-cloudfront/main.tf @@ -35,16 +35,21 @@ resource "aws_cloudfront_distribution" "distribution" { is_ipv6_enabled = true comment = "next-image-optimizer-example-external-cf" - default_cache_behavior { - allowed_methods = ["GET", "HEAD"] - cached_methods = ["GET", "HEAD"] - target_origin_id = module.next_image_optimizer.cloudfront_origin_id + # This is a generic dynamic to create the default cache behavior + dynamic "default_cache_behavior" { + for_each = [module.next_image_optimizer.cloudfront_cache_behavior] - viewer_protocol_policy = "redirect-to-https" - compress = true + content { + allowed_methods = default_cache_behavior.value["allowed_methods"] + cached_methods = default_cache_behavior.value["cached_methods"] + target_origin_id = default_cache_behavior.value["target_origin_id"] + + viewer_protocol_policy = default_cache_behavior.value["viewer_protocol_policy"] + compress = default_cache_behavior.value["compress"] - origin_request_policy_id = module.next_image_optimizer.cloudfront_origin_request_policy_id - cache_policy_id = module.next_image_optimizer.cloudfront_cache_policy_id + origin_request_policy_id = default_cache_behavior.value["origin_request_policy_id"] + cache_policy_id = default_cache_behavior.value["cache_policy_id"] + } } # This is a generic dynamic to create an origin diff --git a/main.tf b/main.tf index 8a5018d..53c5bb4 100644 --- a/main.tf +++ b/main.tf @@ -87,6 +87,9 @@ module "api_gateway" { data "aws_region" "current" {} locals { + # Origin Shield + ############### + # Origin Shield mapping configuration # See: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/origin-shield.html origin_shield_region_mapping = { @@ -104,7 +107,7 @@ locals { eu-west-2 = "eu-west-2" # Europe (London) sa-east-1 = "sa-east-1" # South America (São Paulo) - # Regions where Origin Shield is NOT avaible (choose closest region) + # Regions where Origin Shield is NOT available (choose closest region) us-west-1 = "us-west-2" # US West (N. California) af-south-1 = "eu-west-1" # Africa (Cape Town) ap-east-1 = "ap-southeast-1" # Asia Pacific (Hong Kong) @@ -131,13 +134,16 @@ locals { } : {} # Query string parameters used by image optimizer - # Must be sorted to prevent unnessesary updates of the cloudFront distribution + # Must be sorted to prevent unnecessary updates of the cloudFront distribution cloudfront_allowed_query_string_keys = sort(["url", "w", "q"]) # Headers that are used by the image optimizer cloudfront_allowed_headers = sort(["accept", "referer"]) - cloudfront_origin_image_optimizer = merge( + + # CloudFront origin + ################### + cloudfront_origin = merge( { domain_name = trimprefix(module.api_gateway.apigatewayv2_api_api_endpoint, "https://") origin_id = var.cloudfront_origin_id @@ -151,6 +157,19 @@ locals { }, local.cloudfront_origin_shield_config ) + + # CloudFront cache behavior + ########################### + cloudfront_cache_behavior = { + path_pattern = "/_next/image*" + allowed_methods = ["GET", "HEAD"] + cached_methods = ["GET", "HEAD"] + target_origin_id = local.cloudfront_origin.origin_id + viewer_protocol_policy = "redirect-to-https" + compress = true + origin_request_policy_id = aws_cloudfront_origin_request_policy.this.id + cache_policy_id = aws_cloudfront_cache_policy.this.id + } } resource "random_id" "policy_name" { @@ -219,10 +238,8 @@ module "cloudfront" { cloudfront_create_distribution = var.cloudfront_create_distribution cloudfront_price_class = var.cloudfront_price_class - cloudfront_origin = local.cloudfront_origin_image_optimizer - - cloudfront_origin_request_policy_id = aws_cloudfront_origin_request_policy.this.id - cloudfront_cache_policy_id = aws_cloudfront_cache_policy.this.id + cloudfront_origin = local.cloudfront_origin + cloudfront_default_behavior = local.cloudfront_cache_behavior deployment_name = var.deployment_name tags = var.tags diff --git a/modules/cloudfront-cache/main.tf b/modules/cloudfront-cache/main.tf index 27fa0b5..9834c7a 100644 --- a/modules/cloudfront-cache/main.tf +++ b/modules/cloudfront-cache/main.tf @@ -9,16 +9,20 @@ resource "aws_cloudfront_distribution" "distribution" { comment = var.deployment_name price_class = var.cloudfront_price_class - default_cache_behavior { - allowed_methods = ["GET", "HEAD"] - cached_methods = ["GET", "HEAD"] - target_origin_id = lookup(var.cloudfront_origin, "origin_id", null) + dynamic "default_cache_behavior" { + for_each = [var.cloudfront_default_behavior] - viewer_protocol_policy = "redirect-to-https" - compress = true + content { + allowed_methods = default_cache_behavior.value["allowed_methods"] + cached_methods = default_cache_behavior.value["cached_methods"] + target_origin_id = default_cache_behavior.value["target_origin_id"] + + viewer_protocol_policy = default_cache_behavior.value["viewer_protocol_policy"] + compress = default_cache_behavior.value["compress"] - origin_request_policy_id = var.cloudfront_origin_request_policy_id - cache_policy_id = var.cloudfront_cache_policy_id + origin_request_policy_id = default_cache_behavior.value["origin_request_policy_id"] + cache_policy_id = default_cache_behavior.value["cache_policy_id"] + } } dynamic "origin" { diff --git a/modules/cloudfront-cache/variables.tf b/modules/cloudfront-cache/variables.tf index a93ed90..83b7dca 100644 --- a/modules/cloudfront-cache/variables.tf +++ b/modules/cloudfront-cache/variables.tf @@ -10,12 +10,8 @@ variable "cloudfront_origin" { type = any } -variable "cloudfront_origin_request_policy_id" { - type = string -} - -variable "cloudfront_cache_policy_id" { - type = string +variable "cloudfront_default_behavior" { + type = any } variable "deployment_name" { diff --git a/outputs.tf b/outputs.tf index 01b9217..6887eac 100644 --- a/outputs.tf +++ b/outputs.tf @@ -13,9 +13,19 @@ output "cloudfront_origin_id" { value = var.cloudfront_origin_id } +output "cloudfront_origin" { + description = "Predefined CloudFront origin. Can be used to embed the image optimizer into an existing CloudFront resource." + value = local.cloudfront_origin +} + output "cloudfront_origin_image_optimizer" { - description = "Predefined CloudFront origin of the image optimizer. Can be used to embedd the image optimizer into an existing CloudFront resource." - value = local.cloudfront_origin_image_optimizer + description = "Deprecated, please use cloudfront_origin instead." + value = local.cloudfront_origin +} + +output "cloudfront_cache_behavior" { + description = "Predefined CloudFront cache behavior. Can be used to embed the image optimizer into an existing CloudFront resource." + value = local.cloudfront_cache_behavior } output "cloudfront_origin_request_policy_id" {