AWSTemplateFormatVersion: '2010-09-09'
Description: 'Create RDS Aurora MySQL database'
Parameters:
  CIDR:
    Description: 'Enter the CIDR IP range to access this database instance'
    Type: String
    Default: 10.0.0.0/24
  VPCID:
    Description: 'Choose the VPC details to launch the database instance. '
    #Type: String
    Type: 'AWS::EC2::VPC::Id'
  Engine:
    Description: 'Enter the Aurora Engine and version'
    Type: String
    Default: 5.7.mysql-aurora.2.03.4
  DBInstanceClass:
    Description: 'Enter the RDS instance type of database server'
    Type: String
    Default: 'db.t3.medium'
  DBName:
    Description: 'Enter the name of the database'
    Type: String
    Default: ''
  DBBackupRetentionPeriod:
    Description: 'The number of days to keep snapshots of the cluster.'
    Type: Number
    MinValue: 1
    MaxValue: 35
    Default: 7
  DBMasterUsername:
    Description: 'Enter the master username for the DB instance '
    Type: 'String'
    Default: master
  DBMasterPassword:
    Description: 'Enter the master password for the DB instance .'
    Type: String
    NoEcho: true
    Default: ''
  PreferredBackupWindow:
    Description: 'The daily time range in UTC during which you want to create automated backups.'
    Type: String
    Default: '09:54-10:24'
  PreferredMaintenanceWindow:
    Description: The weekly time range (in UTC) during which system maintenance can occur.
    Type: String
    Default: 'sat:07:00-sat:07:30'
  DBSubnetGroup:
    Description: 'Enter the DB Subnet Group from your desired VPC where instance is being lauched'
    Type : String
  
Mappings:
  EngineMap:
    aurora:
      Engine: aurora
      EngineVersion: '5.6.10a'
      Port: 3306
      ClusterParameterGroupFamily: 'aurora5.6'
      ParameterGroupFamily: 'aurora5.6'
    '5.6.mysql-aurora.1.19.1':
      Engine: aurora
      EngineVersion: '5.6.mysql_aurora.1.19.1'
      Port: 3306
      ClusterParameterGroupFamily: 'aurora5.6'
      ParameterGroupFamily: 'aurora5.6'
    'aurora-mysql':
      Engine: 'aurora-mysql'
      EngineVersion: '5.7.12'
      Port: 3306
      ClusterParameterGroupFamily: 'aurora-mysql5.7'
      ParameterGroupFamily: 'aurora-mysql5.7'
    '5.7.mysql-aurora.2.03.4':
      Engine: 'aurora-mysql'
      EngineVersion: '5.7.mysql_aurora.2.03.4'
      Port: 3306
      ClusterParameterGroupFamily: 'aurora-mysql5.7'
      ParameterGroupFamily: 'aurora-mysql5.7'
    '5.7.mysql-aurora.2.04.3':
      Engine: 'aurora-mysql'
      EngineVersion: '5.7.mysql_aurora.2.04.3'
      Port: 3306
      ClusterParameterGroupFamily: 'aurora-mysql5.7'
      ParameterGroupFamily: 'aurora-mysql5.7'
Conditions:
  HasEngineMySQL: !Or [!Equals [!FindInMap [EngineMap, !Ref Engine, Engine], 'aurora'], !Equals [!FindInMap [EngineMap, !Ref Engine, Engine], 'aurora-mysql']]

Resources:
  
  DBSecurityGroup:
    Type: "AWS::EC2::SecurityGroup"
    Properties:
      GroupDescription: DB Security group to access MySQL database
      VpcId: !Ref VPCID
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: !FindInMap [EngineMap, !Ref Engine, Port]
          ToPort: !FindInMap [EngineMap, !Ref Engine, Port]
          CidrIp: !Ref CIDR
  DBClusterParameterGroup:
    Type: 'AWS::RDS::DBClusterParameterGroup'
    Properties:
      Description: !Ref 'AWS::StackName'
      Family: !FindInMap [EngineMap, !Ref Engine, ClusterParameterGroupFamily]
      Parameters: !If
      - HasEngineMySQL
      - character_set_client: utf8
        character_set_connection: utf8
        character_set_database: utf8
        character_set_filesystem: utf8
        character_set_results: utf8
        character_set_server: utf8
        collation_connection: utf8_general_ci
        collation_server: utf8_general_ci
      - client_encoding: 'UTF8'
  DBCluster:
    DeletionPolicy: Snapshot # default
    UpdateReplacePolicy: Snapshot
    Type: 'AWS::RDS::DBCluster'
    Properties:
      BackupRetentionPeriod: !Ref DBBackupRetentionPeriod
      DatabaseName: !Ref DBName
      DBClusterParameterGroupName: !Ref DBClusterParameterGroup
      DBSubnetGroupName: !Ref DBSubnetGroup
      Engine: !FindInMap [EngineMap, !Ref Engine, Engine]
      EngineMode: provisioned
      EngineVersion: !FindInMap [EngineMap, !Ref Engine, EngineVersion]
      MasterUsername: !Ref DBMasterUsername
      MasterUserPassword: !Ref DBMasterPassword
      Port: !FindInMap [EngineMap, !Ref Engine, Port]
      PreferredBackupWindow: !Ref PreferredBackupWindow
      PreferredMaintenanceWindow: !Ref PreferredMaintenanceWindow
      VpcSecurityGroupIds:
      - !Ref DBSecurityGroup
  DBParameterGroup:
    Type: 'AWS::RDS::DBParameterGroup'
    Properties:
      Description: !Ref 'AWS::StackName'
      Family: !FindInMap [EngineMap, !Ref Engine, ParameterGroupFamily]
  DBInstanceA:
    Type: 'AWS::RDS::DBInstance'
    Properties:
      AllowMajorVersionUpgrade: false
      AutoMinorVersionUpgrade: true
      CopyTagsToSnapshot: true
      DBClusterIdentifier: !Ref DBCluster
      DBInstanceClass: !Ref DBInstanceClass
      DBParameterGroupName: !Ref DBParameterGroup
      DBSubnetGroupName: !Ref DBSubnetGroup
      Engine: !FindInMap [EngineMap, !Ref Engine, Engine]

Outputs:
  TemplateID:
    Description: 'cloudonaut.io template id.'
    Value: 'state/rds-aurora'
  TemplateVersion:
    Description: 'cloudonaut.io template version.'
    Value: '__VERSION__'
  StackName:
    Description: 'Stack name.'
    Value: !Sub '${AWS::StackName}'
  ClusterName:
    Description: 'The name of the cluster.'
    Value: !Ref DBCluster
    Export:
      Name: !Sub '${AWS::StackName}-ClusterName'