{
  "AWSTemplateFormatVersion": "2010-09-09",

  "Description": "AWS CloudFormation Sample Template RedshiftClusterInVpc: Create an Amazon Redshift cluster in an Amazon Virtual Private Cloud (VPC). **WARNING** This template creates Amazon Redshift Cluster. You will be billed for the AWS resources used if you create a stack from this template.",

  "Parameters": {
    "DatabaseName": {
      "Description": "The name of the first database to be created when the redshift cluster is created",
      "Type": "String",
      "Default": "defaultdb",
      "AllowedPattern": "([a-z]|[0-9])+",
      "ConstraintDescription" : "must contain a-z or 0-9 only."
    },

    "ClusterType": {
      "Description": "The type of the cluster",
      "Type": "String",
      "Default": "single-node",
      "AllowedValues": [ "single-node", "multi-node" ],
      "ConstraintDescription" : "must be single-node or multi-node."
    },

    "NumberOfNodes": {
      "Description": "The number of compute nodes in the redshift cluster.  When cluster type is specified as: 1) single-node, the NumberOfNodes parameter should be specified as 1, 2) multi-node, the NumberOfNodes parameter should be greater than 1",
      "Type": "Number",
      "Default": "1"
    },

    "NodeType": {
      "Description": "The node type to be provisioned for the redshift cluster",
      "Type": "String",
      "Default": "dc1.large",
      "AllowedValues" : [ "dw1.xlarge", "dw1.8xlarge", "dw2.large", "dw2.8xlarge", "dc1.large", "dc1.8xlarge", "ds1.xlarge", "ds1.8xlarge", "ds2.xlarge", "ds2.8xlarge" ]
,
      "ConstraintDescription" : "must be a valid RedShift node type."
    },

    "MasterUsername": {
      "Description": "The user name associated with the master user account for the redshift cluster that is being created",
      "Type": "String",
      "AllowedPattern": "([a-z])([a-z]|[0-9])*",
      "NoEcho": "true",
      "ConstraintDescription" : "must start with a-z and contain only a-z or 0-9."
    },

    "MasterUserPassword": {
      "Description": "The password associated with the master user account for the redshift cluster that is being created. ",
      "Type": "String",
      "NoEcho": "true",
      "MinLength": "1",
      "MaxLength": "41",
      "AllowedPattern" : "[a-zA-Z0-9]*",
      "ConstraintDescription" : "must contain only alphanumeric characters."
    },

    "DatabasePort": {
      "Description": "The port that Redshift will listen on and that will be allowed through the Security Group. ",
      "Type": "String",
      "Default": "5439"
    }
  },

  "Conditions": {
    "IsMultiNodeCluster": { "Fn::Equals": [ { "Ref": "ClusterType" }, "multi-node" ] }
  },

  "Resources": {
    "RedshiftCluster": {
      "Type": "AWS::Redshift::Cluster",
      "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": "false"
      }
    },
    "RedshiftClusterParameterGroup": {
      "Type": "AWS::Redshift::ClusterParameterGroup",
      "Properties": {
        "Description": "Cluster paraeter 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": "Subnet" } ]
      }
    },
    "VPC": {
      "Type": "AWS::EC2::VPC",
      "Properties": {
        "CidrBlock": "10.0.0.0/16"
      }
    },
    "Subnet": {
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "CidrBlock": "10.0.0.0/24",
        "VpcId": { "Ref": "VPC" }
      }
    },
    "SecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "Security group",
        "SecurityGroupIngress": [ {
          "CidrIp": "10.0.0.0/16", "FromPort": { "Ref" : "DatabasePort" }, "ToPort": { "Ref" : "DatabasePort" }, "IpProtocol": "tcp"
        } ],
        "VpcId": { "Ref": "VPC" }
      }
    }
  },

  "Outputs": {
    "ClusterEndpoint": {
      "Description" : "Endpoint for the newly created RedShift cluster",
      "Value": { "Fn::Join": [ ":", [ { "Fn::GetAtt": [ "RedshiftCluster", "Endpoint.Address" ] }, { "Fn::GetAtt": [ "RedshiftCluster", "Endpoint.Port" ] } ] ] }
    }
  }
}
