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

  "Description" : "This template enables roles and features of Windows Server. **WARNING** This template creates Amazon EC2 Windows instance and related resources. You will be billed for the AWS resources used if you create a stack from this template.",

  "Parameters" : {
    "KeyName" : {
      "Description" : { "Ref" : "AWS::StackId" },
      "Type" : "AWS::EC2::KeyPair::KeyName",
      "ConstraintDescription" : "must be the name of an existing EC2 KeyPair."
    },

    "InstanceType" : {
      "Description" : "Amazon EC2 instance type",
      "Type" : "String",
      "Default" : "m4.large",
      "AllowedValues" : [ "t1.micro", "t2.micro", "t2.small", "t2.medium", "m1.small", "m1.medium", "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "m3.medium", "m3.large", "m3.xlarge", "m3.2xlarge", "m4.large", "m4.xlarge", "m4.2xlarge", "m4.4xlarge", "m4.10xlarge", "c1.medium", "c1.xlarge", "c3.large", "c3.xlarge", "c3.2xlarge", "c3.4xlarge", "c3.8xlarge", "c4.large", "c4.xlarge", "c4.2xlarge", "c4.4xlarge", "c4.8xlarge", "g2.2xlarge", "r3.large", "r3.xlarge", "r3.2xlarge", "r3.4xlarge", "r3.8xlarge", "i2.xlarge", "i2.2xlarge", "i2.4xlarge", "i2.8xlarge", "d2.xlarge", "d2.2xlarge", "d2.4xlarge", "d2.8xlarge", "hi1.4xlarge", "hs1.8xlarge", "cr1.8xlarge", "cc2.8xlarge", "cg1.4xlarge"]
    ,
      "ConstraintDescription" : "must be a valid EC2 instance type."
    },
    "Roles" : {
      "Description" : "A SPACE seperated list of roles that you want to enable on this instance. Valid values are AD-Certificate, AD-Domain-Services, ADLDS, DHCP, DNS, Fax, File-Services, NPAS, Print-Services, Web-Server, and WDS.",
      "Type" : "String",
      "Default" : "None",
      "AllowedPattern" : "(((AD\\-Certificate)|(AD\\-Domain\\-Services)|(ADLDS)|(DHCP)|(DNS)|(Fax)|(File\\-Services)|(NPAS)|(Print\\-Services)|(Web\\-Server)|(WDS))( ((AD\\-Certificate)|(AD\\-Domain\\-Services)|(ADLDS)|(DHCP)|(DNS)|(Fax)|(File\\-Services)|(NPAS)|(Print\\-Services)|(Web\\-Server)|(WDS)))*)|(None)"
    },
    "Features" : {
      "Description" : "A SPACE seperated list of features that you want to enable on this instance. Valid values are NET-Framework, BITS, BitLocker, BranchCache, CMAK, Desktop-Experience, DAMC, Failover-Clustering, GPMC, Ink-Handwriting, Internet-Print-Client, ISNS, LPR-Port-Monitor, MSMQ, Multipath-IO, NLB, PNRP, qWave, Remote-Assistance, RDC, RPC-over-HTTP-Proxy, Simple-TCPIP, SMTP-Server, SNMP-Services, Storage-Mgr-SANS, Subsystem-UNIX-Apps, Telnet-Client, Telnet-Server, TFTP-Client, Biometric-Framework, Windows-Internal-DB, PowerShell-ISE, Backup-Features, Migration, WSRM, TIFF-IFilter, WinRM-IIS-Ext, WINS-Server, Wireless-Networking, and XPS-Viewer.",
      "Type" : "String",
      "Default" : "None",
      "AllowedPattern" : "(((NET\\-Framework)|(BITS)|(BitLocker)|(BranchCache)|(CMAK)|(Desktop\\-Experience)|(DAMC)|(Failover\\-Clustering)|(GPMC)|(Ink\\-Handwriting)|(Internet\\-Print\\-Client)|(ISNS)|(LPR\\-Port\\-Monitor)|(MSMQ)|(Multipath\\-IO)|(NLB)|(PNRP)|(qWave)|(Remote\\-Assistance)|(RDC)|(RPC\\-over\\-HTTP\\-Proxy)|(Simple\\-TCPIP)|(SMTP\\-Server)|(SNMP\\-Services)|(Storage\\-Mgr\\-SANS)|(Subsystem\\-UNIX\\-Apps)|(Telnet\\-Client)|(Telnet\\-Server)|(TFTP\\-Client)|(Biometric\\-Framework)|(Windows\\-Internal\\-DB)|(PowerShell\\-ISE)|(Backup\\-Features)|(Migration)|(WSRM)|(TIFF\\-IFilter)|(WinRM\\-IIS\\-Ext)|(WINS\\-Server)|(Wireless\\-Networking)|(XPS\\-Viewer))( ((NET\\-Framework)|(BITS)|(BitLocker)|(BranchCache)|(CMAK)|(Desktop\\-Experience)|(DAMC)|(Failover\\-Clustering)|(GPMC)|(Ink\\-Handwriting)|(Internet\\-Print\\-Client)|(ISNS)|(LPR\\-Port\\-Monitor)|(MSMQ)|(Multipath\\-IO)|(NLB)|(PNRP)|(qWave)|(Remote\\-Assistance)|(RDC)|(RPC\\-over\\-HTTP\\-Proxy)|(Simple\\-TCPIP)|(SMTP\\-Server)|(SNMP\\-Services)|(Storage\\-Mgr\\-SANS)|(Subsystem\\-UNIX\\-Apps)|(Telnet\\-Client)|(Telnet\\-Server)|(TFTP\\-Client)|(Biometric\\-Framework)|(Windows\\-Internal\\-DB)|(PowerShell\\-ISE)|(Backup\\-Features)|(Migration)|(WSRM)|(TIFF\\-IFilter)|(WinRM\\-IIS\\-Ext)|(WINS\\-Server)|(Wireless\\-Networking)|(XPS\\-Viewer)))*( )*)|(None)"
    },
    "SourceCidrForRDP" : {
      "Description" : "IP Cidr from which you are likely to RDP into the instances. You can add rules later by modifying the created security groups e.g. 54.32.98.160/32",
      "Type" : "String",
      "MinLength" : "9",
      "MaxLength" : "18",
      "AllowedPattern" : "^([0-9]+\\.){3}[0-9]+\\/[0-9]+$"
    }
  },

  "Mappings" : {
    "AWSRegion2AMI" : {
      "us-east-1"        : {"Windows2008r2" : "ami-d0ac46c6", "Windows2012r2" : "ami-abf616bd"},
      "us-west-2"        : {"Windows2008r2" : "ami-6b9f200b", "Windows2012r2" : "ami-1562d075"},
      "us-west-1"        : {"Windows2008r2" : "ami-fb2a789b", "Windows2012r2" : "ami-76227116"},
      "eu-west-1"        : {"Windows2008r2" : "ami-36a9f450", "Windows2012r2" : "ami-dd9eb6ae"},
      "eu-west-2"        : {"Windows2008r2" : "ami-4a949e2e", "Windows2012r2" : "ami-738a8017"},
      "eu-central-1"     : {"Windows2008r2" : "ami-c4e629ab", "Windows2012r2" : "ami-6f559800"},
      "ap-northeast-1"   : {"Windows2008r2" : "ami-d2bbc2b5", "Windows2012r2" : "ami-7accbe1d"},
      "ap-northeast-2"   : {"Windows2008r2" : "ami-c8da0ba6", "Windows2012r2" : "ami-e979af87"},
      "ap-southeast-1"   : {"Windows2008r2" : "ami-d6b41eb5", "Windows2012r2" : "ami-89e74cea"},
      "ap-southeast-2"   : {"Windows2008r2" : "ami-75abaf16", "Windows2012r2" : "ami-059d9866"},
      "ap-south-1"       : {"Windows2008r2" : "ami-67d2a408", "Windows2012r2" : "ami-31a6d05e"},
      "us-east-2"        : {"Windows2008r2" : "ami-f5547190", "Windows2012r2" : "ami-c06d48a5"},
      "ca-central-1"     : {"Windows2008r2" : "ami-bbf24fdf", "Windows2012r2" : "ami-7805b71c"},
      "sa-east-1"        : {"Windows2008r2" : "ami-18066374", "Windows2012r2" : "ami-f4d84298"},
      "cn-north-1"       : {"Windows2008r2" : "ami-b5c512d8", "Windows2012r2" : "ami-d4ad7ab9"}
    }

  },

  "Resources" : {

    "InstanceSecurityGroup" : {
      "Type" : "AWS::EC2::SecurityGroup",
      "Properties" : {
        "GroupDescription" : "Enable RDP",
        "SecurityGroupIngress" : [
          {"IpProtocol" : "tcp", "FromPort" : "3389", "ToPort" : "3389", "CidrIp" : { "Ref" : "SourceCidrForRDP" }}
        ]
      }
    },

    "WindowsServer": {
      "Type" : "AWS::EC2::Instance",
      "Metadata" : {
        "AWS::CloudFormation::Init" : {
          "config" : {
            "files" : {
              "c:\\cfn\\cfn-hup.conf" : {
                "content" : { "Fn::Join" : ["", [
                  "[main]\n",
                  "stack=", { "Ref" : "AWS::StackId" }, "\n",
                  "region=", { "Ref" : "AWS::Region" }, "\n"
                ]]}
              },
              "c:\\cfn\\hooks.d\\cfn-auto-reloader.conf" : {
                "content": { "Fn::Join" : ["", [
                  "[cfn-auto-reloader-hook]\n",
                  "triggers=post.update\n",
                  "path=Resources.WindowsServer.Metadata.AWS::CloudFormation::Init\n",
                  "action=cfn-init.exe -v -s ", { "Ref" : "AWS::StackId" },
                  " -r WindowsServer",
                  " --region ", { "Ref" : "AWS::Region" }, "\n"
                ]]}
              }
            },

            "commands" : {
              "1-install-roles" : {
                "command" : { "Fn::Join" : [ "", [
                  "if not \"None\" EQU \"",
                  { "Ref" : "Roles" },
                  "\" (servermanagercmd -install ",
                  { "Ref" : "Roles" },
                  " -restart)"]]
                }
              },
              "2-install-features" : {
                "command" : { "Fn::Join" : [ "", [
                  "if not \"None\" EQU \"",
                  { "Ref" : "Features" },
                  "\" (servermanagercmd -install ",
                  { "Ref" : "Features" },
                  " -restart)"]]
                }
              },
              "3-signal-success" : {
                "command" : { "Fn::Join" : [ "", [
                  "cfn-signal.exe -e %ERRORLEVEL% \"",
                  { "Fn::Base64" : { "Ref" : "WindowsServerWaitHandle" }},
                  "\""]]
                }
              }
            },

            "services" : {
              "windows" : {
                "cfn-hup" : {
                  "enabled" : "true",
                  "ensureRunning" : "true",
                  "files" : ["c:\\cfn\\cfn-hup.conf", "c:\\cfn\\hooks.d\\cfn-auto-reloader.conf"]
                }
              }
            }
          }
        }
      },

      "Properties": {
        "InstanceType" : { "Ref" : "InstanceType" },
        "ImageId" : { "Fn::FindInMap" : [ "AWSRegion2AMI", { "Ref" : "AWS::Region" }, "Windows2008r2" ]},
        "SecurityGroups" : [ {"Ref" : "InstanceSecurityGroup"} ],
        "KeyName" : { "Ref" : "KeyName" },
        "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
          "<script>\n",

          "cfn-init.exe -v -s ", { "Ref" : "AWS::StackId" },
          " -r WindowsServer",
          " --region ", { "Ref" : "AWS::Region" }, "\n",

          "</script>"
        ]]}}
      }
    },

    "WindowsServerWaitHandle" : {
      "Type" : "AWS::CloudFormation::WaitConditionHandle"
    },

    "WindowsServerWaitCondition" : {
      "Type" : "AWS::CloudFormation::WaitCondition",
      "DependsOn" : "WindowsServer",
      "Properties" : {
        "Handle" : {"Ref" : "WindowsServerWaitHandle"},
        "Timeout" : "1800"
      }
    }
  },

  "Outputs" : {
    "RolesEnabled" : {
      "Value" : { "Ref" : "Roles" },
      "Description" : "Roles enabled on this instance."
    },
    "FeaturesEnabled" : {
      "Value" : { "Ref" : "Features" },
      "Description" : "Features enabled on this instance."
    }
  }
}