简体   繁体   中英

How to get the public DNS of EC2 instance from outside using AWS CLI?

I have one instance and planning to have more in the near future but only if I can manage them from the outside world. Hence I need to be remotely request new servers, start them up or shut them down at will. Hence, since the IP address will change every time, I will need a method to query servers' public DNS name. And all of this needs to be done via AWS CLI.

For the ;ast few days I am trying to hunt this information down and I am finally here.

My server, outside the EC2 cluster has AWS CLI installed. I have my "Access Key ID" copied from the IAM page and assigned to the proper variable on this server

$ echo $AWS_ACCESS_KEY
XXXXXXXXXXXX4PEXXXXX
$ ec2-describe-instances
sanity-check: Your system clock is 302 seconds behind.
<?xml version="1.0" encoding="UTF-8"?>
<DescribeInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/">
        <requestId>53eb7530-2147-4ab6-8d6c-d9ddbdeef290</requestId>
        <reservationSet/>
</DescribeInstancesResponse>
$ ec2-describe-instances i-4XXXXXX0
sanity-check: Your system clock is 302 seconds behind.
+----------------------------+---------------------------------------------+
|            Code            |                   Message                   |
+----------------------------+---------------------------------------------+
| InvalidInstanceID.NotFound | The instance ID 'i-4XXXXXX0' does not exist |
+----------------------------+---------------------------------------------+

The secret key and the instance number both have been copied from the console and pasted into the terminal window. There is no way that there is a typo in either of them.

1.您能否在服务器中安装NTP并尝试查看错误“”

Install NTP:
yum install ntp
service ntpd restart

wait for some time to sync time & then fire below command,
ec2-describe-instances i-4XXXXXX0

Hope it'll help :)

First you need to configure AWS CLI properly and baked in your AWS ACCESS KEY and SECRET which has policy to describe instance.

For more information on how to setup aws-cli follow the link http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html

Once it is done properly, you can list ec2 instance in a particular region by passing region within query. Ex :- Describe all the ec2-instances in us-west-2 region.

aws ec2 describe-instances --region us-west-1

You can also use "jq" with pipe symbol, for parsing the json and just get the public ip of ec2-instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM