{
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Description" : "This template creates a single server installation of Microsoft SharePoint Foundation 2010. **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. Also, you are solely responsible for complying with the license terms for the software downloaded and installed by this template. By creating a stack from this template, you are agreeing to such terms.",

  "Parameters" : {
    "KeyName" : {
      "Description" : "Name of an existing EC2 KeyPair",
      "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."
    },
    "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" : {
      "af-south-1"       : {"Windows2008r2" : "NOT_SUPPORTED", "Windows2012r2" : "ami-0cfa9dded00d79010"},
      "ap-east-1"        : {"Windows2008r2" : "NOT_SUPPORTED", "Windows2012r2" : "ami-0f5d39d4cbc341c86"},
      "ap-northeast-1"   : {"Windows2008r2" : "ami-085c81c14a7b381ca", "Windows2012r2" : "ami-0454ff3af36274845"},
      "ap-northeast-2"   : {"Windows2008r2" : "ami-088344835427780e6", "Windows2012r2" : "ami-06d7a1ce46931c14c"},
      "ap-northeast-3"   : {"Windows2008r2" : "ami-0cee4466916e5a052", "Windows2012r2" : "ami-0c326532f9c4d9162"},
      "ap-south-1"       : {"Windows2008r2" : "ami-0fd8cc2e22ab2adda", "Windows2012r2" : "ami-09ebce4ce578dc5e8"},
      "ap-south-2"       : {"Windows2008r2" : "NOT_SUPPORTED", "Windows2012r2" : "ami-0869fbe9b3838138b"},
      "ap-southeast-1"   : {"Windows2008r2" : "ami-0495e047d2d1badaa", "Windows2012r2" : "ami-0e20726e49b76eab8"},
      "ap-southeast-2"   : {"Windows2008r2" : "ami-015a1163c733ed7f2", "Windows2012r2" : "ami-06f996e615c48aa41"},
      "ap-southeast-3"   : {"Windows2008r2" : "NOT_SUPPORTED", "Windows2012r2" : "ami-0e1388490bf389fd1"},
      "ap-southeast-4"   : {"Windows2008r2" : "NOT_SUPPORTED", "Windows2012r2" : "ami-075ad37d631e62706"},
      "il-central-1"     : {"Windows2008r2" : "NOT_SUPPORTED", "Windows2012r2" : "ami-0cf0064a8aae084b4"},
      "ca-central-1"     : {"Windows2008r2" : "ami-0afa7c1dbb0b9d1d1", "Windows2012r2" : "ami-08c0b33851325380c"},
      "cn-north-1"       : {"Windows2008r2" : "NOT_SUPPORTED", "Windows2012r2" : "ami-039ba42d86d34e171"},
      "cn-northwest-1"   : {"Windows2008r2" : "NOT_SUPPORTED", "Windows2012r2" : "ami-0530533b937d3c4d2"},
      "eu-central-1"     : {"Windows2008r2" : "ami-056afe0639d091031", "Windows2012r2" : "ami-05310014dbdc44a99"},
      "eu-north-1"       : {"Windows2008r2" : "ami-099397d9c5ef80746", "Windows2012r2" : "ami-0fb7acaec06741f9f"},
      "eu-south-1"       : {"Windows2008r2" : "NOT_SUPPORTED", "Windows2012r2" : "ami-09e043e582ffb399a"},
      "eu-west-1"        : {"Windows2008r2" : "ami-09eefdfde3f696305", "Windows2012r2" : "ami-03cda46dd642e55f8"},
      "eu-west-2"        : {"Windows2008r2" : "ami-0eca562c3df3c8a6b", "Windows2012r2" : "ami-0cb2af77be2ed2ff0"},
      "eu-west-3"        : {"Windows2008r2" : "ami-0b3d55b8cd5b7f54b", "Windows2012r2" : "ami-00a5fbd26da55e240"},
      "me-south-1"       : {"Windows2008r2" : "NOT_SUPPORTED", "Windows2012r2" : "ami-057555625187f1d3b"},
      "me-central-1"     : {"Windows2008r2" : "NOT_SUPPORTED", "Windows2012r2" : "ami-04b7e532dc2c45700"},
      "eu-south-2"       : {"Windows2008r2" : "NOT_SUPPORTED", "Windows2012r2" : "ami-0393ee4c633da412e"},
      "eu-central-2"     : {"Windows2008r2" : "NOT_SUPPORTED", "Windows2012r2" : "ami-0f8e7a608a6403440"},
      "sa-east-1"        : {"Windows2008r2" : "ami-04c78ad2bd0a5dd3c", "Windows2012r2" : "ami-034d4b42e99b678ed"},
      "us-east-1"        : {"Windows2008r2" : "ami-02fa4836310cbeccd", "Windows2012r2" : "ami-0957787dbb9364346"},
      "us-east-2"        : {"Windows2008r2" : "ami-038739908c6a12458", "Windows2012r2" : "ami-05cfbff0b36f8f8bc"},
      "us-west-1"        : {"Windows2008r2" : "ami-0eb6f805520147bde", "Windows2012r2" : "ami-079be55edd04e1ad0"},
      "us-west-2"        : {"Windows2008r2" : "ami-0e56450bb7dc3d34b", "Windows2012r2" : "ami-06cf09bb5b6fd52c6"}
    }

  },

  "Resources" : {
    "SharePointFoundationSecurityGroup" : {
      "Type" : "AWS::EC2::SecurityGroup",
      "Properties" : {
        "GroupDescription" : "Enable HTTP and RDP",
        "SecurityGroupIngress" : [
          {"IpProtocol" : "tcp", "FromPort" : "80", "ToPort" : "80", "CidrIp" : "0.0.0.0/0"},
          {"IpProtocol" : "tcp", "FromPort" : "3389", "ToPort" : "3389", "CidrIp" : { "Ref" : "SourceCidrForRDP" }}
        ]
      }
    },

    "SharePointFoundationEIP" : {
      "Type" : "AWS::EC2::EIP",
      "Properties" : {
        "InstanceId" : { "Ref" : "SharePointFoundation" }
      }
    },

    "SharePointFoundation": {
      "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.SharePointFoundation.Metadata.AWS::CloudFormation::Init\n",
                  "action=cfn-init.exe -v -s ", { "Ref" : "AWS::StackId" },
                                                 " -r SharePointFoundation",
                                                 " --region ", { "Ref" : "AWS::Region" }, "\n"
                ]]}
              },
              "C:\\SharePoint\\SharePointFoundation2010.exe" : {
                "source" : "http://d3adzpja92utk0.cloudfront.net/SharePointFoundation.exe"
              }
            },
            "commands" : {
              "1-extract" : {
                "command" : "C:\\SharePoint\\SharePointFoundation2010.exe /extract:C:\\SharePoint\\SPF2010 /quiet /log:C:\\SharePoint\\SharePointFoundation2010-extract.log"
              },
              "2-prereq" : {
                "command" : "C:\\SharePoint\\SPF2010\\PrerequisiteInstaller.exe /unattended"
              },
              "3-install" : {
                "command" : "C:\\SharePoint\\SPF2010\\setup.exe /config C:\\SharePoint\\SPF2010\\Files\\SetupSilent\\config.xml"
              }
            },
            
            "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" }, "Windows2012r2" ]},
        "SecurityGroups" : [ {"Ref" : "SharePointFoundationSecurityGroup"} ],
        "KeyName" : { "Ref" : "KeyName" },
        "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
          "<script>\n",

          "cfn-init.exe -v -s ", { "Ref" : "AWS::StackId" }, 
          " -r SharePointFoundation",
          " --region ", { "Ref" : "AWS::Region" }, "\n",

          "cfn-signal.exe -e %ERRORLEVEL% ", { "Fn::Base64" : { "Ref" : "SharePointFoundationWaitHandle" }}, "\n",
          
          "</script>"
          ]]}}
        }
    },

    "SharePointFoundationWaitHandle" : {
      "Type" : "AWS::CloudFormation::WaitConditionHandle"
    },

    "SharePointFoundationWaitCondition" : {
      "Type" : "AWS::CloudFormation::WaitCondition",
      "DependsOn" : "SharePointFoundation",
      "Properties" : {
        "Handle" : {"Ref" : "SharePointFoundationWaitHandle"},
        "Timeout" : "3600"
      }
    }
  },

  "Outputs" : {
    "SharePointFoundationURL" : {
      "Value" : { "Fn::Join" : ["", ["http://", { "Ref" : "SharePointFoundationEIP" } ]] },
      "Description" : "SharePoint Team Site URL. Please retrieve Administrator password of the instance and use it to access the URL"
    }
  }
}
