Skip to content

Commit ea21bbf

Browse files
committed
fix: add 404 default behaviour is not default origin specified
1 parent 9f169e3 commit ea21bbf

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

cloudfront/module/main.tf

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ resource "aws_lambda_function" "origin_request" {
187187
}
188188
}
189189

190+
191+
190192
resource "aws_lambda_permission" "allow_cloudfront_origin_request" {
191193
count = length(local.lambda_origins) > 0 ? 1 : 0
192194
region = "us-east-1"
@@ -435,6 +437,24 @@ resource "aws_cloudfront_distribution" "distribution" {
435437
}
436438
}
437439

440+
dynamic "origin" {
441+
for_each = aws_lambda_function_url.default_origin_shim
442+
443+
content {
444+
domain_name = split("/", origin.value.function_url)[2]
445+
origin_id = "default-lambda-shim"
446+
origin_access_control_id = aws_cloudfront_origin_access_control.lambda_oac[0].id
447+
448+
custom_origin_config {
449+
origin_read_timeout = 30
450+
origin_protocol_policy = "https-only"
451+
origin_ssl_protocols = ["TLSv1.2", "SSLv3"]
452+
http_port = 80
453+
https_port = 443
454+
}
455+
}
456+
}
457+
438458
dynamic "origin" {
439459
for_each = local.vpc_origins
440460

@@ -560,7 +580,7 @@ resource "aws_cloudfront_distribution" "distribution" {
560580
default_cache_behavior {
561581
allowed_methods = ["GET", "HEAD", "OPTIONS", "PUT", "POST", "PATCH", "DELETE"]
562582
cached_methods = ["GET", "HEAD", "OPTIONS"]
563-
target_origin_id = keys(local.default_origin)[0]
583+
target_origin_id = keys(local.actual_default_origin)[0]
564584
viewer_protocol_policy = "redirect-to-https"
565585

566586
# Add Lambda@Edge for auth preservation and webhook signing (only if not a Lambda origin and Lambda@Edge exists)

cloudfront/module/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "domain_name" {
2+
value = aws_cloudfront_distribution.distribution.domain_name
3+
description = "The domain name of the cloudfront distribution."
4+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
exports.handler = async () => {
4+
return {
5+
statusCode: 404,
6+
headers: {
7+
'Content-Type': 'text/plain',
8+
'Cache-Control': 'no-cache, no-store, must-revalidate'
9+
},
10+
body: '404 - Not Found'
11+
};
12+
};

cloudfront/module/shim.tf

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
data "archive_file" "default_route_lambda" {
2+
type = "zip"
3+
output_path = "${path.module}/default-route.zip"
4+
5+
source {
6+
content = file("${path.module}/scripts/default-route.js")
7+
filename = "index.js"
8+
}
9+
}
10+
11+
resource "aws_lambda_function" "default_origin_shim" {
12+
count = length(local.default_origin) < 1 ? 1 : 0
13+
filename = data.archive_file.default_route_lambda.output_path
14+
function_name = "${var.suga.stack_id}-cloudfront-default-origin-shim"
15+
role = aws_iam_role.lambda_edge_origin_request[0].arn
16+
handler = "index.handler"
17+
source_code_hash = data.archive_file.default_route_lambda.output_base64sha256
18+
runtime = "nodejs22.x"
19+
timeout = 5
20+
memory_size = 128
21+
publish = true
22+
}
23+
24+
resource "aws_lambda_function_url" "default_origin_shim" {
25+
count = length(aws_lambda_function.default_origin_shim) > 0 ? 1 : 0
26+
function_name = aws_lambda_function.default_origin_shim[0].function_name
27+
authorization_type = "AWS_IAM"
28+
depends_on = [aws_lambda_function.default_origin_shim[0]]
29+
}
30+
31+
resource "aws_lambda_permission" "allow_cloudfront_to_execute_lambda_shim" {
32+
count = length(aws_lambda_function.default_origin_shim) > 0 ? 1 : 0
33+
function_name = aws_lambda_function.default_origin_shim[0].function_name
34+
principal = "cloudfront.amazonaws.com"
35+
action = "lambda:InvokeFunctionUrl"
36+
source_arn = aws_cloudfront_distribution.distribution.arn
37+
depends_on = [aws_lambda_function.default_origin_shim[0], aws_cloudfront_distribution.distribution]
38+
}
39+
40+
locals {
41+
actual_default_origin = length(local.default_origin) > 0 ? local.default_origin : { "default-lambda-shim" : aws_lambda_function_url.default_origin_shim[0].function_url }
42+
}

0 commit comments

Comments
 (0)