{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "DatabaseName": {
      "Description": "The name of the first database to be created when the cluster is created",
      "Type": "String",
      "Default": "dev",
      "AllowedPattern": "([a-z]|[0-9])+"
    },
    "KeyName": {
      "Description": "Name of an existing EC2 KeyPair to enable SSH access to the device simulator EC2 instance",
      "Type": "AWS::EC2::KeyPair::KeyName",
      "ConstraintDescription": "must be the name of an existing EC2 KeyPair."
    },
    "VPCCIDR": {
            "Description": "CIDR Block for the VPC you are creating.",
            "Type": "String",
            "Default": "10.0.0.0/16",
            "AllowedPattern": "[a-zA-Z0-9]+\\..+"
        },
    "VPCSUBNET": {
            "Description": "CIDR Block for the VPC you are creating.",
            "Type": "String",
            "Default": "10.0.0.0/24",
            "AllowedPattern": "[a-zA-Z0-9]+\\..+"
        },
    "VPCSUBNET2": {
            "Description": "CIDR Block for the VPC you are creating. This subnet is for DMS",
            "Type": "String",
            "Default": "10.0.1.0/24",
            "AllowedPattern": "[a-zA-Z0-9]+\\..+"
        },
    "ClusterType": {
      "Description": "The type of cluster",
      "Type": "String",
      "Default": "single-node",
      "AllowedValues": ["single-node", "multi-node"]
    },
    "NumberOfNodes": {
      "Description": "The number of compute nodes in the cluster. For multi-node clusters, the NumberOfNodes parameter must be greater than 1",
      "Type": "Number",
      "Default": "1"
    },
    "NodeType": {
      "Description": "The type of node to be provisioned",
      "Type": "String",
      "Default": "dc1.large",
      "AllowedValues": ["dc1.large"]
    },
    "MasterUsername": {
      "Description": "The user name that is associated with the master user account for the cluster that is being created",
      "Type": "String",
      "Default": "admin",
      "AllowedPattern": "([a-z])([a-z]|[0-9])*"
    },
    "MasterUserPassword": {
      "Description": "The password that is associated with the master user account for the cluster that is being created.",
      "Type": "String",
      "NoEcho": "true"
    },
    "InboundTraffic": {
      "Description": "Allow inbound traffic to the cluster from this CIDR range.",
      "Type": "String",
      "MinLength": "9",
      "MaxLength": "18",
      "Default": "0.0.0.0/0",
      "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
      "ConstraintDescription": "must be a valid CIDR range of the form x.x.x.x/x."
    },
    "PortNumber": {
      "Description": "The port number on which the cluster accepts incoming connections.",
      "Type": "Number",
      "Default": "5439"
    }
  },
  "Conditions": {
    "IsMultiNodeCluster": {
      "Fn::Equals": [{
        "Ref": "ClusterType"
      }, "multi-node"]
    }
  },
  "Resources": {
    "Ec2InstanceSecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "Enable SSH access via port 22",
        "SecurityGroupIngress": [{
          "IpProtocol": "tcp",
          "FromPort": "22",
          "ToPort": "22",
          "CidrIp": "0.0.0.0/0"
        }, {
          "IpProtocol": "tcp",
          "FromPort": "80",
          "ToPort": "80",
          "CidrIp": "0.0.0.0/0"
        }, {
          "IpProtocol": "tcp",
          "FromPort": "5439",
          "ToPort": "5439",
          "CidrIp": "0.0.0.0/0"
        },{
          "IpProtocol": "tcp",
          "FromPort": "1521",
          "ToPort": "1521",
          "CidrIp": "0.0.0.0/0"
        }],
      "VpcId": {
          "Ref": "VPC"
        }
      }
    },


    "EipAddress": {
      "Type": "AWS::EC2::EIP",
      "DependsOn": "Ec2Instance",
      "Properties": {
        "Domain": "vpc",
        "InstanceId": {
          "Ref": "Ec2Instance"
        }
      }
    },

    "Ec2Instance": {
      "Type": "AWS::EC2::Instance",
      "DependsOn": "RedshiftCluster",
      "Properties": {
        "ImageId": "ami-baf37ada",
        "KeyName": {
          "Ref": "KeyName"
        },
        "InstanceType": "t2.large",
        "NetworkInterfaces": [{
          "AssociatePublicIpAddress": "true",
          "DeviceIndex": "0",
          "GroupSet": [{
            "Ref": "Ec2InstanceSecurityGroup"
          }],
          "SubnetId": {
            "Ref": "PublicSubnet"
          }
        }],
        "BlockDeviceMappings" : [
               {
                  "DeviceName" : "/dev/xvda",

                  "Ebs" : {
                     "VolumeType" : "gp2",
                     "DeleteOnTermination" : "false",
                     "VolumeSize" : "30"
                  }
               }]

      }
    },
    "RedshiftCluster": {
      "Type": "AWS::Redshift::Cluster",
      "DependsOn": "AttachGateway",
      "Properties": {
        "ClusterType": {
          "Ref": "ClusterType"
        },
        "NumberOfNodes": {
          "Fn::If": ["IsMultiNodeCluster", {
            "Ref": "NumberOfNodes"
          }, {
            "Ref": "AWS::NoValue"
          }]
        },
        "NodeType": {
          "Ref": "NodeType"
        },
        "DBName": {
          "Ref": "DatabaseName"
        },
        "MasterUsername": {
          "Ref": "MasterUsername"
        },
        "MasterUserPassword": {
          "Ref": "MasterUserPassword"
        },
        "ClusterParameterGroupName": {
          "Ref": "RedshiftClusterParameterGroup"
        },
        "VpcSecurityGroupIds": [{
          "Ref": "SecurityGroup"
        }],
        "ClusterSubnetGroupName": {
          "Ref": "RedshiftClusterSubnetGroup"
        },
        "PubliclyAccessible": "true",
        "Port": {
          "Ref": "PortNumber"
        }
      }
    },
    "RedshiftClusterParameterGroup": {
      "Type": "AWS::Redshift::ClusterParameterGroup",
      "Properties": {
        "Description": "Cluster parameter group",
        "ParameterGroupFamily": "redshift-1.0",
        "Parameters": [{
          "ParameterName": "enable_user_activity_logging",
          "ParameterValue": "true"
        }]
      }
    },
    "RedshiftClusterSubnetGroup": {
      "Type": "AWS::Redshift::ClusterSubnetGroup",
      "Properties": {
        "Description": "Cluster subnet group",
        "SubnetIds": [{
          "Ref": "PublicSubnet"
        }]
      }
    },
    "RedshiftDMSRoleForMigration": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Statement": [{
            "Effect": "Allow",
            "Principal": {
              "Service": [
                "redshift.amazonaws.com"
              ]
            },
            "Action": [
              "sts:AssumeRole"
            ]
          }]
        },
        "ManagedPolicyArns": [
          "arn:aws:iam::aws:policy/service-role/AmazonDMSRedshiftS3Role"
        ],
        "Path": "/"
      }
    },
    "VPC": {
      "Type": "AWS::EC2::VPC",
      "Properties": {
        "CidrBlock": {
          "Ref": "VPCCIDR"
        },
        "EnableDnsSupport" : "true",
        "EnableDnsHostnames" : "true"
      }
    },
    "PublicSubnet": {
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "CidrBlock": {
          "Ref": "VPCSUBNET"
        },
        "VpcId": {
          "Ref": "VPC"
        },
        "AvailabilityZone" : "us-west-2a"
      }
    },
    "PublicSubnet2": {
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "CidrBlock": {
          "Ref": "VPCSUBNET2"
        },
        "VpcId": {
          "Ref": "VPC"
        },
        "AvailabilityZone" : "us-west-2b"
      }
    },
    "SecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "Security group",
        "SecurityGroupIngress": [{
          "CidrIp": {
            "Ref": "InboundTraffic"
          },
          "FromPort": {
            "Ref": "PortNumber"
          },
          "ToPort": {
            "Ref": "PortNumber"
          },
          "IpProtocol": "tcp"
        }],
        "VpcId": {
          "Ref": "VPC"
        }
      }
    },
    "myInternetGateway": {
      "Type": "AWS::EC2::InternetGateway"
    },
    "AttachGateway": {
      "Type": "AWS::EC2::VPCGatewayAttachment",
      "Properties": {
        "VpcId": {
          "Ref": "VPC"
        },
        "InternetGatewayId": {
          "Ref": "myInternetGateway"
        }
      }
    },
    "PublicRouteTable": {
      "Type": "AWS::EC2::RouteTable",
      "Properties": {
        "VpcId": {
          "Ref": "VPC"
        }
      }
    },
    "PublicRoute": {
      "Type": "AWS::EC2::Route",
      "DependsOn": "AttachGateway",
      "Properties": {
        "RouteTableId": {
          "Ref": "PublicRouteTable"
        },
        "DestinationCidrBlock": "0.0.0.0/0",
        "GatewayId": {
          "Ref": "myInternetGateway"
        }
      }
    },
    "PublicSubnetRouteTableAssociation": {
      "Type": "AWS::EC2::SubnetRouteTableAssociation",
      "Properties": {
        "SubnetId": {
          "Ref": "PublicSubnet"
        },
        "RouteTableId": {
          "Ref": "PublicRouteTable"
        }
      }
    },
    "PublicSubnetRouteTableAssociation2": {
      "Type": "AWS::EC2::SubnetRouteTableAssociation",
      "Properties": {
        "SubnetId": {
          "Ref": "PublicSubnet2"
        },
        "RouteTableId": {
          "Ref": "PublicRouteTable"
        }
      }
    }

  },
  "Outputs": {
    "ClusterEndpoint": {
      "Description": "Cluster endpoint",
      "Value": {
        "Fn::Join": [":", [{
          "Fn::GetAtt": ["RedshiftCluster", "Endpoint.Address"]
        }, {
          "Fn::GetAtt": ["RedshiftCluster", "Endpoint.Port"]
        }]]
      }
    },
    "ClusterName": {
      "Description": "Name of cluster",
      "Value": {
        "Ref": "RedshiftCluster"
      }
    },
    "ParameterGroupName": {
      "Description": "Name of parameter group",
      "Value": {
        "Ref": "RedshiftClusterParameterGroup"
      }
    },
    "RedshiftClusterSubnetGroupName": {
      "Description": "Name of cluster subnet group",
      "Value": {
        "Ref": "RedshiftClusterSubnetGroup"
      }
    },
    "IpAddressEc2OracleOnPrem": {
      "Description": "Public IP of the EC2 Oracle Server",
      "Value": {
        "Ref": "EipAddress"
      }
    },
     "PublicDNSName" : {
      "Description" : "DNS Endpoint of the EC2 Oracle Server",
      "Value" : { "Fn::GetAtt" : [ "Ec2Instance", "PublicDnsName" ] }
    },
     "IAMRedshiftDMSRoleName": {
      "Description": "The IAM role to be associated with the Redshift",
      "Value": {
        "Ref": "RedshiftDMSRoleForMigration"
      }
    },
    "RedshiftClusterSecurityGroupName": {
      "Description": "Name of cluster security group",
      "Value": {
        "Ref": "SecurityGroup"
      }
    }
  }
}