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

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.


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

  • EC2Dream - Scripting with PowerShell
  • PowerShell and Amazon Web Service

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


$wc = New-Object System.Net.WebClient;
$instanceIdResult = $wc.DownloadString("http://169.254.169.254/latest/meta-data/instance-id")
Write-Host $instanceIdResult