10

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?

6 Answers 6

8

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.

1
  • 3
    This returns a webresponse - in order to get the actual id - you might want to use: $instanceId = (Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/instance-id).Content Feb 23, 2015 at 9:53
3

Alternative: maybe you could do this using PowerShell on Amazon's EC2. Here are some links to start:

0
2

Powershell would be the easiest way to do this:

$webclient = new-object System.Net.WebClient
$myip = $webclient.DownloadString("http://169.254.169.254/latest/meta-data/local-ipv4")
myprogram.exe /instanceID=$myip

2
$wc = New-Object System.Net.WebClient;
$instanceIdResult = $wc.DownloadString("http://169.254.169.254/latest/meta-data/instance-id")
Write-Host $instanceIdResult
1
  • 2
    How is this any different from the other answer using a WebClient and DownloadString??
    – Chris S
    Apr 30, 2013 at 5:28
2

So I found another way of doing this:

$instanceId = Get-EC2InstanceMetadata -Path '/instance-id'

It seems a lot cleaner and result contains just what you expect "Instance ID"

0

wget doesn't have an option to output the contents of the downloaded file to screen (or at least the version I have here doesn't), so you have to use a temporary file anyway. What you can do then is the following:

wget -O "instance-id" "http://169.254.169.254/2008-08-08/meta-data/instance-id"
set /p ID=< instance-id

set /p usually prompts for something and we just redirect the file's contents here. This assumes that the instance ID is the only thing that's in that file. You can have a little more fun with parsing text when using for /f but for a simple "put the first line of that file into a variable" set /p suffices.

2
  • Does wget -O - http://... not output the file to standard out? Dec 19, 2009 at 10:00
  • Ah, drat. Wasn't written into wget --help but it works, yes. Sorry.
    – Joey
    Jan 5, 2010 at 19:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .