{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "AppStream Stand-Alone is subject to the AWS Customer Agreement and AppStream Service Terms.  AppStream Stand-Alone may only be used for development and testing; you may not host connections from end users on your AppStream Stand-Alone Instance.  Do not download, transmit or otherwise distribute AppStream materials provided with the AppStream Stand-Alone instance.",
  "Parameters": {
    "LauncherPath": {
      "Description": "Full path to launch the application.",
      "Type": "String",
      "Default": "C:\\app\\XStxDirectXServer\\XStxDirectXServer.exe"
    },
    "InstallerParameters": {
      "Description": "Command line parameters for installing the application.",
      "Type": "String",
      "Default": " "
    },
    "ApplicationPresignedS3Url": {
      "Description": "Pre-signed or public S3 URL to the application installer.",
      "Type": "String",
      "Default": "https://s3.amazonaws.com/appstream-public/XStxDirectXServerInstaller_1.6.exe"
    },
    "AppStreamSDKVersion": {
      "Description": "Supported AppStream SDK Version",
      "Type": "String",
      "Default": "1.6"
    },
    "KeyPairName": {
      "Description": "Keypair associated with the EC2 instance",
      "Type": "String",
      "MinLength": "1",
      "ConstraintDescription": "Must provide a keypair to be associated with the EC2 instance"
    },
    "LauncherParameters": {
      "Description": "Command line parameters for launching the application",
      "Type": "String",
      "Default": " "
    },
    "NvidiaGRIDdrivers": {
      "Description": "AppStream Stand-Alone requires the use of NVidia GRID drivers. Enter \"y\" or \"yes\" in the NVidiaGRIDdrivers parameter to direct us to download and install the NVidia GRID K520 drivers from http://www.nvidia.com/download/driverResults.aspx/64595/en-us and accept the applicable license agreement on your behalf.",
      "Type": "String",
      "AllowedPattern": "(yes|YES|Yes|y|Y)",
      "ConstraintDescription": "AppStream Stand-Alone requires the use of NVidia GRID drivers. Enter \"y\" or \"yes\" in the NVidiaGRIDdrivers parameter to direct us to download and install the NVidia GRID K520 drivers from http://www.nvidia.com/download/driverResults.aspx/64595/en-us and accept the applicable license agreement on your behalf."
    },
    "DeveloperPassword": {
      "Description": "Your AppStream streaming application will run under the Developer user account. Please enter an alphanumeric password for the Developer user that is between 12-20 characters, include at least 2 numbers, 2 lower case letters, 2 upper case letters, 2 special characters (@#$%^&+=) and does not contain any spaces.",
      "Type": "String",
      "AllowedPattern": "^(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z])(?=.*[A-Z].*[A-Z])(?=.*[@#$%^&+=].*[@#$%^&+=])(?=\\S+$).{12,20}$",
      "ConstraintDescription": "You must provide a password that is between 12-20 characters, include at least 2 numbers, 2 lower case letters, 2 upper case letters, 2 special characters (@#$%^&+=) and does not contain any spaces.",
      "NoEcho": "true"
    },
    "TightVNC": {
      "Description": "You may access AppStream Stand-Alone through VNC. Enter \"y\" or \"yes\" to direct us to download and install the free open-source TightVNC server from http://www.tightvnc.com/download.php and accept the applicable license agreement on your behalf. The TightVNC password will be the password you provided in the TightVNCPassword parameter above.",
      "Type": "String"
    },
    "TightVNCPassword": {
      "Description": "If you chose to install TightVNC, please enter an alphanumeric password for TightVNC that is between 8 characters, include at least 1 number, 1 lower case letter, 1 upper case letter, 1 special character (@#$%^&+=) and does not contain any spaces.",
      "Type": "String",
      "AllowedPattern": "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8}$",
      "ConstraintDescription": "You must provide a password that is exactly 8 characters long, include at least 1 number, 1 lower case letter, 1 upper case letter, 1 special character (@#$%^&+=) and does not contain any spaces.",
      "NoEcho": "true"
    },
    "MicrosoftVirtualAudioDrivers": {
      "Description": "AppStream Stand-Alone requires the use of free Virtual Audio drivers from Microsoft. Enter \"y\" or \"yes\" in the MicrosoftVirtualAudioDrivers parameter to direct us to download and install the Microsoft Virtual Audio drivers from http://code.msdn.microsoft.com/windowshardware/virtual-audio-device-3d4e6150/ and accept the applicable license agreement on your behalf.",
      "Type": "String",
      "AllowedPattern": "(yes|YES|Yes|y|Y)",
      "ConstraintDescription": " AppStream Stand-Alone requires the use of free Virtual Audio drivers from Microsoft. Enter \"y\" or \"yes\" in the MicrosoftVirtualAudioDrivers parameter to direct us to download and install the Microsoft Virtual Audio drivers from http://code.msdn.microsoft.com/windowshardware/virtual-audio-device-3d4e6150/ and accept the applicable license agreement on your behalf."
    }
  },
  "Mappings": {
    "RegionMap": {
      "us-east-1": {
        "64": "ami-c6ac7eae"
      },
      "us-west-1": {
        "64": "ami-799a973c"
      },
      "us-west-2": {
        "64": "ami-0f45003f"
      },
      "ap-northeast-1": {
        "64": "ami-05792204"
      },
      "eu-west-1": {
        "64": "ami-50d90527"
      }
    }
  },
  "Resources": {
    "SecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "Enable streaming, VNC, and RDP traffic",
        "SecurityGroupIngress": [
          {
            "IpProtocol": "tcp",
            "FromPort": "80",
            "ToPort": "80",
            "CidrIp": "0.0.0.0/0"
          },
          {
            "IpProtocol": "udp",
            "FromPort": "9070",
            "ToPort": "9880",
            "CidrIp": "0.0.0.0/0"
          },
          {
            "IpProtocol": "icmp",
            "FromPort": "0",
            "ToPort": "0",
            "CidrIp": "0.0.0.0/0"
          },
          {
            "IpProtocol": "tcp",
            "FromPort": "5800",
            "ToPort": "5800",
            "CidrIp": "0.0.0.0/0"
          },
          {
            "IpProtocol": "tcp",
            "FromPort": "5900",
            "ToPort": "5900",
            "CidrIp": "0.0.0.0/0"
          },
          {
            "IpProtocol": "tcp",
            "FromPort": "3389",
            "ToPort": "3389",
            "CidrIp": "0.0.0.0/0"
          }
        ]
      }
    },
    "BaseInstance": {
      "Type": "AWS::EC2::Instance",
      "Metadata": {
        "AWS::CloudFormation::Init": {
          "config": {
            "files": {
              "C:\\Users\\Public\\AppStream\\ApplicationInstaller.exe": {
                "source": {
                  "Ref": "ApplicationPresignedS3Url"
                }
              },
              "C:\\Users\\Public\\AppStream\\StxServerComponentsInstaller.exe": {
                "source": {
                  "Fn::Join": [
                    "",
                    [
                      "https://s3.amazonaws.com/appstream-public/StxServerComponentsInstaller_",
                      {
                        "Ref": "AppStreamSDKVersion"
                      },
                      ".exe"
                    ]
                  ]
                }
              },
              "C:\\Users\\Public\\AppStream\\AppStreamSimulator.exe": {
                "source": "https://s3.amazonaws.com/appstream-public/AppStreamSimulator.exe"
              },
              "C:\\Users\\Public\\AppStream\\AppStreamBootstrap.exe": {
                "source": "https://s3.amazonaws.com/appstream-public/AppStreamBootstrap.exe"
              }
            },
            "commands": {
              "1-install-stx-components": {
                "command": {
                  "Fn::Join": [
                    "",
                    [
                      "C:\\Users\\Public\\AppStream\\StxServerComponentsInstaller.exe ",
                      "C:\\stx\\",
                      {
                        "Ref": "AppStreamSDKVersion"
                      },
                      "\\bin"
                    ]
                  ]
                }
              },
              "2-install-application": {
                "command": {
                  "Fn::Join": [
                    " ",
                    [
                      "C:\\Users\\Public\\AppStream\\ApplicationInstaller.exe",
                      {
                        "Ref": "InstallerParameters"
                      }
                    ]
                  ]
                }
              }
            }
          }
        }
      },
      "Properties": {
        "InstanceType": "g2.2xlarge",
        "ImageId": {
          "Fn::FindInMap": [
            "RegionMap",
            {
              "Ref": "AWS::Region"
            },
            "64"
          ]
        },
        "UserData": {
          "Fn::Base64": {
            "Fn::Join": [
              "",
              [
                "<powershell>\n",
                "CMD.EXE /C cfn-init.exe -v -s",
                {
                  "Ref": "AWS::StackId"
                },
                " -r BaseInstance",
                " --region ",
                {
                  "Ref": "AWS::Region"
                },
                "\n",
                "$developer = [ADSI]\"WinNT://$env:COMPUTERNAME/Developer,user\"\n",
                "$developer.SetPassword('",
                {
                  "Ref": "DeveloperPassword"
                },
                "')\n",
                "$developer.SetInfo()\n",
                "CMD.EXE /C netsh advfirewall firewall add rule name=\"STX-UDP-100\" dir=in action=allow protocol=UDP localport=9070-9880\n",
                "$wc=new-object System.Net.WebClient\n",
                "$vncpassword=\"",
                {
                  "Ref": "TightVNCPassword"
                },
                "\"\n",
                "if (\"",
                {
                  "Ref": "TightVNC"
                },
                "\" -cmatch \"(yes|YES|Yes|y|Y)\") {\n",
                "$wc.DownloadFile(\"https://s3.amazonaws.com/appstream-public/tightvnc-2.7.10-setup-64bit.msi\",\"C:\\Users\\Public\\AppStream\\tightvnc-setup.msi\")\n",
                "msiexec /i \"C:\\Users\\Public\\AppStream\\tightvnc-setup.msi\" /quiet /norestart SET_USEVNCAUTHENTICATION=1 VALUE_OF_USEVNCAUTHENTICATION=1 SET_PASSWORD=1 VALUE_OF_PASSWORD=$vncPassword\n",
                "CMD.EXE /C cfn-signal.exe -i TightVNC -d \"TightVNC was successfully installed with the password provided in the TightVNCPassword parameter.\" -e %ERRORLEVEL% ",
                {
                  "Fn::Base64": {
                    "Ref": "BaseInstanceWaitHandle"
                  }
                },
                "\n",
                "} else {\n",
                "CMD.EXE /C cfn-signal.exe -i TightVNC -d \"TightVNC was not installed because you did not request it in the parameters.\" ",
                {
                  "Fn::Base64": {
                    "Ref": "BaseInstanceWaitHandle"
                  }
                },
                "\n",
                "}\n",
                "$i_path = \"C:\\Users\\Public\\AppStream\"\n",
                "$ls_script = \"install_logstash.ps1\"\n",
                "$s3_source = \"https://s3.amazonaws.com/logstash-forwarder\"\n",
                "$wc.DownloadFile(\"$($s3_source)/$($ls_script)\", \"$($i_path)\\$($ls_script)\")\n",
                "$result = & \"$($i_path)\\$($ls_script)\" -installation_path \"$($i_path)\"\n",
                "Remove-Item \"$($i_path)\\$($ls_script)\"\n",
                "$launchFile = \"C:\\Users\\Public\\AppStream\\launch.txt\"\n",
                "$stream = [System.IO.StreamWriter] $launchFile\n",
                "$stream.WriteLine(\"",
                {
                  "Ref": "LauncherPath"
                },
                " ",
                {
                  "Ref": "LauncherParameters"
                },
                "\")\n",
                "$applicationDirectory = Split-Path \"",
                {
                  "Ref": "LauncherPath"
                },
                "\"\n",
                "$stream.WriteLine($applicationDirectory)\n",
                "$stream.Close();\n",
                "\"trigger\" >> $launchFile\".trigger\";\n",
                "CMD.EXE /C cfn-signal.exe -e %ERRORLEVEL% ",
                {
                  "Fn::Base64": {
                    "Ref": "BaseInstanceWaitHandle"
                  }
                },
                "\n",
                "CMD.EXE /C shutdown.exe /s /t 259200\n",
                "</powershell>\n"
              ]
            ]
          }
        },
        "SecurityGroups": [
          {
            "Ref": "SecurityGroup"
          }
        ],
        "KeyName": {
          "Ref": "KeyPairName"
        }
      }
    },
    "BaseInstanceWaitHandle": {
      "Type": "AWS::CloudFormation::WaitConditionHandle"
    },
    "BaseInstanceWaitCondition": {
      "Type": "AWS::CloudFormation::WaitCondition",
      "DependsOn": "BaseInstance",
      "Properties": {
        "Handle": {
          "Ref": "BaseInstanceWaitHandle"
        },
        "Timeout": "43200",
        "Count": "2"
      }
    }
  },
  "Outputs": {
    "InstanceId": {
      "Value": {
        "Ref": "BaseInstance"
      }
    },
    "PublicIp": {
      "Value": {
        "Fn::GetAtt": [
          "BaseInstance",
          "PublicDnsName"
        ]
      },
      "Description": "DNS Name of the newly created EC2 instance"
    },
    "TightVNC": {
      "Description": "Status of TightVNC install if applicable",
      "Value": {
        "Fn::GetAtt": [
          "BaseInstanceWaitCondition",
          "Data"
        ]
      }
    }
  }
}
