AWS CloudFront origin failover with custom error responses
CloudFront origin failover is often confused with custom error handling. Origin failover changes which backend receives a request; a custom error response changes what the viewer receives after the selected backend returns an error. Treating one as the other produces brittle automation and can hide a regional outage.
This walkthrough uses Terraform to deploy an origin group, publish branded error assets and verify the behaviour in a test distribution. The design keeps the active site in Sydney, a fallback in Melbourne, and treats CloudFront’s observed HTTP status as the failover signal.
Separate failover from error rendering
Configure the primary and secondary origins in one origin group. CloudFront sends normal traffic to the primary, then tries the secondary when the primary times out or returns a configured failure code such as 500, 502, 503 or 504. Uncached requests can return to the primary after it recovers.
A custom error page is the final presentation layer, not the trigger for failover. If the secondary succeeds, its content is returned instead of the error page. The custom response is used only when eligible origins cannot provide a usable response, so error assets must exist on the origin supplying the failure. Route 53 health checks can complement operations, but they do not control this per-request decision.
Design for Australian traffic
For an Australian workload, run the primary application in Sydney (ap-southeast-2) and the fallback in Melbourne (ap-southeast-4). The fallback can be a versioned static bucket configured as another origin, an Application Load Balancer or a second regional service. Keep origin domains and certificate names independent so one DNS failure does not disable both paths.
CloudFront still caches at global edges, so Australian data-residency obligations apply to origins, logs and backups rather than edge copies alone. Plan deployments around the AEST/AEDT daylight-saving change and Australian public holidays, when maintenance is most likely to overlap with local operations. If the secondary is static, publish an explicit Australian support message rather than allowing a generic global maintenance page.
Define the origin group in Terraform
Terraform makes the failover contract reviewable. The origin group deliberately omits 404, preventing a missing page from diverting an entire cache behaviour to the fallback site.
resource "aws_cloudfront_origin_group" "au_failover" {
name = "au-primary-secondary"
primary {
origin_id = "au-primary"
}
secondary {
origin_id = "au-secondary"
}
failover_criteria {
status_codes = [500, 502, 503, 504]
}
}
# Inside aws_cloudfront_distribution.site
origin_group {
origin_id = aws_cloudfront_origin_group.au_failover.id
}
The primary and secondary IDs must match the origin_id values declared by the distribution’s origin blocks. Add the appropriate S3 origin access control or custom-origin TLS settings to those blocks. Apply with a reviewed plan so a typo in an origin ID cannot silently remove resilience.
Configure custom error responses safely
Map only the statuses that need a designed page. For example, preserve 404 as 404 and point it to /errors/404.html; map 500 or 503 to an approved viewer response and a maintenance asset. Publish each referenced path on every origin capable of returning that status, or the custom page can trigger another failure.
custom_error_response {
error_code = 404
response_code = 404
response_page_path = "/errors/404.html"
error_caching_min_ttl = 60
}
custom_error_response {
error_code = 503
response_code = 200
response_page_path = "/errors/503.html"
error_caching_min_ttl = 30
}
Set a short, explicit error-cache TTL. A 30-to-60-second TTL absorbs an origin outage without leaving stale maintenance content for long, while a zero TTL increases load when the backend is already unhealthy. Custom mappings can change the status seen by clients, so alarm on origin logs and synthetic checks as well as CloudFront error metrics.
Automate deployment and rollback
Run terraform plan and terraform apply in a controlled CI/CD pipeline, ideally through AWS CodeBuild for an Australian organisation already standardising on AWS. Store state in an encrypted, access-controlled backend, require approval for changes to origin criteria, and tag the distribution with the release identifier. A pipeline step can poll CloudFront until the distribution reports Deployed.
Keep error pages with the application release artefact rather than editing them manually in S3. Upload them, invalidate changed assets and affected request paths, then promote the same versioned objects to the fallback origin. If a rollout fails, redeploy the previous artefact and Terraform state rather than reversing a partially propagated CloudFront change by hand.
Test failures without disrupting service
Exercise the design in a separate distribution before production. Make the test primary return 500 and confirm that CloudFront serves the secondary marker, then make both origins fail and verify the custom page. Restore the primary and confirm that new requests return to it. Use unique paths or disabled caching so an earlier successful response is not mistaken for correct failover.
Run a final controlled production test during an agreed low-traffic window, especially after origin or TLS changes. Purge the test path and any changed error asset, then inspect origin logs on both sides. Remember that cached error responses can remain active until their TTL expires. Schedule the window around the AEST/AEDT clock change and public holidays, and define who can stop the fault injection.
Monitor recovery and edge security
Alarm on CloudFront’s 5xx rate, origin latency, fallback responses and custom error-page delivery. A synthetic check that bypasses the cache should verify the primary independently, while another confirms the fallback marker. Track time to detection, failover and recovery; a healthy static page is continuity, not proof that the primary application has recovered.
For hybrid estates that already use Radware, the Radware application delivery material helps compare external health signals with CDN failover, but a health check does not replace CloudFront’s observed origin status. Keep WAF rules, rate limits and TLS policy aligned across both origins, and retain Australian audit logs mapped to the ACSC Essential Eight so the failover can be explained and reviewed.