Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to figure out the 'Public DNS Name' from within an Amazon EC2 instance?

I have a Windows 2008r2 instance on Amazon EC2. I would like to be able to get access to its "Public DNS Name" from within the instance. The Public DNS Name can also be found on my AWS EC2 console.

Is there a way to do this?

like image 555
Jay Godse Avatar asked Jan 13 '23 13:01

Jay Godse


1 Answers

Unknown's answer is technically correct (+1), but in order to provide the background and some more details, I'd like to mention the respective Amazon EC2 Instance Metadata and User Data:

Instance metadata is data about your EC2 instance that you can use to configure or manage the running instance. Instance metadata is divided into categories. For more information, see Instance Metadata Categories.

One of the many available metadata categories is the public-hostname:

The instance's public DNS. If the instance is in a VPC, this category is only returned if the enableDnsHostnames attribute is set to true. For more information, see Using DNS with Your VPC.

You can Retrieve Instance Metadata from within a running EC2 instance via the dedicated URI http://169.254.169.254/latest/meta-data/ with any HTTP capable tool, e.g. the ubiquitous cURL, the highly recommended HTTPie (a CLI, cURL-like tool for humans) and of course, most obvious for your use case, with PowerShell, esp. version 3.0+, e.g.:

PS> $publicHostname = Invoke-WebRequest `
      -Uri http://169.254.169.254/latest/meta-data/public-hostname
like image 108
Steffen Opel Avatar answered Jan 16 '23 19:01

Steffen Opel