Consolidating Domains with CloudFront Functions

I recently consolidated my two separate blogs (ai.saurav.io and cloud.saurav.io) into a single unified home: blog.saurav.io.

While moving the markdown files was easy, the networking challenge took a bit more finesse. I needed to ensure that visitors (and search engines) visiting the old domains were automatically redirected to the new one, verifying path preservation.

Here is how I solved it using CloudFront Functions.

CloudFront Consolidation Architecture

The Architecture

Instead of maintaining separate CloudFront distributions or S3 buckets for redirection—which is the “old school” way—I pointed all domains to a single CloudFront distribution and handled the routing logic at the edge.

  1. CloudFront: Added ai.saurav.io, cloud.saurav.io, and blog.saurav.io as aliases (CNAMEs) to my main distribution.
  2. DNS: Updated Route53 to point all three domains to that distribution.
  3. Edge Logic: Attached a CloudFront Function to the Viewer Request event.

The CloudFront Function

CloudFront Functions are lightweight Javascript functions that run at AWS edge locations. They are perfect for header manipulation and URL redirects because they have extremely low latency and cost.

Here is the function code I used to force the redirect:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function handler(event) {
var request = event.request;
var host = request.headers.host.value;
var uri = request.uri;

// Check if the request is coming from one of the old domains
if (host === 'ai.saurav.io' || host === 'cloud.saurav.io') {
return {
statusCode: 301,
statusDescription: 'Moved Permanently',
headers: {
"location": { "value": "https://blog.saurav.io" + uri }
}
};
}

// Otherwise, let the request proceed to the origin (S3)
return request;
}

Why Not Just “Point” the Domains?

A common question is: “Why can’t I just add the CNAMEs to CloudFront and be done with it?”
Technically, that would serve the content. If a visitor accesses ai.saurav.io, they would see the blog. But serving content and managing identity are two different things.

Here is the critical difference between “Just Pointing” (CNAME only) vs. “Redirecting” (CloudFront Function):

Feature Edge Redirect (CloudFront Function) “Just Pointing” (No Function)
Browser URL Bar Updates to blog.saurav.io automatically. Stays on ai.saurav.io.
User Experience Visitors know they are on the new site. Visitors are confused; they see the old domain but new content.
SEO (Google) Consolidates Authority. Google transfers “link juice” from the old domain to the new one. Duplicate Content Penalty. Google sees two identical websites on two different domains, which hurts rankings for both.
Analytics Unified traffic stats under blog. Fragmented stats across ai, cloud, and blog.

Why This Approach Matches Modern Architecture

The only non-code way to achieve this would be to create three separate S3 buckets (one for content, two empty ones for redirects) and potentially separate CloudFront distributions for each.

By using a CloudFront Function, I kept the infrastructure minimal:

  • 1 S3 Bucket
  • 1 CloudFront Distribution
  • 1 Function

This approach is cleaner, easier to maintain, and ensures that my diverse technical interests in AI and Cloud are finally unified under one roof.

Built with AI

This entire migration—from identifying the conflicting aliases, writing the Python scripts, to authoring this blog post—was planned and executed using Antigravity IDE and the Google Gemini 3 Pro model. The agent figured out the complex steps, and I simply validated the plan. It turns hours of DevOps work into single commands.

AWS CDK workshop troubleshooting notes

AWS CDK workshop troubleshooting notes

CDK is a new way to manage infrastructure as code(IaC). It looked interesting but I never got to work with CDK much. I recently swithced my roles to become a software engineer at Amazon and we heavily use CDK for our CI/CD pipelines. So I need to practice and get better at using CDK. While doing this CDK pipeline workshop(link below), I ran into a bunch of issues. Here are my troubleshooting notes.

https://cdkworkshop.com/20-typescript/70-advanced-topics/200-pipelines

Issue 1: This CDK CLI is not compatible with the CDK library used by your application. Please upgrade the CLI to the latest version. (Cloud assembly schema version mismatch: Maximum schema version supported is 8.0.0, but found 9.0.0)

Uninstall the CDK version:

npm uninstall -g aws-cdk

Install specfic version which your application is using. For ex: CDK 1.158.0

npm install -g aws-cdk@1.158.0

Reference: https://stackoverflow.com/questions/66565550/how-to-solve-cdk-cli-version-mismatch


Issue 2: When trying to push code to codecommit you get this error error: src refspec main does not match any

Check the local branch name. If you’re trying to push to the main remote branch and your local branch is master you’ll run in to this error. Fix this by renaming the local branch to main and push to remote after that.

git master -m main
git push


Issue 3: TS2304: Cannot find blob

add the dom entry to the lib in tsconfig.json. Your tsconfig file should look like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"compilerOptions": {
"target":"ES2020",
"module": "commonjs",
"lib": ["es2020", "dom"],
"outDir": "dist",
"resolveJsonModule": true,
},
"exclude": [
"coverage",
"node_modules",
"dist",
"tests"
]
}

Reference: https://stackoverflow.com/a/66275649