{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Description" : "Hadoop Cluster Demo Template",
  "Parameters" : {
    "VpcId" : {
      "Type" : "String",
      "Description" : "VPC CIDR address space",
      "AllowedPattern" : "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/16$",
      "Default" : "10.100.0.0/16",
      "ConstraintDescription" : "must be a valid CIDR"
    },
    "HadoopClusterCidrAz1" : {
      "Type" : "String",
      "Description" : "CIDR address space reserved for Hadoop cluster resources within AZ 1",
      "AllowedPattern" : "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/24$",
      "Default" : "10.100.1.0/24",
      "ConstraintDescription" : "must be a valid CIDR"
    },
    "HadoopClusterCidrAz2" : {
      "Type" : "String",
      "Description" : "CIDR address space reserved for Hadoop cluster resources within AZ 2",
      "AllowedPattern" : "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/24$",
      "Default" : "10.100.2.0/24",
      "ConstraintDescription" : "must be a valid CIDR"
    },
    "HadoopClusterManagerCidrAz2" : {
      "Type" : "String",
      "Description" : "CIDR address space reserved for the hadoop cluster manager resources within AZ 2",
      "AllowedPattern" : "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/24$",
      "Default" : "10.100.0.0/24",
      "ConstraintDescription" : "must be a valid CIDR"
    },
    "HadoopClusterManagerInstanceType" : {
      "Type" : "String",
      "Description" : "Type of EC2 instance to launch for hadoop management server.",
      "AllowedValues" : [ "t1.micro", "m1.small", "m1.medium", "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "m3.xlarge", "m3.2xlarge", "c1.medium", "c1.xlarge", "cc1.4xlarge", "cc2.8xlarge", "cg1.4xlarge" ],
      "Default" : "m1.small"
    },
    "MyPublicIP" : {
      "Type" : "String",
      "Description" : "The source IP address used to access to the hadoop cluster manager",
      "AllowedPattern" : "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/32$",
      "Default" : "44.45.46.47/32",
      "ConstraintDescription" : "must be a valid IP"
    },
    "HadoopClusterManagerPrivateIP" : {
      "Type" : "String",
      "Description" : "CIDR address reserved for Ambari server within AZ 1",
      "AllowedPattern" : "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$",
      "Default" : "10.100.0.100",
      "ConstraintDescription" : "must be a valid IP"
    },
    "HadoopMasterInstanceType" : {
      "Type" : "String",
      "Description" : "Type of EC2 instance to launch for Hadoop Master Server",
      "AllowedValues" : [ "t1.micro", "m1.small", "m1.medium", "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "m3.xlarge", "m3.2xlarge", "c1.medium", "c1.xlarge", "cc1.4xlarge", "cc2.8xlarge", "cg1.4xlarge" ],
      "Default" : "m1.medium"
    },
    "HadoopMasterPrivateIP" : {
      "Type" : "String",
      "Description" : "CIDR address reserved for hadoop master server within AZ 1",
      "AllowedPattern" : "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$",
      "Default" : "10.100.1.100",
      "ConstraintDescription" : "must be a valid IP"
    },
    "HadoopSlaveInstanceType" : {
      "Type" : "String",
      "Description" : "Type of EC2 instance to launch for Hadoop Slave Server.",
      "AllowedValues" : [ "t1.micro", "m1.small", "m1.medium", "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "m3.xlarge", "m3.2xlarge", "c1.medium", "c1.xlarge", "cc1.4xlarge", "cc2.8xlarge", "cg1.4xlarge" ],
      "Default" : "m1.medium"
    },
    "HadoopSlavePrivateIP" : {
      "Type" : "String",
      "Description" : "CIDR address reserved for hadoop slave server within AZ 2",
      "AllowedPattern" : "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$",
      "Default" : "10.100.2.100",
      "ConstraintDescription" : "must be a valid IP"
    },
    "HadoopClusterPrivateKey" : {
      "Type" : "String",
      "Description" : "Cluster Manager private key",
      "Default" : "HadoopClusterPrivateKey"
    },
    "NATInstanceType" : {
      "Description" : "NAT EC2 instance type",
      "Type" : "String",
      "Default" : "m1.small",
      "AllowedValues" : [ "t1.micro", "m1.small", "m1.medium", "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "m3.xlarge", "m3.2xlarge", "c1.medium", "c1.xlarge", "cc1.4xlarge", "cc2.8xlarge", "cg1.4xlarge" ],
      "ConstraintDescription" : "must be a valid EC2 instance type"
    }
  },
  "Mappings" : {
    "RHELRegionMap" : {
      "us-east-1" : {
        "AMI" : "ami-a25415cb"
      },
      "us-west-1" : {
        "AMI" : "ami-6283a827"
      },
      "us-west-2" : {
        "AMI" : "ami-b8a63b88"
      },
      "sa-east-1" : {
        "AMI" : "ami-fd73d7e0"
      },
      "eu-west-1" : {
        "AMI" : "ami-75342c01"
      },
      "ap-southeast-1" : {
        "AMI" : "ami-80bbf3d2"
      },
      "ap-southeast-2" : {
        "AMI" : "ami-1d62f027"
      },
      "ap-northeast-1" : {
        "AMI" : "ami-5769f956"
      }
    },
    "NATRegionMap" : {
      "ap-northeast-1" : {
        "AMI" : "ami-cd43d9cc"
      },
      "ap-southeast-1" : {
        "AMI" : "ami-f22772a0"
      },
      "ap-southeast-2" : {
        "AMI" : "ami-3bae3201"
      },
      "eu-west-1" : {
        "AMI" : "ami-f3e30084"
      },
      "sa-east-1" : {
        "AMI" : "ami-d78325ca"
      },
      "us-east-1" : {
        "AMI" : "ami-ad227cc4"
      },
      "us-west-1" : {
        "AMI" : "ami-d69aad93"
      },
      "us-west-2" : {
        "AMI" : "ami-f032acc0"
      }
    }
  },
  "Resources" : {
    "HadoopClusterManagerSecurityGroup" : {
      "Type" : "AWS::EC2::SecurityGroup",
      "Properties" : {
        "VpcId" : {
          "Ref" : "HadoopClusterVpcId"
        },
        "GroupDescription" : "Security Group containing the Hadoop cluster manager resources",
        "SecurityGroupIngress" : [ {
          "IpProtocol" : "tcp",
          "FromPort" : "22",
          "ToPort" : "22",
          "CidrIp" : {
            "Ref" : "MyPublicIP"
          }
        }, {
          "IpProtocol" : "tcp",
          "FromPort" : "8080",
          "ToPort" : "8080",
          "CidrIp" : {
            "Ref" : "MyPublicIP"
          }
        }, {
          "IpProtocol" : "tcp",
          "FromPort" : "0",
          "ToPort" : "65535",
          "SourceSecurityGroupId" : {
            "Ref" : "HadoopClusterSecurityGroup"
          }
        } ],
        "SecurityGroupEgress" : [ {
          "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" : "443",
          "ToPort" : "443",
          "CidrIp" : "0.0.0.0/0"
        } ]
      }
    },
    "HadoopClusterSecurityGroup" : {
      "Type" : "AWS::EC2::SecurityGroup",
      "Properties" : {
        "VpcId" : {
          "Ref" : "HadoopClusterVpcId"
        },
        "GroupDescription" : "Security Group containing Hadoop Master and Slave Servers",
        "SecurityGroupEgress" : [ {
          "IpProtocol" : "tcp",
          "FromPort" : "80",
          "ToPort" : "80",
          "CidrIp" : "0.0.0.0/0"
        }, {
          "IpProtocol" : "tcp",
          "FromPort" : "22",
          "ToPort" : "22",
          "CidrIp" : "0.0.0.0/0"
        }, {
          "IpProtocol" : "tcp",
          "FromPort" : "9418",
          "ToPort" : "9418",
          "CidrIp" : "0.0.0.0/0"
        }, {
          "IpProtocol" : "tcp",
          "FromPort" : "8080",
          "ToPort" : "8080",
          "CidrIp" : "0.0.0.0/0"
        }, {
          "IpProtocol" : "tcp",
          "FromPort" : "443",
          "ToPort" : "443",
          "CidrIp" : "0.0.0.0/0"
        } ]
      }
    },
    "HadoopClusterSecurityGroupEgress01" : {
      "Type" : "AWS::EC2::SecurityGroupEgress",
      "Properties" : {
        "GroupId" : {
          "Ref" : "HadoopClusterSecurityGroup"
        },
        "IpProtocol" : "tcp",
        "FromPort" : "0",
        "ToPort" : "65535",
        "DestinationSecurityGroupId" : {
          "Ref" : "HadoopClusterManagerSecurityGroup"
        }
      }
    },
    "HadoopClusterSecurityGroupEgress02" : {
      "Type" : "AWS::EC2::SecurityGroupEgress",
      "Properties" : {
        "GroupId" : {
          "Ref" : "HadoopClusterSecurityGroup"
        },
        "IpProtocol" : "tcp",
        "FromPort" : "0",
        "ToPort" : "65535",
        "DestinationSecurityGroupId" : {
          "Ref" : "HadoopClusterSecurityGroup"
        }
      }
    },
    "HadoopClusterSecurityGroupIngress01" : {
      "Type" : "AWS::EC2::SecurityGroupIngress",
      "Properties" : {
        "GroupId" : {
          "Ref" : "HadoopClusterSecurityGroup"
        },
        "IpProtocol" : "tcp",
        "FromPort" : "0",
        "ToPort" : "65535",
        "SourceSecurityGroupId" : {
          "Ref" : "HadoopClusterManagerSecurityGroup"
        }
      }
    },
    "HadoopClusterSecurityGroupIngress02" : {
      "Type" : "AWS::EC2::SecurityGroupIngress",
      "Properties" : {
        "GroupId" : {
          "Ref" : "HadoopClusterSecurityGroup"
        },
        "IpProtocol" : "tcp",
        "FromPort" : "0",
        "ToPort" : "65535",
        "SourceSecurityGroupId" : {
          "Ref" : "HadoopClusterSecurityGroup"
        }
      }
    },
    "HadoopClusterManagerInstance" : {
      "Type" : "AWS::EC2::Instance",
      "Properties" : {
        "ImageId" : { "Fn::FindInMap" : [ "RHELRegionMap", { "Ref" : "AWS::Region" }, "AMI" ] },
        "Tags" : [ {
          "Key" : "Name",
          "Value" : "clustermanager.example.com"
        } ],
        "InstanceType" : {
          "Ref" : "HadoopClusterManagerInstanceType"
        },
        "PrivateIpAddress" : {
          "Ref" : "HadoopClusterManagerPrivateIP"
        },
        "SubnetId" : {
          "Ref" : "HadoopClusterManagerSubnetAz2"
        },
        "KeyName" : {
          "Ref" : "HadoopClusterPrivateKey"
        },
        "SecurityGroupIds" : [ {
          "Ref" : "HadoopClusterManagerSecurityGroup"
        } ]
      }
    },
    "HadoopClusterVpcId" : {
      "Type" : "AWS::EC2::VPC",
      "Properties" : {
        "CidrBlock" : {
          "Ref" : "VpcId"
        },
        "EnableDnsSupport" : "true",
        "EnableDnsHostnames" : "true"
      }
    },
    "HadoopClusterManagerEIP" : {
      "Type" : "AWS::EC2::EIP",
      "Properties" : {
        "InstanceId" : {
          "Ref" : "HadoopClusterManagerInstance"
        },
        "Domain" : "vpc"
      }
    },
    "InternetGateway" : {
      "Type" : "AWS::EC2::InternetGateway"
    },
    "AttachIGW" : {
      "Type" : "AWS::EC2::VPCGatewayAttachment",
      "Properties" : {
        "InternetGatewayId" : {
          "Ref" : "InternetGateway"
        },
        "VpcId" : {
          "Ref" : "HadoopClusterVpcId"
        }
      },
      "DependsOn" : "InternetGateway"
    },
    "RouteViaIGW" : {
      "Type" : "AWS::EC2::RouteTable",
      "Properties" : {
        "VpcId" : {
          "Ref" : "HadoopClusterVpcId"
        }
      },
      "DependsOn" : "AttachIGW"
    },
    "RouteViaNAT" : {
      "Type" : "AWS::EC2::RouteTable",
      "Properties" : {
        "VpcId" : {
          "Ref" : "HadoopClusterVpcId"
        }
      },
      "DependsOn" : "AttachIGW"
    },
    "HadoopClusterSubnetAz1" : {
      "Type" : "AWS::EC2::Subnet",
      "Properties" : {
        "AvailabilityZone" : {
          "Fn::Select" : [ "0", {
            "Fn::GetAZs" : ""
          } ]
        },
        "CidrBlock" : {
          "Ref" : "HadoopClusterCidrAz1"
        },
        "Tags" : [ {
          "Key" : "Subnet",
          "Value" : "HadoopClusterSubnetAz1"
        } ],
        "VpcId" : {
          "Ref" : "HadoopClusterVpcId"
        }
      }
    },
    "HadoopMasterInstance" : {
      "Type" : "AWS::EC2::Instance",
      "Properties" : {
        "ImageId" : { "Fn::FindInMap" : [ "RHELRegionMap", { "Ref" : "AWS::Region" }, "AMI" ] },
        "Tags" : [ {
          "Key" : "Name",
          "Value" : "Hadoopmaster.example.com"
        } ],
        "BlockDeviceMappings" : [
          {
            "DeviceName"  : "/dev/sdc",
            "VirtualName" : "ephemeral0"
          }
        ],
        "InstanceType" : {
          "Ref" : "HadoopMasterInstanceType"
        },
        "PrivateIpAddress" : {
          "Ref" : "HadoopMasterPrivateIP"
        },
        "SubnetId" : {
          "Ref" : "HadoopClusterSubnetAz1"
        },
        "KeyName" : {
          "Ref" : "HadoopClusterPrivateKey"
        },
        "SecurityGroupIds" : [ {
          "Ref" : "HadoopClusterSecurityGroup"
        } ]
      }
    },

    "HadoopClusterSubnetAz2" : {
      "Type" : "AWS::EC2::Subnet",
      "Properties" : {
        "AvailabilityZone" : {
          "Fn::Select" : [ "1", {
            "Fn::GetAZs" : ""
          } ]
        },
        "CidrBlock" : {
          "Ref" : "HadoopClusterCidrAz2"
        },
        "Tags" : [ {
          "Key" : "Subnet",
          "Value" : "HadoopClusterSubnetAz2"
        } ],
        "VpcId" : {
          "Ref" : "HadoopClusterVpcId"
        }
      }
    },
    "HadoopSlaveInstance" : {
      "Type" : "AWS::EC2::Instance",
      "Properties" : {
        "ImageId" : { "Fn::FindInMap" : [ "RHELRegionMap", { "Ref" : "AWS::Region" }, "AMI" ] },
        "Tags" : [ {
          "Key" : "Name",
          "Value" : "Hadoopslave.example.com"
        } ],
        "BlockDeviceMappings" : [
          {
            "DeviceName"  : "/dev/sdc",
            "VirtualName" : "ephemeral0"
          }
        ],
        "InstanceType" : {
          "Ref" : "HadoopSlaveInstanceType"
        },
        "PrivateIpAddress" : {
          "Ref" : "HadoopSlavePrivateIP"
        },
        "SubnetId" : {
          "Ref" : "HadoopClusterSubnetAz2"
        },
        "KeyName" : {
          "Ref" : "HadoopClusterPrivateKey"
        },
        "SecurityGroupIds" : [ {
          "Ref" : "HadoopClusterSecurityGroup"
        } ]
      }
    },
    "HadoopClusterManagerSubnetAz2" : {
      "Type" : "AWS::EC2::Subnet",
      "Properties" : {
        "AvailabilityZone" : {
          "Fn::Select" : [ "1", {
            "Fn::GetAZs" : ""
          } ]
        },
        "CidrBlock" : {
          "Ref" : "HadoopClusterManagerCidrAz2"
        },
        "Tags" : [ {
          "Key" : "Subnet",
          "Value" : "HadoopClusterManagerSubnetAz2"
        } ],
        "VpcId" : {
          "Ref" : "HadoopClusterVpcId"
        }
      }
    },
    "IGWDefaultRoute" : {
      "Type" : "AWS::EC2::Route",
      "Properties" : {
        "RouteTableId" : {
          "Ref" : "RouteViaIGW"
        },
        "DestinationCidrBlock" : "0.0.0.0/0",
        "GatewayId" : {
          "Ref" : "InternetGateway"
        }
      }
    },
    "NATDefaultRoute" : {
      "Type" : "AWS::EC2::Route",
      "Properties" : {
        "RouteTableId" : {
          "Ref" : "RouteViaNAT"
        },
        "DestinationCidrBlock" : "0.0.0.0/0",
        "InstanceId" : {
          "Ref" : "NATDevice"
        }
      }
    },
    "NATIPAddress" : {
      "Type" : "AWS::EC2::EIP",
      "DependsOn" : "AttachIGW",
      "Properties" : {
        "Domain" : "vpc",
        "InstanceId" : {
          "Ref" : "NATDevice"
        }
      }
    },
    "NATDevice" : {
      "Type" : "AWS::EC2::Instance",
      "Properties" : {
        "InstanceType" : {
          "Ref" : "NATInstanceType"
        },
        "Tags" : [ {
          "Key" : "Name",
          "Value" : "nat.example.com"
        } ],
        "KeyName" : {
          "Ref" : "HadoopClusterPrivateKey"
        },
        "SubnetId" : {
          "Ref" : "HadoopClusterManagerSubnetAz2"
        },
        "SourceDestCheck" : "false",
        "ImageId" : { "Fn::FindInMap" : [ "NATRegionMap", { "Ref" : "AWS::Region" }, "AMI" ] },
        "SecurityGroupIds" : [ {
          "Ref" : "NATSecurityGroup"
        } ]
      }
    },
    "NATSecurityGroup" : {
      "Type" : "AWS::EC2::SecurityGroup",
      "Properties" : {
        "GroupDescription" : "Enable internal access to the NAT device",
        "VpcId" : {
          "Ref" : "HadoopClusterVpcId"
        },
        "SecurityGroupEgress" : [ {
          "IpProtocol" : "tcp",
          "FromPort" : "80",
          "ToPort" : "80",
          "CidrIp" : "0.0.0.0/0"
        }, {
          "IpProtocol" : "tcp",
          "FromPort" : "9418",
          "ToPort" : "9418",
          "CidrIp" : "0.0.0.0/0"
        }, {
          "IpProtocol" : "tcp",
          "FromPort" : "443",
          "ToPort" : "443",
          "CidrIp" : "0.0.0.0/0"
        } ]
      }
    },
    "NATSecurityGroupIngress01" : {
      "Type" : "AWS::EC2::SecurityGroupIngress",
      "Properties" : {
        "GroupId" : {
          "Ref" : "NATSecurityGroup"
        },
        "IpProtocol" : "tcp",
        "FromPort" : "22",
        "ToPort" : "22",
        "SourceSecurityGroupId" : {
          "Ref" : "HadoopClusterSecurityGroup"
        }
      }
    },
    "NATSecurityGroupIngress02" : {
      "Type" : "AWS::EC2::SecurityGroupIngress",
      "Properties" : {
        "GroupId" : {
          "Ref" : "NATSecurityGroup"
        },
        "IpProtocol" : "tcp",
        "FromPort" : "80",
        "ToPort" : "80",
        "SourceSecurityGroupId" : {
          "Ref" : "HadoopClusterSecurityGroup"
        }
      }
    },
    "NATSecurityGroupIngress03" : {
      "Type" : "AWS::EC2::SecurityGroupIngress",
      "Properties" : {
        "GroupId" : {
          "Ref" : "NATSecurityGroup"
        },
        "IpProtocol" : "tcp",
        "FromPort" : "443",
        "ToPort" : "443",
        "SourceSecurityGroupId" : {
          "Ref" : "HadoopClusterSecurityGroup"
        }
      }
    },
    "NATSecurityGroupIngress04" : {
      "Type" : "AWS::EC2::SecurityGroupIngress",
      "Properties" : {
        "GroupId" : {
          "Ref" : "NATSecurityGroup"
        },
        "IpProtocol" : "tcp",
        "FromPort" : "9418",
        "ToPort" : "9418",
        "SourceSecurityGroupId" : {
          "Ref" : "HadoopClusterSecurityGroup"
        }
      }
    },
    "HadoopClusterSubnetAz1RouteTableAssociation" : {
      "Type" : "AWS::EC2::SubnetRouteTableAssociation",
      "Properties" : {
        "SubnetId" : {
          "Ref" : "HadoopClusterSubnetAz1"
        },
        "RouteTableId" : {
          "Ref" : "RouteViaNAT"
        }
      }
    },
    "HadoopClusterSubnetAz2RouteTableAssociation" : {
      "Type" : "AWS::EC2::SubnetRouteTableAssociation",
      "Properties" : {
        "SubnetId" : {
          "Ref" : "HadoopClusterSubnetAz2"
        },
        "RouteTableId" : {
          "Ref" : "RouteViaNAT"
        }
      }
    },
    "HadoopClusterManagerSubnetAz2RouteTableAssociation" : {
      "Type" : "AWS::EC2::SubnetRouteTableAssociation",
      "Properties" : {
        "SubnetId" : {
          "Ref" : "HadoopClusterManagerSubnetAz2"
        },
        "RouteTableId" : {
          "Ref" : "RouteViaIGW"
        }
      }
    }
  },
  "Outputs" : {
    "HadoopClusterSubnetAz1" : {
      "Description" : "Subnet ids for hadoop cluster subnets",
      "Value" : {
        "Fn::Join" : [ ",", [ {
          "Ref" : "HadoopClusterSubnetAz1"
        }, {
          "Ref" : "HadoopClusterSubnetAz2"
        } ] ]
      }
    },
    "HadoopClusterManagerSubnetAz2" : {
      "Description" : "Subnet ids for hadoop manager subnet",
      "Value" : {
        "Ref" : "HadoopClusterManagerSubnetAz2"
      }
    },
    "HadoopClusterManagerEIP" : {
      "Value" : {
        "Ref" : "HadoopClusterManagerEIP"
      },
      "Description" : "Public IP address of hadoop cluster manager"
    }
  }
}