AWSTemplateFormatVersion: '2010-09-09'
Description: CloudFormation template to create an RDS SQL Server database for automated deployments.
Parameters:
  SqlServerInstanceName:
    NoEcho: 'false'
    Description: RDS SQL Server Instance Name
    Default: SQLServerCFNPrimaryInstance
    Type: String
    MinLength: '1'
    MaxLength: '63'
    AllowedPattern: "[a-zA-Z][a-zA-Z0-9]*"
  DatabaseUsername:
    Description: The database admin account user name.
    Default: masteradmin
    AllowedPattern: "[a-zA-Z0-9]+"
    ConstraintDescription: must contain only alphanumeric characters.
    MaxLength: '16'
    MinLength: '1'
    Type: String
  DatabasePassword:
    AllowedPattern: "^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)"
    ConstraintDescription: Must contain only alphanumeric characters with at least one capital letter and one number.
    Description: The database admin account password.
    MaxLength: '41'
    MinLength: '8'
    NoEcho: 'true'
    Type: String
  CIDRIP:
    Type: String
    Description: IP range to allow access to the RDS SQL Server instance.
  InstanceType:
    Type: String
    Default: db.m6i.xlarge
    Description: Instance type for the RDS SQL Server instance.
  StorageType:
    Type: String
    Description: Instance storage type for the RDS SQL Server instance.
    Default: gp3
  AllocatedStorage:
    Type: String
    Description: Instance allocated storage type for the RDS SQL Server instance.
    Default: 20
Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
    - Label:
        default: SQL Instance name, master username and password
      Parameters:
        - SqlServerInstanceName
        - DatabaseUsername
        - DatabasePassword
      ParameterLabels:
        SqlServerInstanceName:
            default: Instance name
        DatabaseUsername:
            default: Master user name
        DatabasePassword:
            default: Password
Resources:
  SQLServerSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: SQL Server Security Group
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: '1433'
          ToPort: '1433'
          CidrIp: !Ref CIDRIP
  PrimaryDBInstance:
    Type: "AWS::RDS::DBInstance"
    Properties:
      AllocatedStorage: !Ref AllocatedStorage
      DBInstanceClass: !Ref InstanceType
      Engine: sqlserver-ee
      EngineVersion: "15.00.4236.7.v1"
      DBInstanceIdentifier:
        Ref: SqlServerInstanceName
      LicenseModel: license-included
      MultiAZ: true
      PubliclyAccessible: false
      BackupRetentionPeriod: '7'
      StorageType: !Ref StorageType
      MasterUsername:
        Ref: DatabaseUsername
      MasterUserPassword:
        Ref: DatabasePassword
      MonitoringInterval: '60'
      MonitoringRoleArn: !GetAtt SampleCloudFormationDBMonitorRole.Arn
      EnablePerformanceInsights: true
  SampleCloudFormationDBMonitorRole:
    Type: AWS::IAM::Role
    Properties:
      Path: "/"
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - monitoring.rds.amazonaws.com
            Action:
              - sts:AssumeRole
      Tags:
        - Key: "project"
          Value: "CloudFormation deployment for SQL server"









































































































