Get public dns name of ec2 instance using ec2 command line tools in bash

I have an ec2 instance name and you want to make ssh. How can I determine the "Public DNS" of an ec2 instance using the ec2 instance name.

I want to do this with bash.

+13
amazon-web-services amazon-ec2 dns
source share
5 answers
aws ec2 describe-instances --instance-ids i-12abc34 --query 'Reservations[].Instances[].PublicDnsName' 

Where i-12abc34 is your instance id

+14
source share

Using API EC2 Tools:

 # Region is only needed if not in us-east-1 $ ec2-describe-instances --region <region> <instance id> 

Using the single AWS CLI tool :

 $ aws --region <region> ec2 describe-instances --instance-ids <instance id 

I prefer a unified tool because it offers comprehensive and consistent data.

+7
source share

If you install the cloud-utils tool as described in this answer, it will be much more straight forward.

stack overflow

 ec2-metadata --public-ipv4 > public-ipv4: 54.200.4.52 
+2
source share

It depends on what you mean by "calculation". If you mean finding out yourself, you cannot. The public DNS name has nothing to do with the ec2 instance name. A common DNS name consists of a public IP address, region / availability zone, type of service, aws domain name, etc. For example, ec2-xx-xxx-x-xx.us-west-2.compute.amazonaws.com, since the public IP address changes every time you stop and start your instance, if you do not use an elastic IP address, your public DNS name will be changed.

If you mean finding out using the AWS API or the CLI tool, you can. Using the EC2 CLI, you must use the ec2-describe-instances of instance_id command. Again, the instance must be started, and the public DNS changes after it stops / starts.

+1
source share

You can request an instance metadata service .

Using curl:

 curl -s http://169.254.169.254/latest/meta-data/public-hostname 

Using wget:

 wget -qO - http://169.254.169.254/latest/meta-data/public-hostname 

If bold, the actual blow :

 exec 3<> /dev/tcp/169.254.169.254/80 echo -e "GET /latest/meta-data/public-hostname HTTP/1.0\r\n\r\n" >&3 cat <&3 

(The latter leaves the connection open to me, so cat stuck. Headers are also present in the output)

(This is from the instance itself and requires access to the instance - this is not the version associated with the instance name. There are enough answers here)

0
source share

All Articles