{
  "AWSTemplateFormatVersion": "2010-09-09",

  "Description": "AWS CloudFormation Sample Template: Redshift cluster in a VPC",
 
  "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])+"
    },
    "ClusterType": {
      "Description": "The type of the cluster",
      "Type": "String",
      "Default": "single-node",
      "AllowedValues": [
        "single-node",
        "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": "dw2.large"
    },
    "MasterUsername": {
      "Description": "The user name associated with the master user account for the redshift cluster that is being created",
      "Type": "String",
      "Default": "defaultuser",
      "AllowedPattern": "([a-z])([a-z]|[0-9])*",
      "NoEcho": "true"
    },
    "MasterUserPassword": {
      "Description": "The password associated with the master user account for the redshift cluster that is being created. ",
      "Type": "String",
      "NoEcho": "true"
    }
  },

  "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"
        }
      }
    },
    "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": "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"
        }
      }
    },
    "InternetGateway" : {
      "Type" : "AWS::EC2::InternetGateway"
    },
    "GatewayAttachment" : {
       "Type" : "AWS::EC2::VPCGatewayAttachment",
       "Properties" : {
         "VpcId" : { "Ref" : "VPC" },
         "InternetGatewayId" : { "Ref" : "InternetGateway" }
       }
    },
    "SecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "Security group",
        "SecurityGroupIngress": [
          {
            "CidrIp": "10.0.0.0/16",
            "FromPort": "80",
            "ToPort": "80",
            "IpProtocol": "tcp"
          }
        ],
        "VpcId": {
          "Ref": "VPC"
        }
      }
    }
  },

  "Outputs": {
    "ClusterEndpoint": {
      "Value": {
        "Fn::Join": [
          ":",
          [
            {
              "Fn::GetAtt": [
                "RedshiftCluster",
                "Endpoint.Address"
              ]
            },
            {
              "Fn::GetAtt": [
                "RedshiftCluster",
                "Endpoint.Port"
              ]
            }
          ]
        ]
      }
    }
  }

}