Sub topic 17
Serving the API from a custom domain
This chapter assumes you have deployed an HTTP API with SAM and know what a stage is. It also assumes you own a domain in Route 53, or can add DNS records elsewhere. By default your API answers at https://abc123.execute-api.us-east-1.amazonaws.com. A custom domain such as api.example.com gives clients a stable, readable address that doesn't change if you recreate the API.
The pieces
A custom domain needs four things:
- An ACM certificate for the domain, issued in the same Region as the API. HTTP APIs use regional endpoints only, so the certificate cannot come from another Region.
- A domain name resource in API Gateway that holds the certificate.
- An API mapping that connects the domain to one API and one stage.
- A DNS record that points the domain at the API Gateway regional endpoint.
Adding the domain in SAM
Request and validate the certificate first, either in the ACM console or with the CLI. Then add these parameters and resources to the template from the SAM chapter.
Parameters:
CertificateArn:
Type: String
HostedZoneId:
Type: String
Resources:
ApiDomain:
Type: AWS::ApiGatewayV2::DomainName
Properties:
DomainName: api.example.com
DomainNameConfigurations:
- CertificateArn: !Ref CertificateArn
EndpointType: REGIONAL
SecurityPolicy: TLS_1_2
ApiMapping:
Type: AWS::ApiGatewayV2::ApiMapping
Properties:
DomainName: !Ref ApiDomain
ApiId: !Ref ItemsApi
Stage: !Ref ItemsApiApiGatewayDefaultStage
ApiRecord:
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: !Ref HostedZoneId
Name: api.example.com
Type: A
AliasTarget:
DNSName: !GetAtt ApiDomain.RegionalDomainName
HostedZoneId: !GetAtt ApiDomain.RegionalHostedZoneIdSAM creates a stage named $default for an AWS::Serverless::HttpApi unless you define one. Its logical ID is the API's ID followed by ApiGatewayDefaultStage. Referencing it with !Ref makes CloudFormation create the stage before the mapping.
The alias record uses the domain's regional hosted zone ID, not your own zone's ID. Pass your own zone ID only in the HostedZoneId of the record itself.
After deployment, test the new address:
curl -i https://api.example.com/items/42Mapping paths
The mapping above has no key, so the domain serves the whole API at its root. You can add ApiMappingKey to serve several APIs under one domain, for example v1 for one API and v2 for another. Clients would then call https://api.example.com/v1/items/42. API Gateway strips the mapping key before it reaches your routes, so route definitions stay unchanged.
Common errors
- The certificate request fails or the domain won't create: the certificate is in a different Region, or it is still pending DNS validation.
403 Forbiddenwith a message about a missing authentication token: the mapping is missing, or points at the wrong stage.- The domain doesn't resolve: the DNS record is missing, or DNS caches haven't expired yet.
- Browser calls fail after the move: update the
AllowOriginslist if the front end's own origin changed.
Exercise
Your certificate for api.example.com was issued in us-east-1, but the HTTP API is deployed in eu-west-1. Why does creating the domain name fail, and what is the fix?
Show answer
HTTP APIs use regional endpoints, and the ACM certificate must be in the same Region as the API. Request a new certificate for the domain in eu-west-1, validate it, and use its ARN in the domain name resource.