Simple serverless redirects with Terraform in AWS
It sounds simple… A website has been moved, so the old location needs to redirect to a new location. Technically, it’s not that exciting, but it still requires several properly configured components to ensure a smooth experience for the users. In the old world, you would need at least a web server. This tutorial shows you how to quickly and easily deploy this using TechNative’s Terraform URL Redirect Module in your AWS account.
What you need:
- Basic knowledge of Terraform
- An AWS account
- The old domain should be present as a Route53 zone in your AWS account.
Let’s say your website was previously running at https://first-location.amsterdam
, and now it runs at https://new-location.amsterdam
. Previous visitors may still have bookmarks to the old location, and there may be numerous links to the old location scattered around. “The internet never forgets.”
To ensure that all these old links reach your new website, we want to set up the situation as follows:
When a previous visitor enters new-location.amsterdam
in their browser, the browser will think that it should go to https://first-location.amsterdam
. The browser will perform a DNS query and be redirected to an IP address where a web server listens on port 443. Here, the browser will request the SSL certificate associated with first-location.amsterdam
. Only if this certificate is deemed valid by the browser will it accept the response from the web server. In its response, the web server instructs the browser to directly open the URL https://new-location.amsterdam/we-have-moved
. This page contains a message explaining to the visitor that the website has been relocated to this new location.
We will solve this using AWS components as follows:
A Route53 record in the zone of first-location.amsterdam
will point to a CloudFront distribution. This CF distribution will have an ACM SSL certificate attached, proving that first-location.amsterdam
is indeed controlled by the owner of this CloudFront resource. The CloudFront service will forward all http
and https
requests to an S3 bucket configured as a web server. This S3 web server will then redirect all browsers and web agents to https://first-location.amsterdam/we-have-moved
.
In summary, we will be using these AWS services:
- Route53
- CloudFront
- AWS Certificate Manager
- S3
I won’t go into detail on how to configure your AWS account with Terraform. Check out the tutorials on the Terraform website to get started. Once you have a working setup and can modify your environment with terraform apply
, create a new file in your Terraform codebase and add the code below.
module "redirect_technative_nl" {
source = "wearetechnative/module-url-redirect/aws"
version = "0.1.1"
domain = "first-location.amsterdam"
route53_zone_name = "first-location.amsterdam."
providers = {
aws.us-east-1: aws.us-east-1
}
redirect_target_url = "https://new-location.amsterdam/our-site-has-a-new-home"
}
If you deploy this Terraform code, it will take some time, especially because creating a CloudFront distribution can be time-consuming. Validating the SSL certificates also takes some time. You can repeat the above code blocks for different old domain names as many times as you want.
Thank you for reading this tutorial. If you encounter any issues, please let us know through our github ticket system.