Redirects with Cloudfront functions
Implement domain-level redirects with CloudFront using this Terraform stack, providing an efficient, secure way to handle URL redirection at the edge.

Implementing Domain-Level Redirects with AWS CloudFront and Terraform
When building modern web applications, you often need to handle domain redirects efficiently and securely. Whether you're migrating domains, implementing vanity URLs, or managing multiple environments, having a robust redirect system is crucial. In this post, we'll explore how to implement domain-level redirects using AWS CloudFront and Terraform.
Architecture Overview#
The solution uses several AWS services working together:
- CloudFront - Handles the request routing and executes the redirect logic
- ACM (AWS Certificate Manager) - Provides SSL/TLS certificates for secure communication
- Route 53 - Manages DNS records and routes traffic to CloudFront
- CloudFront Functions - Implements the redirect logic
How It Works#
- When a user visits the domain, Route 53 routes the request to CloudFront
- CloudFront executes a CloudFront Function on the viewer-request event
- The function returns a 307 redirect response to the desired destination
- The browser follows the redirect to the new location
Implementation#
Here's the complete Terraform configuration with detailed explanations:
# CloudFront Distribution
# This handles the request routing and executes the redirect logic
resource "aws_cloudfront_distribution" "redirects" {
tags = {
Name = "${local.prefix}-redirects"
Environment = terraform.workspace
}
depends_on = [aws_acm_certificate.redirect_cert]
# Origin configuration (required by CloudFront but not used for redirects)
origin {
domain_name = "example.com"
origin_id = "noop"
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "http-only"
origin_ssl_protocols = ["TLSv1", "TLSv1.1"]
}
}
enabled = true
is_ipv6_enabled = true
comment = "${local.prefix}-redirects distribution"
# Associate the SSL certificate with the distribution
aliases = [aws_acm_certificate.redirect_cert.domain_name]
viewer_certificate {
acm_certificate_arn = aws_acm_certificate.redirect_cert.arn
ssl_support_method = "sni-only"
minimum_protocol_version = "TLSv1.2_2019"
}
# Configure the default behavior to handle all requests
default_cache_behavior {
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "noop"
viewer_protocol_policy = "redirect-to-https"
min_ttl = 0
# Associate the redirect function with the distribution
function_association {
event_type = "viewer-request"
function_arn = aws_cloudfront_function.redirects.arn
}
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
compress = true
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
}
# Route 53 A record to point the domain to CloudFront
resource "aws_route53_record" "redirect" {
zone_id = aws_route53_zone.primary.zone_id
name = ""
type = "A"
alias {
name = aws_cloudfront_distribution.redirects.domain_name
zone_id = aws_cloudfront_distribution.redirects.hosted_zone_id
evaluate_target_health = false
}
}
# SSL Certificate configuration
resource "aws_acm_certificate" "redirect_cert" {
provider = aws.us-east-1 # Certificates for CloudFront must be in us-east-1
domain_name = "example.com"
validation_method = "DNS"
lifecycle {
create_before_destroy = true
}
}
# DNS records for certificate validation
resource "aws_route53_record" "redirect_cert_validation" {
for_each = {
for dvo in aws_acm_certificate.redirect_cert.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
allow_overwrite = true
name = each.value.name
records = [each.value.record]
ttl = 60
type = each.value.type
zone_id = aws_route53_zone.primary.zone_id
}
# Certificate validation
resource "aws_acm_certificate_validation" "redirect_cert_validation" {
provider = aws.us-east-1
certificate_arn = aws_acm_certificate.redirect_cert.arn
validation_record_fqdns = [for record in aws_route53_record.redirect_cert_validation : record.fqdn]
}
# CloudFront Function for implementing redirect logic
resource "aws_cloudfront_function" "redirects" {
name = "oma-domain-redirects"
runtime = "cloudfront-js-2.0"
comment = "redirects"
publish = true
code = <<EOF
function handler(event) {
// Implement a 307 temporary redirect to the target domain
var response = {
statusCode: 307,
statusDescription: "Temporary Redirect",
headers: {
location: { value: "https://stage.example.com" },
},
};
return response;
}
EOF
}Key Benefits#
- Cost-Effective: CloudFront Functions are extremely cost-effective compared to Lambda@Edge
- Low Latency: The redirect happens at the edge, providing minimal latency for users
- Secure: All traffic is encrypted using SSL/TLS certificates
- Scalable: CloudFront automatically handles scaling based on traffic
- Maintainable: Infrastructure as code makes it easy to version and maintain
Customisation Options#
The CloudFront Function can be customised to implement various redirect patterns:
- Path-based redirects
- Query parameter handling
- Custom header management
- A/B testing scenarios
- Geographic routing
Conclusion#
This Terraform configuration provides a robust foundation for implementing domain-level redirects using AWS CloudFront. The solution is scalable, secure, and cost-effective, making it suitable for both small and large-scale applications.
Remember to replace example.com with your actual domain name and adjust the CloudFront Function logic to match your specific redirect requirements.