---
AWSTemplateFormatVersion: '2010-09-09'
Description: Creates a new domain with all required DNS records for Office 365 services to work correctly.
Parameters:
  DomainName:
    Type: String
    Default: my-site.com
    Description: Specifies the name of the new domain zone you wish to create. Please note that Amazon Route 53 will not let you create a Hosted Zone with name example.com
  DomainDescription:
    Type: String
    Default: My Site
    Description: A description for the new domain zone you wish to create (note, this cannot be changed later).
  MXRecordValue:
    Type: String
    Default: my-site-com
    Description: The string provided by Microsoft for the MX record; this is normally just your domain name with all periods replaced with dashes, e.g. example.com becomes example-com
  TXTRecordValue:
    Type: String
    Default: 'ms12345678'
    Description: The unique code provided to you by Microsoft in the form ms12345678 which they use to validate that you own the domain you are trying to register.

Resources:
  HostedZone:
    Type: "AWS::Route53::HostedZone"
    Properties:
      HostedZoneConfig:
        Comment: !Ref DomainDescription
      Name: !Ref DomainName
      HostedZoneTags:
        - Key: "Name"
          Value: !Ref DomainName
  MxRecord:
    DependsOn: HostedZone
    Type: "AWS::Route53::RecordSet"
    Properties:
      HostedZoneName: !Join ['', [ !Ref DomainName, . ]]
      Name: !Ref DomainName
      Type: MX
      TTL: 3600
      ResourceRecords:
        - !Join ['', [ '0 ', !Ref MXRecordValue, '.mail.protection.outlook.com.' ]]
  TxtRecord:
    DependsOn: HostedZone
    Type: "AWS::Route53::RecordSet"
    Properties:
      HostedZoneName: !Join ['', [ !Ref DomainName, . ]]
      Name: !Ref DomainName
      Type: TXT
      TTL: 3600
      ResourceRecords:
        - !Join ['', [ '"MS=', !Ref TXTRecordValue, '"' ]]
        - '"v=spf1 include:spf.protection.outlook.com -all"'
  AutodiscoverCnameRecord:
    DependsOn: HostedZone
    Type: "AWS::Route53::RecordSet"
    Properties:
      HostedZoneName: !Join ['', [ !Ref DomainName, . ]]
      Name: !Join ['', [ autodiscover., !Ref DomainName, . ]]
      Type: CNAME
      TTL: 3600
      ResourceRecords:
        - autodiscover.outlook.com
  EnterpriseEnrollmentCnameRecord:
    DependsOn: HostedZone
    Type: "AWS::Route53::RecordSet"
    Properties:
      HostedZoneName: !Join ['', [ !Ref DomainName, . ]]
      Name: !Join ['', [ enterpriseenrollment., !Ref DomainName, . ]]
      Type: CNAME
      TTL: 3600
      ResourceRecords:
        - enterpriseenrollment.manage.microsoft.com.
  EnterpriseRegistrationCnameRecord:
    DependsOn: HostedZone
    Type: "AWS::Route53::RecordSet"
    Properties:
      HostedZoneName: !Join ['', [ !Ref DomainName, . ]]
      Name: !Join ['', [ enterpriseregistration., !Ref DomainName, . ]]
      Type: CNAME
      TTL: 3600
      ResourceRecords:
        - enterpriseregistration.windows.net.
  LyncDiscoverCnameRecord:
    DependsOn: HostedZone
    Type: "AWS::Route53::RecordSet"
    Properties:
      HostedZoneName: !Join ['', [ !Ref DomainName, . ]]
      Name: !Join ['', [ lyncdiscover., !Ref DomainName, . ]]
      Type: CNAME
      TTL: 3600
      ResourceRecords:
        - webdir.online.lync.com.
  MsoidCnameRecord:
    DependsOn: HostedZone
    Type: "AWS::Route53::RecordSet"
    Properties:
      HostedZoneName: !Join ['', [ !Ref DomainName, . ]]
      Name: !Join ['', [ msoid., !Ref DomainName, . ]]
      Type: CNAME
      TTL: 3600
      ResourceRecords:
        - clientconfig.microsoftonline-p.net.
  MsoidCnameRecord:
    DependsOn: HostedZone
    Type: "AWS::Route53::RecordSet"
    Properties:
      HostedZoneName: !Join ['', [ !Ref DomainName, . ]]
      Name: !Join ['', [ msoid., !Ref DomainName, . ]]
      Type: CNAME
      TTL: 3600
      ResourceRecords:
        - clientconfig.microsoftonline-p.net.
  SipCnameRecord:
    DependsOn: HostedZone
    Type: "AWS::Route53::RecordSet"
    Properties:
      HostedZoneName: !Join ['', [ !Ref DomainName, . ]]
      Name: !Join ['', [ sip., !Ref DomainName, . ]]
      Type: CNAME
      TTL: 3600
      ResourceRecords:
        - sipdir.online.lync.com
  SipTlsSrvRecord:
    DependsOn: HostedZone
    Type: "AWS::Route53::RecordSet"
    Properties:
      HostedZoneName: !Join ['', [ !Ref DomainName, . ]]
      Name: !Join ['', [ _sip._tls., !Ref DomainName, . ]]
      Type: SRV
      TTL: 3600
      ResourceRecords:
        - 100 1 443 sipdir.online.lync.com.
  SipTcpSrvRecord:
    DependsOn: HostedZone
    Type: "AWS::Route53::RecordSet"
    Properties:
      HostedZoneName: !Join ['', [ !Ref DomainName, . ]]
      Name: !Join ['', [ _sipfederationtls._tcp., !Ref DomainName, . ]]
      Type: SRV
      TTL: 3600
      ResourceRecords:
        - 100 1 5061 sipfed.online.lync.com.
