Fastmail AWS Route53 with CloudFormation

Situation

You use Fastmail for your email with your custom domain name(s) and AWS Route53 to manage your DNS records. You also use CloudFormation to set up your Route53 hosted zones. How do you configure your DNS records for your email?

Good news is that Fastmail has a very helpful guide to setting up your custom domain, including specific instructions for several providers such as CloudFlare, Gandi, Namecheap, GoDaddy, Enom, etc..

But they don’t provide such detailed help with Route53 and CloudFormation.

Solution

Here’s a quick CloudFormation template that might save you some time. It includes MX, SPF and DKIM records for Fastmail and provides you with a variable named Domain so you can use it with any domain (or sub-domain) you’d like.

I reproduced the template below, but here’s a GitHub gist too.

Parameters:

  Domain:
    Description: Domain for hosted zone, e.g. example.com
    Type: String

Resources:

  WebHostedZone:
    Type: AWS::Route53::HostedZone
    Properties:
      Name: !Ref Domain

  WebMailRecordSetGroup:
    Type: AWS::Route53::RecordSetGroup
    Properties:
      HostedZoneId: !Ref WebHostedZone
      RecordSets:
        - Name: !Ref Domain
          TTL: 21600
          Type: MX
          ResourceRecords:
            - 10 in1-smtp.messagingengine.com
            - 20 in2-smtp.messagingengine.com
        - Name: !Ref Domain
          TTL: 21600
          Type: TXT
          ResourceRecords:
            - "\"v=spf1 include:spf.messagingengine.com ?all\""
        - Name: !Join ["", ["fm1._domainkey.", !Ref Domain]]
          TTL: 21600
          Type: CNAME
          ResourceRecords:
            - !Join ["", ["fm1.", !Ref Domain, ".dkim.fmhosted.com"]]
        - Name: !Join ["", ["fm2._domainkey.", !Ref Domain]]
          TTL: 21600
          Type: CNAME
          ResourceRecords:
            - !Join ["", ["fm2.", !Ref Domain, ".dkim.fmhosted.com"]]
        - Name: !Join ["", ["fm3._domainkey.", !Ref Domain]]
          TTL: 21600
          Type: CNAME
          ResourceRecords:
            - !Join ["", ["fm3.", !Ref Domain, ".dkim.fmhosted.com"]]

Outputs:
  HostedZoneId:
    Description: Hosted Zone ID
    Value: !Ref WebHostedZone
  WebMailDomain:
    Description: Mail RecordSetGroup Domain Name
    Value: !Ref WebMailRecordSetGroup