AWSTemplateFormatVersion: '2010-09-09'
Description: 'AWS CloudFormation Sample Template showing how to create an RDS DBInstance
  in an existing Virtual Private Cloud (VPC).
  **WARNING** This template creates an Amazon Relational Database Service database
  instance. You will be billed for the AWS resources used if you create a stack from
  this template.'
Parameters:
  VpcId:
    Type: AWS::EC2::VPC::Id
    Description: VpcId of your existing Virtual Private Cloud (VPC)
    ConstraintDescription: must be the VPC Id of an existing Virtual Private Cloud.
  Subnets:
    Type: 'List<AWS::EC2::Subnet::Id>'
    Description: The list of SubnetIds, for at least two Availability Zones in the
      region in your Virtual Private Cloud (VPC)
    ConstraintDescription: Select at least two SubnetIds that are Private
  SecurityGroupId:
    Type: 'List<AWS::EC2::SecurityGroup::Id>'
    Description: SecurityGroup Id of your EKS Worker Node
    ConstraintDescription: must be SecurityGroupId of an existing Instance
  DBName:
    Default: kubeflow
    Description: Database name for Kubeflow
    Type: String
    MinLength: '1'
    MaxLength: '64'
    AllowedPattern: "[a-zA-Z][a-zA-Z0-9]*"
    ConstraintDescription: must begin with a letter and contain only alphanumeric characters.
  DBUsername:
    Default: admin
    NoEcho: 'true'
    Description: The database admin account username
    Type: String
    MinLength: '1'
    MaxLength: '16'
    AllowedPattern: "[a-zA-Z][a-zA-Z0-9]*"
    ConstraintDescription: must begin with a letter and contain only alphanumeric characters.
  DBPassword:
    Default: Kubefl0w
    NoEcho: 'true'
    Description: The database admin account password
    Type: String
    MinLength: '8'
    MaxLength: '41'
    AllowedPattern: ".*"
    ConstraintDescription: must contain only alphanumeric characters.
  DBClass:
    Default: db.m5.large
    Description: Database instance class
    Type: String
    AllowedValues:
    - db.m5.large
    - db.m5.xlarge
    - db.m5.2xlarge
    - db.m5.4xlarge
    - db.m5.12xlarge
    ConstraintDescription: must select a valid database instance type.
  DBAllocatedStorage:
    Default: '20'
    Description: The size of the database (Gb)
    Type: Number
    MinValue: '20'
    MaxValue: '65536'
    ConstraintDescription: must be between 20 and 65536Gb.
  MultiAZ:
    Description: Multi-AZ master database
    Type: String
    Default: 'false'
    AllowedValues: ['true', 'false']
    ConstraintDescription: must be true or false.
Resources:
  MyDBSubnetGroup:
    Type: AWS::RDS::DBSubnetGroup
    Properties:
      DBSubnetGroupDescription: Subnets available for the RDS DB Instance
      SubnetIds:
        Ref: Subnets
  myVPCSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Security group for RDS DB Instance.
      VpcId:
        Ref: VpcId
  MyDB:
    Type: AWS::RDS::DBInstance
    Properties:
      DBName:
        Ref: DBName
      AllocatedStorage:
        Ref: DBAllocatedStorage
      DBInstanceClass:
        Ref: DBClass
      Engine: MySQL
      EngineVersion: '8.0.17'
      MultiAZ:
        Ref: MultiAZ
      MasterUsername:
        Ref: DBUsername
      MasterUserPassword:
        Ref: DBPassword
      DBSubnetGroupName:
        Ref: MyDBSubnetGroup
      VPCSecurityGroups:
        Ref: SecurityGroupId
    DeletionPolicy: Snapshot
Outputs:
  RDSEndpoint:
    Description: RDS Endpoint
    Value:
      Fn::GetAtt:
        - MyDB
        - Endpoint.Address
  JDBCConnectionString:
    Description: JDBC connection string for database
    Value:
      Fn::Join:
      - ''
      - - jdbc:mysql://
        - Fn::GetAtt:
          - MyDB
          - Endpoint.Address
        - ":"
        - Fn::GetAtt:
          - MyDB
          - Endpoint.Port
        - "/"
        - Ref: DBName
