S3 Bucket Redirect URL With Terraform

Real world example of how to create an S3 bucket with a redirect URL using Cloudfront with Terraform.

In this blog post i'll share a real world example of how to create an S3 bucket with a redirect URL using Cloudfront with Terraform.

This started at my work when we needed to create a simple redirect URL for a specific domain referring to the company's official documentation portal. Our goal was to create a simple URL that would redirect to the documentation portal for our specific team for our internal customers in the company, which would make it easier for them to access.

Our solution involves creating an S3 bucket configured to redirect requests, setting up a CloudFront distribution for global reach, responses, etc, and configuring a Route 53 DNS record to point to our CloudFront distribution.

Here's how these AWS services work together in our setup:

  • AWS S3 Bucket: Hosts a redirect rule to our desired URL.

  • AWS CloudFront: Distributes our content globally.

  • AWS Route 53: Manages DNS to make our short URL accessible.

Let's start with our example:

Setting Up the S3 Bucket for Redirection

Our first step is to create an S3 bucket that will handle the URL redirection using Terraform:

resource "aws_s3_bucket" "redirect_bucket" {
  bucket = "your-bucket-name"

  website {
    redirect_all_requests_to = "https://the-url-you-want-to-redirect-to.com"

This snippet creates an S3 bucket and sets it up to redirect all incoming requests to our specified URL. The website block is used to configure the bucket as a static website, and the redirect_all_requests_to attribute specifies the URL to redirect to.

Configuring the CloudFront Distribution

Next, we set up a CloudFront distribution to serve our content globally. Here's the example configuration:

resource "aws_cloudfront_distribution" "s3_distribution" {
  origin {
    domain_name = "${aws_s3_bucket.redirect_bucket.bucket}.s3-website.${var.region}.amazonaws.com"
    origin_id   = "S3-${aws_s3_bucket.redirect_bucket.id}"

    custom_origin_config {
      http_port              = 80
      https_port             = 443
      origin_protocol_policy = "http-only"
      origin_ssl_protocols   = ["TLSv1.2"]

  # ... Additional configuration ...

This sets up a CloudFront distribution that points to our S3 bucket. Note how we configure the custom_origin_config to ensure secure content delivery. Also, make sure that in the origin block, you specify the correct resource name for the S3 bucket (domain_name).

Setting Up Route 53 for DNS Configuration

For our short URL to be accessible, we need to configure a DNS record in Route 53:

resource "aws_route53_record" "www" {
  zone_id = data.aws_route53_zone.selected.zone_id
  name    = "your-name"
  type    = "A"

  alias {
    name                   = aws_cloudfront_distribution.s3_distribution.domain_name
    zone_id                = aws_cloudfront_distribution.s3_distribution.hosted_zone_id
    evaluate_target_health = false

This Terraform code creates an A record that points to our CloudFront distribution. Make sure to replace your-name with the desired domain name and to understand what's needed in terms or DNS record type.

Keep in mind, this is a simplified example of how to set up an S3 bucket with a redirect URL using CloudFront with Terraform. You may need to adjust/add configurations based on your specific requirements and use case.


Related Articles

Handling CI/CD in a Mono Repo With Multiple Python Packages

Read More

Lab as a Service in DAZN

Read More

Scheduling Lambdas Using AWS EventBridge and Terraform

Read More

GitHub Actions for Dynamic Cross-Platform Testing

Read More