How to do a (serverless) web redirection in AWS Route53?

Curious software engineer
Search for a command to run...

Curious software engineer
No comments yet. Be the first to comment.
In this series, I will treat subjects related to software development, software engineering, and related career.
In July 2021, I was happily introducing in an article the v0.0.1 of my hobby project: utile.space, a Swiss army knife services website. https://sonny.alvesdi.as/building-my-personal-swiss-army-knife-services-part-2 At the end of the blog, I was pro...
It’s the summer holidays, after all. So I wanted to spent some time with my 7 years old daughter to teach her programming. ⌨️ I spent some time before discussing with her some of the basics concept verbally: the different parts of a computer, and bas...

This article is a follow up on my previous one introducing the concept of SE Spectrum: https://sonny.alvesdi.as/a-better-way-to-discuss Existing solutions For long, the easiest way to run a spectrum was to use visual drawing or diagram tools like ...

Introduction to SE Spectrum

One of the greatest tool available on GitHub is Dependabot. For those not familiar with it, in short it's a bot reviewing your list of dependencies and proposing PRs to update them. https://docs.github.com/en/code-security/getting-started/dependabot-...

Photo by Harli Marten on Unsplash As long as I remember I have always been interested by skepticism. And I think one element that really resonated with me has been that conference talk by Phil Plait: https://www.youtube.com/watch?v=FrFRbGjUtJk 15 ...

Imagine you are using a DNS provider and you have a domain example.com and example.net redirecting to example.com. It's pretty easy to set up.
One day you migrate to AWS Route53 because you want geolocation, load balancing, or something of the sort. And disaster! There is no web redirection.
With this article, you will know how to solve that and set up a serverless web redirection at a low cost and a little effort.
Please find below an example of a NodeJS lambda:
export const handler = async(event) => {
const res = {
statusCode: 301,
headers: {
Location: 'https://example.com'
}
};
return res;
};
and Python
def lambda_handler(event, context):
return {
"statusCode": 301,
"headers": {
'Location': "https://example.com"
}
}
Select a REST API (**not Private **)
In the next page,
confirm REST API, not Websocket
create a New API
give it a name like web-redirection
endpoint type select Edge Optimized
In the API detail page,
Click on Actions and select Create Method
Select GET method under the path / and confirm
In the / GET method details,
Finally, publish the API by clicking on Actions > Deploy API
productionStill in the API Gateway service, click on Custom Domain Names
For each domain you want to redirect, Create a Custom Domain Name
Select Edge Optimized to match the API Gateway we created above
And select (or create) a certificate that matches the domain
Once created, setup API mapping to the API gateway we created before with no path required
Go to CloudFront and create a distribution
Do not define any alternate domain, and no certificate
For the origin enter the API gateway endpoint and specify for the origin path /prod as the stage name you defined earlier
Add a Route53 record on the domain
And point at the CloudFront from the previous section
Go to your domain example.net and validate that the redirection to example.com is working!