Skip to content
TypeScript
9.6 KB
Raw Blame
---
title: Custom domains with Route53 + ACM
description: Serve your site or API from your own domain — DNS-validated ACM certificates, Route 53 or another DNS host through a DNS adapter, and the domain prop on Website resources and Lambda Functions.
sidebar:
  order: 6
---

A custom domain on AWS is three moving parts: a TLS certificate (ACM), DNS
records (Route 53, or another DNS host such as Cloudflare), and wiring both
into whatever serves traffic (CloudFront, a load balancer, or API Gateway).
For websites, load-balanced ECS services, and Lambda Functions, Alchemy
collapses all three into a single `domain` prop; for APIs and anything
bespoke, the same primitives — `AWS.ACM.Certificate` and
`AWS.Route53.Record` — are available directly.

## The one-prop path: `domain` on Website resources

`AWS.Website.Router`, `AWS.Website.SsrSite`, and a standalone
`AWS.Website.StaticSite` all accept the same `domain` shape:

```typescript
const router = yield* AWS.Website.Router("WebsiteRouter", {
  domain: {
    name: "app.example.com",
    hostedZoneId: "Z1234567890",
    aliases: ["www.app.example.com"],
  },
  routes: {
    "/*": site.routeTarget,
  },
});
```

With `hostedZoneId` set, Alchemy requests a DNS-validated ACM certificate
covering `name` plus every alias, upserts the validation records into your
zone, waits for issuance, attaches the certificate to the CloudFront
distribution, and creates an `A` alias record for each hostname. The
resource's `url` output becomes `https://app.example.com`. When a site is
attached to a `Router`, put `domain` on the Router — passing it to the
attached `StaticSite` as well is an error.

## Bring your own certificate

If you already have an ACM certificate, pass its ARN as `cert` and Alchemy
skips certificate creation:

```typescript
domain: {
  name: "app.example.com",
  hostedZoneId: "Z1234567890",
  cert: existingCertificateArn,
},
```

DNS records are still managed for you — only the certificate step is
replaced. The certificate must cover the domain name and every alias you
list.

## Use a domain on another DNS host

If the domain's DNS lives outside Route 53 — for example, a domain
registered with Cloudflare Registrar — pass the DNS host's
[adapter](/infrastructure-as-code/dns-adapters) as `dns`:

```typescript
const site = yield* AWS.Website.StaticSite("Site", {
  path: "./dist",
  domain: {
    name: "app.example.com",
    aliases: ["www.app.example.com"],
    dns: Cloudflare.DNS.Adapter(),
  },
});
```

The certificate is still issued by ACM, but its validation `CNAME`s are
written to the Cloudflare zone (always DNS-only), and each hostname gets a
`CNAME` to the CloudFront distribution — Cloudflare flattens it at the zone
apex. The zone is inferred from each hostname; pin it with
`Cloudflare.DNS.Adapter({ zone: "example.com" })`. The same `dns` option works
on `AWS.Website.Router` (including hostnames bound by attached sites),
`AWS.Website.SsrSite`, the `domain` of an `AWS.ECS.Service` load balancer, and
a Lambda Function's `domain`. Include `Cloudflare.providers()` alongside
`AWS.providers()` in the stack. `Hetzner.DNS.Adapter()` works the same way
for a domain on Hetzner DNS.

Records are DNS-only by default. `Cloudflare.DNS.Adapter({ proxied: true })`
puts Cloudflare's CDN in front of CloudFront — set the zone's SSL/TLS mode to
Full (strict) if you do. Validation records are left in the zone when a
certificate is deleted: ACM reuses the same `CNAME` for every certificate on
a name.

## Manage DNS elsewhere

Set `dns: false` to skip DNS entirely:

```typescript
domain: {
  name: "app.example.com",
  cert: existingCertificateArn,
  dns: false,
},
```

No records are created, so you point your DNS provider at the distribution's
domain name yourself. `dns: false` requires `cert` — without DNS access
Alchemy can't complete DNS validation for a new certificate, so you must
bring one.

## Create or reference a hosted zone

The `domain` prop and the primitives below both key off a hosted zone ID. If
the zone lives outside your stack, pass its ID as a plain string; to manage
it in the stack, use `AWS.Route53.HostedZone`:

```typescript
const zone = yield* AWS.Route53.HostedZone("Zone", {
  name: "example.com",
});
```

`zone.id` is the hosted zone ID (without the `/hostedzone/` prefix) and
`zone.nameServers` lists the authoritative name servers — set these as `NS`
records at your registrar (or parent zone) to delegate the domain. Set
`forceDestroy: true` if you want `destroy` to delete any remaining records
before deleting the zone.

## Request a DNS-validated certificate

`AWS.ACM.Certificate` requests a certificate and, when you give it a
`hostedZoneId`, handles validation end to end:

```typescript
const cert = yield* AWS.ACM.Certificate("WebsiteCertificate", {
  domainName: "app.example.com",
  subjectAlternativeNames: ["www.app.example.com"],
  hostedZoneId: zone.id,
});
```

