AWS Builder Center

How I Host Freelance Client Sites on AWS for Almost Nothing (S3 + CloudFront + Route 53)

Send local businesses a live HTTPS sample site in minutes, and host it for almost nothing on AWS.

Architecture

Visitor ──HTTPS──► CloudFront (wildcard cert from ACM)
│
│ viewer-request CloudFront Function:
│ bakery.samples.example.com/about
│ → /bakery/about/index.html
▼
S3 bucket (private, via Origin Access Control)
├── bakery/
├── salon/
└── gym/

Route 53: *.samples.example.com ──alias──► CloudFront
The trick is one bucket, one distribution, one wildcard certificate, one wildcard DNS record. Each sample site is just a folder in the bucket. Adding a new site means uploading a folder. No new DNS records, certificates, or distributions.

Step 1: Create a private S3 bucket

Create a bucket (any name, in the region closest to you) and keep Block all public access turned on. Nothing is public. CloudFront will be the only thing allowed to read it.
Folder layout:
my-sites-bucket/
├── 404.html
├── bakery/
│ ├── index.html
│ └── assets/...
└── salon/
└── index.html

Step 2: Get a free HTTPS certificate

Request a public certificate in AWS Certificate Manager (ACM). Two details trip up almost everyone:
  1. For CloudFront, the certificate must be requested in the  us-east-1  (N. Virginia) region, no matter where your bucket is.
  1. Request a wildcard name,  *.samples.example.com , so every sample site is covered.
Choose DNS validation. ACM gives you a CNAME record to add; if your domain is in Route 53, there’s a one-click button to create it. Public ACM certificates are free and renew automatically as long as the validation record stays in place.

Step 3: Create the CloudFront distribution

Create a distribution with these settings:
  • Origin: your S3 bucket, using Origin Access Control (OAC). When prompted, let CloudFront generate the bucket policy.
  • Viewer protocol policy: Redirect HTTP to HTTPS.
  • Alternate domain name (CNAME):  *.samples.example.com 
  • Custom SSL certificate: the wildcard cert from Step 2.
  • Default root object:  index.html 
  • Cache policy: the managed CachingOptimized policy is a good default for static files.
