How to get the CloudFront distribution's domain name

aws

#1

The goal is to create a custom domain for my Serverless api: api.example.com. So my approach is to create this Route53 record:

ApiDomainRecord:
  Type: AWS::Route53::RecordSet
  Properties:
    Type: A
    Name: api.example.com
    HostedZoneId: Z2PERRPAZRTJGB
    AliasTarget:
      HostedZoneId: Z2FDTNDATAQYW2
      DNSName:
        Fn::GetAtt: [ --> what do we enter here <--, DomainName ]

But how do I provide the domain name from the CloudFront emitted by Serverless?

I am aware about the serverless-domain-manager plugin, but I don’t like how it’s not completely declarative. How It want’s me to run a command at some point. That’s why I’m turning to CloudFormation for this one.


#2

Were you able to get this figured out? If so, would you share your solution? I’m running into the same problem.


#3

Have you created the distribution in the same serverless configuration? If you have the cloudfront distribution definition in your template such as

  MyCloudfrontDistribution:
    Type: "AWS::CloudFront::Distribution"
    Properties:
      DistributionConfig:
        ...

You can use

Fn::GetAtt:MyCloudfrontDistribution
  - MyCloudfrontDistribution
  - DomainName

You refer to the resource you created for your distribution i.e. in this case, MyCloudfrontDistribution

If you didn’t create the distribution in this cloudformation (albeit within serverless) template you can output the value in your other template, then retrieve the value using Fn::ImportValue: