How to Find an AWS EC2 Instance’s Public DNS Name from within Itself

Adam Bertram

Adam Bertram

Read more posts by this author.

If you’re ever in need of using an Amazon AWS’s public DNS name from within a script running on an EC2 instance, here’s a quick way to get it using PowerShell. This technique uses the Instance Metadata that every EC2 instance has available. I’m using the parameterBasicParsing here because if running this on Server Core without IE installed, it will bomb.

((Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/public-hostname -UseBasicParsing).RawContent -split "`n")[-1]

Subscribe to Adam the Automator

Get the latest posts delivered right to your inbox

Looks like you're offline!