AWSTemplateFormatVersion: 2010-09-09
Description: This CloudFormation template creates DMS instance, source and target endpoints.
Rules:
  SubnetsInVPC:
    Assertions:
      - Assert:
          'Fn::EachMemberIn':
            - 'Fn::ValueOfAll':
                - 'AWS::EC2::Subnet::Id'
                - VpcId
            - 'Fn::RefAll': 'AWS::EC2::VPC::Id'
        AssertDescription: All subnets must in the VPC
Parameters:
  SourceOracleDBPort:
    Description: Database Port for Source Oracle database
    Type: String
    Default: 1521
  DMSInstanceType:
    Description: Select the instance class required by DMS Replication Instance.
    Type: String
    Default: dms.c4.4xlarge
    AllowedValues:
      - dms.t2.medium
      - dms.t2.large
      - dms.c4.2xlarge 
      - dms.c4.4xlarge
      - dms.c4.xlarge
      - dms.r4.2xlarge
      - dms.r4.4xlarge
      - dms.r4.xlarge
    ConstraintDescription: must select a valid database instance type.
  SourceOracleDatabaseName:
    Description: The name of the source Oracle database.
    Type: String
    Default: OracleSid
  SourceOracleUserName:
    Description: The user name with the permissions required to allow data migration.
    Type: String
    Default: system
  SourceOracleDBPassword:
    Description: Source DB password
    Type: String
    NoEcho: true
    Default: welcome1
  DMSStorageSize:
    Description: Enter the storage size for DMS Instance
    Type: Number
    MinValue: 50
    MaxValue: 5000
    Default : 200
  DMSSubnet1:
    Description: 'ID of subnet 1 in Availability Zone 1 (e.g., subnet-a0246dcd)'
    Type: 'AWS::EC2::Subnet::Id'
  DMSSubnet2:
    Description: 'ID of subnet 2 in Availability Zone 2 (e.g., subnet-a0246dcd)'
    Type: 'AWS::EC2::Subnet::Id'
  VPCSecurityGroupId:
    Description: Security Group Id that will be assigned to your DMS Instance.
    Type: 'AWS::EC2::SecurityGroup::Id' 
  SourceOracleEndpointID:
    Type: String
    Description: The source database server name for your Oracle database. 
  VPCID:
    Description: Select the Virtual Private Cloud (VPC) for the replication instance.
    Type: 'AWS::EC2::VPC::Id'
  TargetRDSOracleEndpointID:
    Type: String
    Description: The RDS database endpoint identifier for Oracle.
  TargetOracleDBPort:
    Description: Database Port for Target RDS Oracle Endpoint
    Type: String
    Default: 1521
  TargetRDSOracleDatabaseName:
    Description: The name of the Target Oracle database.
    Type: String
    Default: targetrds
  TargetRDSOracleUserName:
    Description: The user name with the permissions required to allow data migration.
    Type: String
    Default: admin
  TargetRDSOracleDBPassword:
    Description: Target RDS DB password
    Type: String
    Default: welcome1
    NoEcho: true
Metadata:
  'AWS::CloudFormation::Interface':
    ParameterGroups:
      - Label:
          default: AWS DMS Instance Parameters
        Parameters:
          - DMSInstanceType
          - DMSStorageSize
      - Label:
          default: Source Oracle Database Configuration
        Parameters:
          - SourceOracleEndpointID
          - SourceOracleDatabaseName
          - SourceOracleUserName
          - SourceOracleDBPassword
          - SourceOracleDBPort
      - Label:
          default: Target RDS Oracle Database Configuration
        Parameters:
          - TargetRDSOracleEndpointID
          - TargetRDSOracleDatabaseName
          - TargetRDSOracleUserName
          - TargetRDSOracleDBPassword
          - TargetOracleDBPort
      - Label:
          default: 'VPC, Subnet and Security Group Configuration'
        Parameters:
          - VPCID
          - VPCSecurityGroupId
          - DMSSubnet1
          - DMSSubnet2
Resources:
  resReplicationSubnetGroup:
    Type: 'AWS::DMS::ReplicationSubnetGroup'
    Properties:
      ReplicationSubnetGroupIdentifier: dms-subnet-group
      ReplicationSubnetGroupDescription: DMS subnet group
      SubnetIds:
        - !Ref DMSSubnet1
        - !Ref DMSSubnet2
  resReplicationInstance:
    Type: 'AWS::DMS::ReplicationInstance'
    Properties:
      ReplicationInstanceClass: !Ref DMSInstanceType
      ReplicationInstanceIdentifier: dms-replication-instance1
      VpcSecurityGroupIds:
        - !Ref VPCSecurityGroupId
      ReplicationSubnetGroupIdentifier: !Ref resReplicationSubnetGroup
      PubliclyAccessible: 'false'
      MultiAZ: 'false'
  resSourceOracleEndpoint:
    Type: 'AWS::DMS::Endpoint'
    Properties:
      EngineName: oracle
      EndpointIdentifier:
        Fn::Join:
        - "-"
        - - ORACLE-SOURCE
          - !Select [0, !Split [".", !Ref SourceOracleDatabaseName]]
      EndpointType: source
      Username: !Ref SourceOracleUserName
      Password: !Ref SourceOracleDBPassword
      ServerName: !Ref SourceOracleEndpointID
      Port: !Ref SourceOracleDBPort
      DatabaseName: !Ref SourceOracleDatabaseName
      #ExtraConnectionAttributes: addSupplementalLogging=Y;useLogminerReader=Y
  resTargetOracleEndpoint:
    Type: 'AWS::DMS::Endpoint'
    Properties:
      EngineName: oracle
      EndpointIdentifier:
        Fn::Join:
        - "-"
        - - RDS-TARGET
          - !Select [0, !Split [".", !Ref TargetRDSOracleDatabaseName]]
      EndpointType: target
      Username: !Ref TargetRDSOracleUserName
      Password: !Ref TargetRDSOracleDBPassword
      ServerName: !Ref TargetRDSOracleEndpointID
      Port: !Ref TargetOracleDBPort
      DatabaseName: !Ref TargetRDSOracleDatabaseName
Outputs:
  DMSRepulicationInstanceARN:
    Description: Name of the DMS Replication Instance.
    Value: !Ref resReplicationInstance
  DMTargetSOracleEndpointARN:
     Description: Name of the RDS Oracle database Target Endpoint.
     Value: !Ref resTargetOracleEndpoint
  DMSSourceOracleEndpointARN:
    Description: Name of the RDS Oracle database Source Endpoint.
    Value: !Ref resSourceOracleEndpoint