Skip to content

Commit

Permalink
Adds preconfigured cache behavior output (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofhouse authored Jun 15, 2021
1 parent ff94359 commit aa58c6b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 31 deletions.
21 changes: 13 additions & 8 deletions examples/with-existing-cloudfront/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 24 additions & 7 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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" {
Expand Down Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions modules/cloudfront-cache/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
8 changes: 2 additions & 6 deletions modules/cloudfront-cache/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
14 changes: 12 additions & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit aa58c6b

Please sign in to comment.