dom farr
dom farr

Reputation: 4181

How to create the SAM template block for a Route53 alias record for a custom GatewayAPI domain

Creating a SAM template to creation of an API + Lambda. Simples!

Resources:
  HelloWorldApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod
      DefinitionBody:
        Fn::Transform:
          Name: AWS::Include
          Parameters:
          Location: ./api.yaml
      

Throw into this a custom domain for the gateway and map it to the stage of the API.

Resources:
  HelloWorldApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod
      DefinitionBody:
        Fn::Transform:
          Name: AWS::Include
          Parameters:
      Domain:
        DomainName:
          Fn::Sub: api-${HelloWorldApi.Stage}.custom-domain.com
        CertificateArn: arn:aws:certificate...

If I was to do this via the console, after creating the custom domain, and mapping the stage, I must configure the DNS Alias record in Route53 for API and mapping

enter image description here

My question is how to create the SAM template block for a Route53 alias record for a custom GatewayAPI domain

Upvotes: 2

Views: 792

Answers (2)

dom farr
dom farr

Reputation: 4181

Thanks to @lamanus for inspiring me to read the docs and see the wood for the trees.

The crux of the original OP was the reference to the mapped custom domain created by AWS::Serverless::Api Getting that reference is not obvious. That said, you don't need to if you create the Route53 in the AWS::Serverless::Api block like so.

HelloWorldApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod
      Domain:
        DomainName:
          Fn::Sub: api-${HelloWorldApi.Stage}.custom-domain.com
        CertificateArn: arn:cert...
        Route53:
          HostedZoneName: custom-domain.com.
          EvaluateTargetHealth: true
      DefinitionBody:
        Fn::Transform:
          Name: AWS::Include
          Parameters:
            Location: ./api.yaml

This SAM resource will create a custom domain, and mapping, and the Route53 target alias.

Upvotes: 3

Lamanus
Lamanus

Reputation: 13581

You can use the CloudFormation template to create the Route 53 Record.

To get the endpoint, you can use the Ref function.

When the logical ID of this resource is provided to the Ref intrinsic function, it returns the ID of the underlying API Gateway API.

So, it is possible to rebuild the api gateway endpoint with the region value. Join the Ref function for the api gateway with the strings, regions such as:

!Join
- ''
- - !Ref HelloWorldApi
  - .execute-api.
  - !Ref AWS::Region (or specific value)
  - .amazonaws.com

and then create a CNAME record to the Route 53 hosted zone. See the AWS docs.

Upvotes: 0

Related Questions