How to find the IP address of an amazon EC2 instance on reboot

前端 未结 10 1293
长发绾君心
长发绾君心 2020-12-12 13:50

On reboot, the IP address of an amazon instance changes. How to find the new IP address using java API?

相关标签:
10条回答
  • 2020-12-12 13:56

    The public IPv4 address is also available from the EC2 instance like so:

    curl checkip.amazonaws.com
    

    And the public hostname is:

    dig -x $(curl -s checkip.amazonaws.com) +short
    
    0 讨论(0)
  • 2020-12-12 14:00

    On reboot, the IP addresses of an EC2 instance do not change. They do generally change on stop/start of a non-VPC EBS boot instance.

    See my answer to your related question here:

    https://stackoverflow.com/questions/7533871/difference-between-rebooting-and-stop-starting-an-amazon-ec2-instance

    That said, you can find the private and public IP addresses through the API call for DescribeInstances in your particular language.

    If you are on the instance itself, you can also find the IP addresses through the user-data API using simple HTTP:

    http://instance-data/latest/meta-data/local-ipv4
    http://instance-data/latest/meta-data/public-ipv4
    

    For example,

    wget -qO- http://instance-data/latest/meta-data/public-ipv4
    

    Elastic IP addresses are recommended for keeping a consistent (static) externally facing IP address for a particular service or server. These need to be re-assigned to an instance after a stop/start (but not after a reboot).

    0 讨论(0)
  • 2020-12-12 14:00

    You can use CLI CRUD aplication to AWS resources. AWS-CRUD-Manager

    To find all ec2 instances

    ./awsconsole.py ec2 all
    

    To find all of data for one ec2 instances (including IP priv and public)

    ./awsconsole.py ec2 find -i <id-insta`ce>
    
    0 讨论(0)
  • 2020-12-12 14:01

    this is how I did it on an Ubuntu 12.04 EC2 instance within a VPC:

    curl ifconfig.me
    

    not sure why but public-ipv4 was not found under the meta-data url

    0 讨论(0)
  • 2020-12-12 14:07

    you Can Use This.

                    var ipofnewServer = string.Empty;    
                    while (string.IsNullOrEmpty(ipofnewServer))
                    {
                        var statusRequest1 = new DescribeInstancesRequest
                        {
                            InstanceIds = new List<string>() { instanceId }
                        };
    
                        var result = amazonEc2client.DescribeInstances(statusRequest1);
                        var status = result.Reservations[0].Instances.FirstOrDefault();
                        if (status != null)
                        {
                            ipofnewServer = status.PublicIpAddress;
                        }
                    }
    
    0 讨论(0)
  • 2020-12-12 14:08

    Assuming your rebooting the instance and not launching from scratch than you can assign an elastic IP which always remains with the ec2 instance(unless you move the IP to another server). This allows you to point all your DNS to that one IP and not worry that a reboot will cause you issues.

    I think thats what your asking but there are other things you could be asking. The internal IP of the server changes(if you relaunch the instance not reboot) and you can't 'reserve' it so you may need to create a script to keep you the new IP(if your pointing internal services to it).

    hope that helps

    0 讨论(0)
提交回复
热议问题