views:

366

answers:

1

I have a batch file that I'd like to run on startup of an EC2 Windows AMI. The program I'd like to run from that batch file takes the instance-id of the EC2 machine as a parameter. What is the simplest way to get that Instance ID passed as an argument to that program?

From Amazon's Documentation on the subject, I see that you're supposed to issue a WGET to a specified URL and parse the response. So an alternate way of phrasing this question might be "How do I pass the contents of a HTTP request to a program as an argument in a Windows batch file"

In pseudocode, this is what I'd like to do:

set ID = GET http://169.254.169.254/2008-08-08/meta-data/instance-id
myprogram.exe /instanceID=%ID%

Any suggestions on how I might proceed?

+1  A: 

You cannot open URLs directly in a batch file. You can certainly use wget or similar to retrieve the contents of that URL. Then you either get a file (whose contents you can assign to an environment variable [see for example Set= log.txt in batch]) or you'll get output from the program in which case you can wrap it into

for /f "usebackq delims=" %%x in (`print_contents_of_url http://169.254.169.254/2008-08-08/meta-data/instance-id`) do set ID=%%x

which would set %ID% to the server response at that URL [assuming that the program does what I named it]).

In both cases you'll need a separate program to speak HTTP, though.

Joey
That makes sense. So wget>file.txt, then set /p name=<log.txt. Thanks!
Jason Kester
You can't just pipe wget's output. But wget will write what it gets to a file with the -O option, so you should use wget -O file.txt http://...
Joey