Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add AWS::Route53::RecordSet DnsRecord to a serverless Cloudfront Distribution

I found this on how to associate a route53 dns record with a S3 bucket in a serverless.yml file.

I've tried to adapt that to the case of deploying a cloudfront distrib

DnsRecord:
  Type: "AWS::Route53::RecordSet"
  Properties:
    AliasTarget:
      DNSName: <cloudfrontdistribution id>
      HostedZoneId: Z21DNDUVLTQW6Q
    HostedZoneName: ${self:custom.appFQDN}.
    Name:
      Ref: WebAppCloudFrontDistribution
    Type: 'CNAME'

but am struggling with how to get the distribution id as a ref rather than a fixed string.

How would I do this?

like image 333
bebbi Avatar asked Dec 18 '22 23:12

bebbi


2 Answers

To set up an AliasTarget, you actually just need to provide the CloudFront DNS name for the DNSName parameter, not the distribution ID. You can do this with:

!GetAtt WebAppCloudFrontDistribution.DomainName

I'm assuming that WebAppCloudFrontDistribution is the logical ID of an AWS::CloudFront::Distribution resource in your template and not a parameter. If this is actually a parameter, just set the value of the parameter to the DNS name listed for the distribution in the AWS console dashboard for CloudFront.

There are some other things you'll need to fix in your template:

  • HostedZoneName should be the name of the Route53 hosted zone, not the FQDN you want to use. Personally, I prefer to use the HostedZoneId property for AWS::Route53::RecordSet resources instead since it's clearer what the meaning of this property is, but to each their own. (Note: HostedZoneId property for the AWS::Route53::RecordSet resource should be the HostedZoneId for YOUR hosted zone, not the same value as the AliasTarget HostedZoneId.)
  • Name should be the DNS name that you want to be a CNAME for the CloudFront distribution resource.
  • I know it's a bit weird, but with alias targets, you have to set the type to either "A" (for IPv4) or "AAAA" (IPv6). I recommend doing both - you can do this by creating a duplicate of your AWS::Route53::RecordSet resource but set type to "AAAA" instead of "A".

Finally, note that in order for this to work, you will also need to make sure to add the FQDN as an alternate name for the CloudFront distribution resource - you can set this using the "Aliases" property of the "DistributionConfig" property of the distribution resource in your template, or by configuring this manually for the distribution settings in the AWS console if you're not creating the resource in this template.

like image 112
John Nicely Avatar answered May 16 '23 08:05

John Nicely


I struggled to create a AWS::Route53::RecordSet with CloudFormation producing unspecific, unhelpful error messages of the type "The resource failed to create". The key for me was to use HostedZoneId rather than HostedZoneName to specify the parent "hosted zone". This is what I ended up with:

  NaaaaaComDNSEntry: 
    Type: 'AWS::Route53::RecordSet'
    DependsOn: NaaaaaComCloudFront
    Properties: 
      AliasTarget:
        DNSName: !GetAtt NaaaaaComCloudFront.DomainName
        # For CloudFront, HostedZoneId is always Z2FDTNDATAQYW2, see:
        # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-route53-aliastarget.html#cfn-route53-aliastarget-hostedzoneid
        HostedZoneId:  Z2FDTNDATAQYW2
      # HostedZoneId is for ID for 'naaaaa.com.'; In theory its valid to use `HostedZoneName` OR `HostedZoneId`
      # but in practice the recordset always failed to create if I used `HostedZoneName`
      HostedZoneId: ZABCDEFGHIJK5M
      Name: 'www.naaaaa.com.'
      Type: 'A'
like image 26
Nick Ager Avatar answered May 16 '23 06:05

Nick Ager