How to programmatically/remotely execute a program in EC2 Windows instance
amazon-ec2, amazon-web-services, automation, upload, windows
Solution
The command `ec2-run-instances` has two additional arguments that can be used when running the command. The `user-data` command and `user-data-file` both of these perform the same task just they read from different input. When you use this argument the contents of the user-data will be uploaded to a amazon hosted URI `http://169.254.169.254/1.0/user-data` only available to the instance that was launched.
The normal way to do this in the linux environment would be to upload a shell script to the instance to download the exe, your user-data-file might look something like this...
#! /bin/bash
wget http://www.domain.com/my-file.exe
In Windows there's no default service installed to execute the user-data-file when the instance is booted but there is an open-source project CloudInit.NET which simulates the same process but with a powershell script. The only requirements are .NET 4.0 and CloudInit.NET. Once installed it will execute the user-data-file when the instance is booted. It's very easy to download a file and execute it with a powershell script.
!# /powershell/
$wc = New-Object System.Net.WebClient
$wc.DownloadFile("http://www.domain.com/my-file.exe", "C:\my-file.exe");
& 'C:\my-file.exe'
Problem
I'd like to launch an EC2 Windows instance, upload an EXEecutable & execute it (all in an automated fashion, this is important) So far I was able to programmatically launch EC2 Windows instance & get its parameters (password / IP), now I'd like to find a way to upload this Executable (from my Windows machine or from my other EC2 linux instance) & run it. I thought about launching an RDP connection & using a macro software to upload & execute the file, but based on previous experiences this is a poor/fragile approach to say the least. I also thought about uploading this EXE to a server, then do something like this on Windows: ``` wget http://www.domain.com/my-file.exe ``` Except that Windows doesn't have wget! So my question is: is there a way to programmatically upload & execute an EXEcutable in EC2 Windows instance?