How to configure DNS records for a Next.js Vercel app using AWS Route53
Tags: aws, nextjs
Published: 2022-05-11 22:49:16

Configuring the proper settings in AWS Route53 can sometimes be a bit time-consuming for something that really should be simple. We recently configured our AWS Route53 hosted zone to point to our Next.js app at Vercel. The process involves several not-so-intuitive steps, so we figured that writing a basic guide might help others in the community facing a similar challenge.

Vercel is amazing, but we ran into a snag when we saw the screen below. Remedying this situation takes a few extra steps laid out below. AWS does not allow apex records (root domain) to point to Vercel.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i23s53gep13h1deeo9ij.png)

Our configuration instructions below walks you through how you can route all www and non-www traffic for both http/https to a Vercel app.

Steps:

**Step 1: Add Vercel domain**. Add your domain to Vercel (Project Settings => Domains)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y67hcqn9ffs72v2pu9mu.png)

**Step 2: Create an S3 bucket**. Next we need to create an S3 bucket in AWS that matches your domain. For example, we created avanawallet.com. It is important that it matches your domain name exactly.

After you create your bucket, go to Properties => Static website hosting and change the following settings:

- Static website hosting = enabled
- Hosting type = Redirect requests for an object
- Host name = your website, for instance, we used www.avanawallet.com
- Protocol = https

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dcox8g8st6wtvycy8i5n.png)

Once you've setup your bucket you will see a link for the bucket website. Take note and save this link - you will need it later.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cv2jv21aas61fhvkhlcq.png)

Now if you follow that link, it will redirect you to the host name you entered. In theory you could point a CNAME record to this link and all would be good... except we need to also handle https requests. So let's go ahead and figure that out in Steps 3 and 4.

**Step 3: Create SSL Certificate**. Create your SSL/TLS certificate in AWS Certificate Manager. For the domain configuration, we suggest adding two settings similar to below. This setting will cover all subdomains (including www).
- avanawallet.com
- *.avanawallet.com 

**Step 4: Create Cloudfront Distribution**. Next we need to create a Cloudfront distribution so we can add our certificate created in Step 3.

Create a distribution with the following settings:
- Origin domain: Use the URL from Step 2. It's important that you use this URL, and not the pre-populated one from S3 that Cloudfront generates.
- Viewer protocol policy: Redirect HTTP to HTTPS (or "HTTP and HTTPS" if you prefer)
- Price class: We recommend "Use all edge locations (best performance)", but any option will work. Latency times increase with other options.
- Alternate domain name (CNAME): add your domain name. For instance, we added avanawallet.com
- Custom SSL certificate: Choose the certificate you created in Step 3


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pm319ygxgnfugjlhwix2.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54lmn4k6shvd05n33fed.png)

Great, now we should have a working Cloudfront distribution. Let's move on to the DNS records, the final step.


**Step 5: Configure Route53**. 

Now we need to add two A Records. First, let's add the A Record for www. Create an "A Record" for www.avanawallet.com and point that to 76.76.21.21 (Vercel, confirm IP is still the same just to be safe before you point there).  


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/og1icvlictqevgt0o8rf.png)

Next, we need to create an A Record for the apex - point that to the Cloudfront distribution we created in Step 4. Choose "Alias => Cloudfront => [your region] => [distribution]"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aflgs727byalmwikr6yy.png)

Great, we should be all setup now. Test your setting. I recommend testing all four combinations to make sure they work:
- http://avanawallet.com
- https://avanawallet.com
- http://www.avanawallet.com
- https://www.avanawallet.com

All should route you to https://www.avanawallet.com.

Hope this brief guide helps!