AWSTemplateFormatVersion: '2010-09-09'
Description: Creates DMS instance and its endpoints
Parameters:
  ReplicationInstanceType:
    Type    : String
    Default : dms.r4.large
    Description: Enter the DMS instance type.
  AllocatedStorageSize:
    Type: Number
    Default : 200
    Description: Enter the storage size.
  DMSReplicationSubnetGroup:
    Type    : String
    Default : ''
    Description: Enter the DMS Subnet Group name
  DMSSecurityGroup:
    Type    : String
    Default : ''
    Description: Enter the DMS Security Group Id (sg-0b6d3f949312a5)
  SourceDatabaseName:
    Type    : String
    Default : ''
    Description: Enter the source database name
  SourceEndpoint:
    Type    : String
    Default : ''
    Description: Enter the source database endpoint
  TargetDatabaseName:
    Type    : String
    Default : ''
    Description: Enter target database name
  TargetEndpoint:
    Type    : String
    Default : ''
    Description: Enter the target database endpoint
  SourceUsername:
    Type    : String 
    Description: Enter the source database username.
  SourcePassword:
    Type    : String 
    NoEcho  : true
    Description: Enter the source database password.
  SourceDBPort:
    Type    : String
    Description: Enter the source database port.
    Default : 3306
  TargetUsername:
    Type    : String 
    Description: Enter the target database username.
  TargetPassword:
    Type    : String 
    NoEcho  : true
    Description: Enter the target database password.
  TargetDBPort:
    Type    : String
    Description: Enter the target database port.
    Default : 3306

  
    
    
    

Resources:
# Create Replication Instance 

  ReplicationInstance:
    Type: AWS::DMS::ReplicationInstance
    Properties:
      AllocatedStorage         : !Ref AllocatedStorageSize
      AllowMajorVersionUpgrade : false
      AutoMinorVersionUpgrade  : false
      PubliclyAccessible       : false
      MultiAZ                  : true
      ReplicationInstanceClass : !Ref ReplicationInstanceType
      ReplicationInstanceIdentifier: 
        !Join
          - '-'
          - - 'REP-INST'
            - !Select [0, !Split [".", !Ref SourceDatabaseName]]
            - !Select [0, !Split [".", !Ref TargetDatabaseName]]   
      ReplicationSubnetGroupIdentifier: !Ref DMSReplicationSubnetGroup
      VpcSecurityGroupIds: [ !Ref DMSSecurityGroup ]

# Create MariaDB Endpoint

  SourceDBEndpoint:
    Type: AWS::DMS::Endpoint
    Properties:
      DatabaseName: !Ref SourceDatabaseName
      EndpointIdentifier:
        Fn::Join:
        - "-"
        - - MARIA-DB-SOURCE
          - !Select [0, !Split [".", !Ref SourceDatabaseName]]
      EndpointType  : source
      EngineName    : mariadb
      Port          : !Ref SourceDBPort
      ServerName    : !Ref SourceEndpoint
      Username      : !Ref SourceUsername
      Password      : !Ref SourcePassword
      
# Create MySQL Endpoint
                
  TargetDBEndpoint:
    Type: AWS::DMS::Endpoint
    Properties:
      DatabaseName: !Ref TargetDatabaseName
      EndpointIdentifier:
        Fn::Join:
        - "-"
        - - AURORA-MYSQL-MIG-TARGET
          - !Select [0, !Split [".", !Ref TargetDatabaseName]]
      EndpointType : target
      ExtraConnectionAttributes : "initstmt=SET FOREIGN_KEY_CHECKS=0;parallelLoadThreads=1"
      EngineName   : aurora
      Port         : !Ref TargetDBPort
      ServerName   : !Ref TargetEndpoint
      Username     : !Ref TargetUsername
      Password     : !Ref TargetPassword