0
0
AwsHow-ToBeginner · 4 min read

How to Point Your Domain to an AWS S3 Bucket

To point a domain to an AWS S3 bucket, first enable static website hosting on the S3 bucket and note its website endpoint URL. Then, create an alias record in Route 53 that directs your domain to the S3 website endpoint using an A or AAAA record.
📐

Syntax

To point your domain to an S3 bucket, you use DNS records in Route 53. The key parts are:

  • Bucket name: Must match your domain name exactly.
  • Static website hosting: Enabled on the S3 bucket to get a website endpoint URL.
  • Route 53 record: An A or AAAA alias record pointing to the S3 website endpoint.
terraform
resource "aws_s3_bucket" "website_bucket" {
  bucket = "example.com"
  acl    = "public-read"

  website {
    index_document = "index.html"
    error_document = "error.html"
  }
}

resource "aws_route53_record" "alias" {
  zone_id = "ZONEID"
  name    = "example.com"
  type    = "A"

  alias {
    name                   = aws_s3_bucket.website_bucket.website_endpoint
    zone_id                = "Z3AQBSTGFYJSTF"  # S3 website endpoint hosted zone ID
    evaluate_target_health = false
  }
}
💻

Example

This example shows how to create an S3 bucket configured for static website hosting and a Route 53 alias record to point your domain to the bucket's website endpoint.

bash
aws s3api create-bucket --bucket example.com --region us-east-1

aws s3 website s3://example.com/ --index-document index.html --error-document error.html

aws route53 change-resource-record-sets --hosted-zone-id ZONEID --change-batch '{
  "Changes": [{
    "Action": "UPSERT",
    "ResourceRecordSet": {
      "Name": "example.com.",
      "Type": "A",
      "AliasTarget": {
        "HostedZoneId": "Z3AQBSTGFYJSTF",
        "DNSName": "example.com.s3-website-us-east-1.amazonaws.com.",
        "EvaluateTargetHealth": false
      }
    }
  }]
}'
Output
Bucket created: example.com Static website hosting enabled on bucket example.com Route 53 alias record created for example.com pointing to S3 website endpoint
⚠️

Common Pitfalls

  • Bucket name mismatch: The S3 bucket name must exactly match the domain name.
  • Using S3 REST endpoint instead of website endpoint: Route 53 alias must point to the S3 website endpoint, not the REST API endpoint.
  • Missing static website hosting: The bucket must have static website hosting enabled.
  • Incorrect hosted zone ID: Use the correct hosted zone ID for the S3 website endpoint region.
  • Not setting public read permissions: The bucket content must be publicly readable for website hosting.
terraform
Wrong alias target example:

alias {
  name    = aws_s3_bucket.website_bucket.bucket_regional_domain_name
  zone_id = aws_s3_bucket.website_bucket.hosted_zone_id
}

Correct alias target example:

alias {
  name    = aws_s3_bucket.website_bucket.website_endpoint
  zone_id = "Z3AQBSTGFYJSTF"  # S3 website endpoint hosted zone ID
}
📊

Quick Reference

  • Bucket name = domain name (e.g., example.com)
  • Enable static website hosting on the bucket
  • Set bucket policy for public read access
  • Create Route 53 alias record pointing to S3 website endpoint
  • Use correct hosted zone ID for S3 website endpoint region

Key Takeaways

Your S3 bucket name must exactly match your domain name to serve the website.
Enable static website hosting on the S3 bucket to get the website endpoint URL.
Create a Route 53 alias record pointing your domain to the S3 website endpoint, not the REST endpoint.
Set the bucket policy to allow public read access for website content.
Use the correct hosted zone ID for the S3 website endpoint region in Route 53 alias records.