Windows – How to get an EC2 Windows machine’s instance-id into a batch variable

amazon ec2batch-filewindows

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?

Best Answer

PowerShell 3.0 and Invoke-WebRequest:

PS> $instanceId = Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/instance-id

Or if you need to survive in batch, us a win32 binary of curl.

Or based on your use-case, you could use CloudFormation to get the Instance-Id during the API call and pass it to cf-init for a bootstrap action for your application deployment.

Related Topic