The generated bucket policy looks like this, and it’s worth understanding what it says:
json
{
"Version": "2012-10-17",
"Statement": [{
"Sid": "AllowCloudFrontServicePrincipalReadOnly",
"Effect": "Allow",
"Principal": { "Service": "cloudfront.amazonaws.com" },
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::my-sites-bucket/*",
"Condition": {
"StringEquals": {
"AWS:SourceArn": "arn:aws:cloudfront::ACCOUNT_ID:distribution/DISTRIBUTION_ID"
}
}
}]
}
Only your specific distribution may read objects. Even if someone guesses your bucket name, they can’t get files from S3 directly.

Step 4: The routing trick (a CloudFront Function)

S3 doesn’t know that  bakery.samples.example.com  should map to the  bakery/  folder, and it doesn’t serve  index.html  for sub-paths automatically when accessed through CloudFront’s S3 REST origin. A tiny CloudFront Function fixes both.
Attach this as a viewer request function:
javascript
function handler(event) {
var request = event.request;
var host = request.headers.host.value;
var site = host.split('.')[0];

// Only allow simple folder names
if (!/^[a-z0-9-]+$/.test(site)) {
return { statusCode: 404, statusDescription: 'Not Found' };
}

var uri = request.uri;

// /about/ -> /about/index.html, /about -> /about/index.html
if (uri.endsWith('/')) {
uri += 'index.html';
} else if (uri.indexOf('.') === -1) {
uri += '/index.html';
}

request.uri = '/' + site + uri;
return request;
}
What it does:
  • Reads the subdomain ( bakery ) and treats it as the folder name
  • Rejects odd-looking names, so nobody can use the host header to probe other paths
  • Adds  index.html  for clean URLs
  • Prefixes the folder
Because viewer-request functions run before the cache lookup, each site’s files are cached separately under their rewritten paths.
Friendly 404s: with OAC and no list permission on the bucket, a missing file usually comes back as a 403. In the distribution’s Error pages settings, map 403 (and 404) to  /404.html  with a 404 response code.

Step 5: Point DNS at CloudFront

In Route 53, create a hosted zone for your domain (or for the  samples  subdomain), then add:
  • An A record (alias) for  *.samples.example.com  pointing to your CloudFront distribution
  • Optionally, an AAAA alias record for IPv6
Visit  https://bakery.samples.example.com  and the site is live.

Step 6: Deploy a new sample in one command

This is the part that makes the approach pleasant day to day:
bash
# Upload (or update) a site
aws s3 sync ./bakery-site s3://my-sites-bucket/bakery/ --delete

# Clear cached copies of that site
aws cloudfront create-invalidation \
--distribution-id YOUR_DIST_ID \
--paths "/bakery/*"
Put these two lines in a small  deploy.sh  that takes the site name as an argument, and you’ve got a new demo link in seconds. At the time of writing, CloudFront includes a monthly allowance of free invalidation paths, which is plenty for this workflow, but verify current limits.

Step 7: Don’t let samples compete with the real thing

Sample sites shouldn’t show up in Google. They’re drafts, they may use a business’s name and photos, and you don’t want them ranking before the client’s real site. Add two protections:
  1. A  robots.txt  at the bucket root with  Disallow: / . Since routing is by folder, you can include one per site.
  1. Better, a response headers policy that adds  X-Robots-Tag: noindex, nofollow , so it applies to every response automatically.
While you’re setting up the headers policy, attach CloudFront’s managed security headers policy or create a custom one with HSTS,  X-Content-Type-Options , and a referrer policy. It takes a couple of minutes and makes even a throwaway demo look professional to anyone who inspects it.

Step 8: Win the client, then hand over properly

When a business says yes, you have options, depending on how you want to work:
  • You host it for a monthly fee. Create their domain’s DNS records and add their domain to the distribution (a new certificate is needed for their domain). Since your costs per site are tiny, a small maintenance fee is mostly margin, and it’s a reasonable recurring service.
  • Give them their own AWS account or hand over the files and instructions.
  • Move it to a dedicated stack. For clients with more traffic or special requirements, use a separate bucket and distribution so one client’s changes never touch another’s.
Whichever you choose, take the sample off your wildcard domain once the real site is live.

Real costs

Take 10 sample sites of about 5 MB each, with a handful of visits per day:
Item
Rough cost
S3 storage (about 50 MB)
Fractions of a cent per month
S3 requests
Negligible
CloudFront transfer and requests
Typically within the free allowance at this scale (verify current limits)
ACM certificate
Free
CloudFront Function
Fractions of a cent at this volume
Route 53 hosted zone
A small fixed monthly fee per zone
Domain registration
Varies by TLD, billed yearly
So the realistic ongoing cost is dominated by the hosted zone and the domain, not the hosting. If you want to push it lower, you can keep DNS at your registrar (or any DNS provider) and use a wildcard CNAME to the CloudFront domain, and still validate your ACM certificate with a CNAME record. The trade-off is that you lose Route 53’s alias records, which matters only for apex domains like  example.com , not for subdomains like these.
Before you build anything, set up an AWS Budgets alert, for example at a few dollars, so any surprise reaches your inbox before it reaches your card.

Things that went wrong (or will, if you’re not careful)

1. Certificate in the wrong region. If CloudFront can’t see your certificate, it’s almost certainly not in  us-east-1 .
2. Subpages return errors. You forgot the function, or it isn’t associated with the viewer request event on the default behavior.
3. You updated a file but still see the old version. CloudFront cached it. Run the invalidation, or use hashed filenames for assets so cache busting is automatic.
4. Everything is a 403. Usually a bucket policy that doesn’t match the distribution ARN, or a file uploaded to the wrong folder. Remember the folder name must equal the subdomain.
5. A new subdomain works for you but not for the client. DNS caching. A new wildcard record resolves immediately, but local resolvers may remember earlier failed lookups for a while.
6. Uppercase letters in the folder name. Hostnames are case-insensitive but S3 keys are not. The function above only accepts lowercase, which avoids this.

Make it repeatable and safe

Once the manual version works, turn it into code:
  • Define the bucket, distribution, function, and DNS records in AWS CDK or SAM/CloudFormation, so you can rebuild the whole setup in minutes.
  • Deploy from GitHub Actions using an OIDC role instead of storing long-lived AWS access keys in your repository.
  • Give that role only the permissions it needs: write to the bucket and create invalidations on that one distribution.

Takeaways

  • A static site doesn’t need a server, a hosting plan, or a monthly bill with a big number on it.
  • One bucket, one distribution, and one wildcard cert can serve many sites by routing on the hostname.
  • Keep the bucket private, serve only through CloudFront with OAC, and add basic security headers.
  • Block search indexing on samples, and set a budget alert on day one.
  • The real value isn’t the infrastructure. It’s that you can send a prospective client a working, HTTPS-secured website tailored to their business within hours of noticing they don’t have one.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article