'AWS SAM custom domain fails when using !Ref

If I'm defining a custom domain as a standalone resource that I'm referencing with !Ref my deployment fails. But if I use the same details for a nested object it works. Any idea why?

Custom Domains only works if both DomainName and CertificateArn are provided.

ApiDomain:
  Type: AWS::ApiGateway::DomainName
  Properties:
    DomainName: api.mydomain.com
    CertificateArn: "arn:aws:acm:blah-blah"
    Route53:
      HostedZoneName: "mydomain.com."

MyApi:
  Type: AWS::Serverless::Api
  Properties:
    StageName: Prod
    Cors: "'*'"
    Domain: !Ref ApiDomain

But if I nested the domain object - it magically works:

MyApi:
  Type: AWS::Serverless::Api
  Properties:
    StageName: Prod
    Cors: "'*'"
    Domain:
      DomainName: api.mydomain.com
      CertificateArn: "arn:aws:acm:blah-blah"
      Route53:
        HostedZoneName: "mydomain.com."


Solution 1:[1]

You can't use your ApiDomain this way. The Domain property of the AWS::Serverless::Api resource requires a DomainConfiguration object. ApiDomain is a DomainName, which is not the same as a DomainConfiguration object. Besides DomainName, a DomainConfiguration also creates base path mappings and Route 53 records.

If you want to define your custom domain as a standalone resource, you will have to define the base path mappings and the Route 53 record as standalone resources as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1