Skip to content

Commit 287f712

Browse files
committed
feat: add more custom error response codes
1 parent 4572cdc commit 287f712

File tree

3 files changed

+110
-5
lines changed

3 files changed

+110
-5
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,62 @@ module "cloudfront" {
6767
- Only works with HTML files and requires proper Content-Type headers
6868
- For Angular applications, also injects `ngCspNonce` attribute on the `<app-root>` element when script nonces are enabled
6969

70+
### Custom Error Responses
71+
72+
This module supports configurable custom error responses for CloudFront distributions. This allows you to customize how different HTTP error codes are handled, such as serving custom error pages or routing all errors to your SPA's index.html file.
73+
74+
By default, the module is configured for Single Page Applications (SPAs) and will route common error codes (400, 403, 404, 500, 503) to `/index.html` with a 200 response code.
75+
76+
**Example Configuration for SPA:**
77+
```hcl
78+
module "cloudfront" {
79+
source = "..."
80+
81+
# Default configuration routes all errors to index.html for SPAs
82+
# No additional configuration needed
83+
}
84+
```
85+
86+
**Example Configuration for Custom Error Pages:**
87+
```hcl
88+
module "cloudfront" {
89+
source = "..."
90+
91+
custom_error_responses = [
92+
{
93+
error_code = 404
94+
response_code = 404
95+
response_page_path = "/error/404.html"
96+
},
97+
{
98+
error_code = 500
99+
response_code = 500
100+
response_page_path = "/error/500.html"
101+
}
102+
]
103+
}
104+
```
105+
106+
**Example Configuration for Mixed Approach:**
107+
```hcl
108+
module "cloudfront" {
109+
source = "..."
110+
111+
custom_error_responses = [
112+
{
113+
error_code = 403
114+
response_code = 200
115+
response_page_path = "/index.html"
116+
},
117+
{
118+
error_code = 404
119+
response_code = 404
120+
response_page_path = "/error/404.html"
121+
}
122+
]
123+
}
124+
```
125+
70126
```
71127
locals {
72128
default-src = join(" ", ["default-src", "'none'"])

cloudfront.tf

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,14 @@ resource "aws_cloudfront_distribution" "web_dist" {
119119
}
120120
}
121121

122-
# SPA
123-
custom_error_response {
124-
error_code = 403
125-
response_code = 200
126-
response_page_path = "/index.html"
122+
# Custom error responses
123+
dynamic "custom_error_response" {
124+
for_each = var.custom_error_responses
125+
content {
126+
error_code = custom_error_response.value.error_code
127+
response_code = custom_error_response.value.response_code
128+
response_page_path = custom_error_response.value.response_page_path
129+
}
127130
}
128131

129132
dynamic "logging_config" {
@@ -246,6 +249,16 @@ resource "aws_cloudfront_distribution" "web_redirect" {
246249
}
247250
}
248251

252+
# Custom error responses
253+
dynamic "custom_error_response" {
254+
for_each = var.custom_error_responses
255+
content {
256+
error_code = custom_error_response.value.error_code
257+
response_code = custom_error_response.value.response_code
258+
response_page_path = custom_error_response.value.response_page_path
259+
}
260+
}
261+
249262
default_cache_behavior {
250263
allowed_methods = ["GET", "HEAD", "OPTIONS"]
251264
cached_methods = ["GET", "HEAD", "OPTIONS"]

variables.tf

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,39 @@ variable "enable_compression" {
127127
default = true
128128
type = bool
129129
}
130+
131+
variable "custom_error_responses" {
132+
description = "Custom error responses for CloudFront distribution. For SPA applications, set response_code to 200 and response_page_path to /index.html"
133+
type = list(object({
134+
error_code = number
135+
response_code = number
136+
response_page_path = string
137+
}))
138+
default = [
139+
{
140+
error_code = 400
141+
response_code = 200
142+
response_page_path = "/index.html"
143+
},
144+
{
145+
error_code = 403
146+
response_code = 200
147+
response_page_path = "/index.html"
148+
},
149+
{
150+
error_code = 404
151+
response_code = 200
152+
response_page_path = "/index.html"
153+
},
154+
{
155+
error_code = 500
156+
response_code = 200
157+
response_page_path = "/index.html"
158+
},
159+
{
160+
error_code = 503
161+
response_code = 200
162+
response_page_path = "/index.html"
163+
}
164+
]
165+
}

0 commit comments

Comments
 (0)