Set up a custom domain with an ACM certificate and Route 53
Get a free certificate that renews itself for your apex domain and www, and point both names at your AWS endpoint. Visitors reach your product on your own domain over HTTPS.
Browser
Route 53
ACM certificate
Your endpoint
How it works
What a custom domain needs
Two things have to be true before example.com shows your product over HTTPS. The name has to resolve to your AWS endpoint, and that endpoint has to present a certificate for the name. Doing either by hand in the console works once and then gets forgotten until the certificate expires.
Request one certificate for both names
The certificate covers the apex and www together, so both work and neither shows a browser warning. Public certificates from ACM are free. The mistake people make is the region: a certificate for CloudFront must be in us-east-1, and a certificate for a load balancer must be in the load balancer's region.
Prove you own the domain
ACM asks for a CNAME record for each name and issues the certificate once it can see them. The configuration writes those records to your hosted zone and waits. Leave the records in place, because ACM uses the same records to renew the certificate each year.
Point the names at your endpoint
Alias records send the apex and www to your load balancer or distribution. An alias is used because DNS does not allow a CNAME on the apex. Turn on create_ipv6_records only if the endpoint has IPv6 addresses.
Attach the certificate
Use the certificate_arn output on the load balancer HTTPS listener or on the CloudFront distribution, and add both names as aliases there. The DNS records alone are not enough: the endpoint has to be told which names it serves.
What you set
domain_name
Apex domain, without www For example example.com
zone_id
Route 53 hosted zone for the domain For example Z0123456789ABCDEFGHIJ
alias_target_dns_name
DNS name of the endpoint to point at For example d111111abcdef8.cloudfront.net
alias_target_zone_id
Hosted zone ID of that endpoint For example Z2FDTNDATAQYW2
region
Region for the certificate For example us-east-1
What it costs to run
About $0.50 a month for the hosted zone. Public ACM certificates are free and alias queries to AWS endpoints are not charged. The domain registration itself is billed yearly and is separate. Estimate.
The configuration
# A custom domain with HTTPS: ACM certificate plus Route 53 records.
#
# What this creates:
# - one ACM certificate that covers the apex (example.com) and www
# - the DNS records that prove to ACM you own the domain
# - alias records that point the apex and www at your load balancer,
# CloudFront distribution or other AWS endpoint
terraform {
required_version = ">= 1.6.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
# The certificate is created in this region. Use the region of your load
# balancer, or us-east-1 if the certificate is for CloudFront.
provider "aws" {
region = var.region
}
variable "region" {
description = "Region for the certificate. Must be us-east-1 for CloudFront, otherwise the region of the load balancer."
type = string
default = "us-east-1"
}
variable "domain_name" {
description = "Apex domain, without www, for example example.com."
type = string
}
variable "zone_id" {
description = "ID of the Route 53 hosted zone for domain_name."
type = string
}
variable "alias_target_dns_name" {
description = "DNS name of the AWS endpoint to point at, for example a load balancer or CloudFront domain name."
type = string
}
variable "alias_target_zone_id" {
description = "Hosted zone ID of that endpoint. Load balancers and distributions expose it as an attribute. For CloudFront it is always Z2FDTNDATAQYW2."
type = string
}
variable "create_ipv6_records" {
description = "Also create AAAA records. Turn on when the endpoint has IPv6, as CloudFront and dual stack load balancers do."
type = bool
default = false
}
locals {
hostnames = toset([var.domain_name, "www.${var.domain_name}"])
}
# --- Certificate -------------------------------------------------------------
resource "aws_acm_certificate" "main" {
domain_name = var.domain_name
subject_alternative_names = ["www.${var.domain_name}"]
validation_method = "DNS"
# A replacement certificate is issued before the old one is removed, so
# whatever uses it is never left without one.
lifecycle {
create_before_destroy = true
}
}
# ACM asks for one CNAME per name on the certificate. As long as these
# records stay in the zone, ACM renews the certificate without any action
# from you.
resource "aws_route53_record" "validation" {
for_each = {
for option in aws_acm_certificate.main.domain_validation_options : option.domain_name => {
name = option.resource_record_name
type = option.resource_record_type
record = option.resource_record_value
}
}
zone_id = var.zone_id
name = each.value.name
type = each.value.type
records = [each.value.record]
ttl = 60
allow_overwrite = true
}
# Waits until the certificate is issued, so anything that uses the output
# below gets a certificate that works.
resource "aws_acm_certificate_validation" "main" {
certificate_arn = aws_acm_certificate.main.arn
validation_record_fqdns = [for record in aws_route53_record.validation : record.fqdn]
}
# --- Records for the apex and www ----------------------------------------------
# Alias records, not CNAMEs: DNS does not allow a CNAME on the apex, and
# Route 53 does not charge for alias queries to AWS endpoints.
resource "aws_route53_record" "a" {
for_each = local.hostnames
zone_id = var.zone_id
name = each.value
type = "A"
alias {
name = var.alias_target_dns_name
zone_id = var.alias_target_zone_id
evaluate_target_health = false
}
}
resource "aws_route53_record" "aaaa" {
for_each = var.create_ipv6_records ? local.hostnames : toset([])
zone_id = var.zone_id
name = each.value
type = "AAAA"
alias {
name = var.alias_target_dns_name
zone_id = var.alias_target_zone_id
evaluate_target_health = false
}
}
output "certificate_arn" {
description = "Validated certificate. Attach it to the load balancer listener or CloudFront distribution."
value = aws_acm_certificate_validation.main.certificate_arn
}
output "hostnames" {
description = "Names that now resolve to the endpoint."
value = sort(tolist(local.hostnames))
}
Works with OpenTofu 1.6 or later, and with Terraform. Read it before you apply it: it creates real resources in your account.