Amazon EC2 Instances have metadata they can access. They get it by accessing a web server on a link-local address 169.254.169.254.
This PowerShell function will attempt to open http connection to the link-local address 169.254.169.254, and if successful will confirm that the host is running on Amazon Elastic Compute Cloud (Amazon EC2).

Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#-----------------------------------Detects if a Host is running on AWS EC2--------------------------------------------------------------- Function Test-AWSEC2 { $error.clear() $request = [System.Net.WebRequest]::Create('http://169.254.169.254/') $request.Timeout = 900 try { $response = $request.GetResponse() $response.Close() } catch { $false } if (!$error) { $true } } |


