FIELD NOTE ยท TA-95 REGISTER

How This Site Runs on a Private S3 Bucket

The S3, CloudFront, Terraform, and GitHub Actions path that publishes this site without a public origin or stored AWS credentials.

S3 website hosting uses a public website endpoint. I wanted a private origin, so this site uses the S3 REST endpoint behind CloudFront.

CloudFront reads the bucket through Origin Access Control (OAC). GitHub Actions deploys with short-lived AWS credentials. Terraform defines the bucket, distribution, IAM role, logs, and alarms.

The result is more infrastructure than Amplify Hosting, but every authorization and caching rule is visible in the repository.

Request path

Visitor requests pass through CloudFront and a CloudFront Function to private S3. GitHub Actions deploys by assuming an IAM role.

A request follows this path:

  1. DNS sends traversannan.com to CloudFront.
  2. CloudFront redirects HTTP to HTTPS.
  3. A viewer-request function redirects www and rewrites extensionless paths.
  4. CloudFront returns a cached object or signs a request to S3 through OAC.
  5. The bucket policy accepts reads from this distribution and rejects public traffic.

OAC works with the regional S3 REST endpoint, not the S3 website endpoint.

URL rewriting

Hugo writes /about/index.html, while readers use /about/ or /about. This viewer-request function rewrites the path before CloudFront checks its cache:

function handler(event) {
  var request = event.request;
  var host = request.headers.host.value;

  if (host.startsWith("www.")) {
    return {
      statusCode: 301,
      statusDescription: "Moved Permanently",
      headers: {
        location: {
          value: "https://" + host.substring(4) + request.uri
        }
      }
    };
  }

  var uri = request.uri;

  if (uri.endsWith("/")) {
    request.uri = uri + "index.html";
  } else {
    var lastSegment = uri.split("/").pop();
    if (lastSegment.indexOf(".") === -1) {
      request.uri = uri + "/index.html";
    }
  }

  return request;
}

The dot check is a heuristic. It works here because generated assets have extensions and content routes don’t. A site that permits dots in route names needs a route manifest or a different rule.

Bucket controls

All four Block Public Access settings are enabled. S3 Object Ownership is BucketOwnerEnforced, so the bucket doesn’t use ACLs.

The bucket policy grants s3:GetObject to the CloudFront service principal when the request comes from this distribution. It also denies insecure transport and uploads that request customer-provided encryption keys. This site has no reason to accept SSE-C uploads.

An anonymous request to the S3 endpoint has no authorization path. That’s the property I test. The word “private” by itself proves nothing.

Deployment credentials

The workflow doesn’t store an AWS access key. It requests an OpenID Connect token, and AWS Security Token Service exchanges that token for temporary role credentials.

The role can:

  • list the site bucket;
  • read, write, and delete site objects;
  • manage multipart uploads;
  • invalidate this CloudFront distribution.

OIDC removes a long-lived secret. Repository permissions, workflow files, third-party actions, branch controls, the IAM trust policy, and the deployment role still need review.

A push to main runs Hugo, synchronizes public/ with aws s3 sync --delete, and invalidates /*. A broad invalidation is acceptable for this small site. At higher traffic, I’d use content-addressed assets and invalidate only mutable routes.

Logs and alarms

CloudFront standard logs go to a separate S3 bucket with a 90-day lifecycle. CloudWatch alarms watch 4xx rate, 5xx rate, and cache-hit rate. The dashboard tracks requests, transfer, errors, and S3 metrics.

These tell me whether the site is up and caching. They don’t replace CloudTrail S3 data events, GuardDuty, or a central log archive for a workload that needs security monitoring.

Read-only checks

These commands check the controls above. Replace the example identifiers where needed.

export SITE_BUCKET="travers-personal-site"
export DISTRIBUTION_ID="E1234567890EXAMPLE"

aws sts get-caller-identity

aws s3api get-public-access-block \
  --bucket "$SITE_BUCKET"

aws s3api get-bucket-ownership-controls \
  --bucket "$SITE_BUCKET"

aws s3api get-bucket-policy-status \
  --bucket "$SITE_BUCKET"

aws cloudfront get-distribution \
  --id "$DISTRIBUTION_ID" \
  --query 'Distribution.Status'

curl -fsSI https://traversannan.com/about/
curl -fsSI https://www.traversannan.com/about/

I expect:

  • all four public-access settings to be true;
  • Object Ownership to be BucketOwnerEnforced;
  • S3 to report the policy as non-public;
  • CloudFront to report Deployed;
  • the apex URL to return successfully;
  • www to redirect permanently to the apex domain.

Why I keep it

Amplify can give readers the same result with less infrastructure. For a new static site, I’d test it first.

I keep this stack because it already works, costs little at this traffic level, and lets me inspect each authorization and caching decision. I know how to test and operate it. If that changes, Amplify is the first replacement I’d try.


Related: