# *
# * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# * SPDX-License-Identifier: MIT-0
# *
# * Permission is hereby granted, free of charge, to any person obtaining a copy of this
# * software and associated documentation files (the "Software"), to deal in the Software
# * without restriction, including without limitation the rights to use, copy, modify,
# * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# * permit persons to whom the Software is furnished to do so.
# *
# * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# *
AWSTemplateFormatVersion: '2010-09-09'
Metadata: 
  License: Apache-2.0
Description: 'AWS CloudFormation Sample Template with RDS Custom Parameter group, RDS Custom Options Group and VPC Security Groups'
Parameters:
  VPCID:
    Type: AWS::EC2::VPC::Id
    Description: The VPC ID where the RDS database instance will be hosted
Resources:
# RDS Oracle Custom parameter group with processes and db_32k_cache_size parameters
  MyRDSParamGroupOracle:
    Type: AWS::RDS::DBParameterGroup
    Properties:
      Family: oracle-ee-19
      Description: CloudFormation Sample Database Parameter Group
      DBParameterGroupName: RDSDBParameterGroupOracle
      Parameters:
        processes: '500'
        db_32k_cache_size: '33554432'
# RDS Security group for EC2 and RDS DB Connectivity
  DBEC2SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Open database for access
      GroupName: RDSDBSecGrpOracle
      VpcId: !Ref VPCID
      SecurityGroupIngress:
      - IpProtocol: tcp
        FromPort: '1521'
        ToPort: '1521'
        SourceSecurityGroupId: !Ref 'WebServerSecurityGroup'
        Description: Allow access from WebServerSecurityGroup
      SecurityGroupEgress:
      - IpProtocol: tcp
        FromPort: 0
        ToPort: 65535
        DestinationSecurityGroupId: !Ref 'WebServerSecurityGroup'
        Description: Allow outbound traffic to WebServerSecurityGroup  
# Options Group with custom option S3_INTEGRATION and OLS
  OracleOptionGroup:
    Type: AWS::RDS::OptionGroup
    Properties: 
        OptionGroupDescription: "Oracle Options Grop"
        OptionGroupName: RDSOracleOpGrp
        EngineName: oracle-ee
        MajorEngineVersion: "19"
        OptionConfigurations:
          -
            OptionName: S3_INTEGRATION
          -
            OptionName: OLS
# Security Group for EC2 instance SSH Connectivity
  WebServerSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Enable HTTP access via port 80 and SSH access
      GroupName: AWSEC2SecurityGroup
      VpcId: !Ref VPCID
      SecurityGroupIngress:
      - IpProtocol: tcp
        FromPort: 80
        ToPort: 80
        CidrIp: 18.206.107.24/29 # EC2_INSTANCE_CONNECT "ip_prefix": "18.206.107.24/29" for  "region": "us-east-1". Refer to https://ip-ranges.amazonaws.com/ip-ranges.json for ip_prefix for other regions
        Description: EC2_INSTANCE_CONNECT ip_prefix 18.206.107.24/29 for region us-east-1
      - IpProtocol: tcp
        FromPort: 22
        ToPort: 22
        CidrIp: 18.206.107.24/29 # EC2_INSTANCE_CONNECT "ip_prefix": "18.206.107.24/29" for  "region": "us-east-1". Refer to https://ip-ranges.amazonaws.com/ip-ranges.json for ip_prefix for other regions
        Description: EC2_INSTANCE_CONNECT ip_prefix 18.206.107.24/29 for region us-east-1
# EC2 Security group Egress traffic rule for RDS DB Connectivity
  InstanceSecurityGroupEgress:
    Type: AWS::EC2::SecurityGroupEgress
    Properties:
      Description: Security Group Rule between EC2 and RDS Security groups 
      GroupId: !Ref WebServerSecurityGroup
      IpProtocol: tcp 
      FromPort: 1521 
      ToPort: 1521
      DestinationSecurityGroupId: !Ref 'DBEC2SecurityGroup'