The provider upserts the DNS validation records ACM asks for into the zone,
waits for Route 53 to propagate them, and blocks until the certificate
reaches `ISSUED` — so `cert.certificateArn` is ready to attach the moment
the effect resolves.

When the validation records live on another DNS host, request the
certificate with `dnsValidation: "external"`, publish its records, and wait
with `AWS.ACM.CertificateValidation`:

```typescript
const cert = yield* AWS.ACM.Certificate("WebsiteCertificate", {
  domainName: "app.example.com",
  dnsValidation: "external",
});
yield* Cloudflare.DNS.RecordList("WebsiteCertificateValidation", {
  records: cert.domainValidationOptions.pipe(
    Output.map(AWS.ACM.validationRecordsOf),
  ),
});
const issued = yield* AWS.ACM.CertificateValidation("WebsiteCertificateIssued", {
  certificateArn: cert.certificateArn,
});
```

`issued.certificateArn` resolves once ACM issues the certificate, so
resources that take it deploy after issuance. The `domain` props above do
this for you when `dns` names another host. Certificates are requested in `us-east-1`, the region
CloudFront requires for viewer certificates. ACM certificates are immutable:
changing the domain name, SANs, or validation settings replaces the
certificate with a new one.

## Alias a record to CloudFront

`AWS.Route53.Record` manages a single record set; an `A` record with
`aliasTarget` points a hostname at a CloudFront distribution:

```typescript
yield* AWS.Route53.Record("WebsiteAlias", {
  hostedZoneId: zone.id,
  name: "app.example.com",
  type: "A",
  aliasTarget: {
    hostedZoneId: distribution.hostedZoneId,
    dnsName: distribution.domainName,
  },
});
```

Note that `aliasTarget.hostedZoneId` is the *distribution's* hosted zone ID
(an attribute CloudFront exposes), not your own zone's. Records are written
with `UPSERT` — create and update are the same operation — and the provider
waits for the Route 53 change to reach `INSYNC` before returning, so the
record is live when the deploy finishes.

## Custom domain for a Lambda Function

Function URLs can't serve a custom hostname. Set `domain` on
`AWS.Lambda.Function` and Alchemy puts an API Gateway v2 HTTP API in front
of the Function, with a regional certificate, an API Gateway domain name per
hostname, and the DNS records:

```typescript
const api = yield* AWS.Lambda.Function("Api", {
  main: "./src/handler.ts",
  domain: "api.example.com",
});
// api.domainUrl → "https://api.example.com"
```

The HTTP API uses payload format 2.0, the same event shape as a Function
URL, so a `fetch` handler works unchanged. The object form takes `aliases`,
`hostedZoneId`, and `dns` like the Website `domain` prop.

## Custom domain for API Gateway

For a REST API, create an `AWS.ApiGateway.DomainName` with a certificate and
map your API's stage onto it with a `BasePathMapping`:

```typescript
const domain = yield* AWS.ApiGateway.DomainName("ApiDomain", {
  domainName: "api.example.com",
  regionalCertificateArn: cert.certificateArn,
  endpointConfiguration: { types: ["REGIONAL"] },
  securityPolicy: "TLS_1_2",
});

yield* AWS.ApiGateway.BasePathMapping("Root", {
  domainName: domain.domainName,
  restApiId: api.restApiId,
  stage: "prod",
});
```

One region caveat: a `REGIONAL` domain needs its certificate in the API's own
region, while `AWS.ACM.Certificate` always issues in `us-east-1`. Deploying
to `us-east-1` works as written; in any other region, pass the ARN of a
certificate that already exists there as `regionalCertificateArn`.

## Point DNS at the API

The `DomainName` resource exposes the regional endpoint to alias to:

```typescript
yield* AWS.Route53.Record("ApiAlias", {
  hostedZoneId: zone.id,
  name: "api.example.com",
  type: "A",
  aliasTarget: {
    hostedZoneId: domain.regionalHostedZoneId.as<string>(),
    dnsName: domain.regionalDomainName.as<string>(),
  },
});
```

`regionalDomainName` / `regionalHostedZoneId` are the alias target for
regional endpoints (edge-optimized domains expose
`distributionDomainName` / `distributionHostedZoneId` instead). The `.as<string>()`
narrows the attribute's type — API Gateway only populates the regional fields
for regional endpoint configurations.

## Where next

- [Websites on AWS](/aws/frontend/websites) — the `domain` prop in the context of the
  full websites surface
- [Deploy a static site](/aws/frontend/static-site) — Router composition and
  cache invalidation
- [REST API (API Gateway v1)](/aws/apis/api-gateway) — the API this
  domain fronts
- Reference: [Certificate](/providers/aws/acm/certificate),
  [Record](/providers/aws/route53/record),
  [HostedZone](/providers/aws/route53/hostedzone),
  [DomainName](/providers/aws/apigateway/domainname),
  [Cloudflare DNS Records](/providers/cloudflare/dns/records)
- [DNS Adapters](/infrastructure-as-code/dns-adapters) — publish records on
  another DNS host, or build an adapter for your own