问题
I am running an EC2 Linux instance. For some maintenance purposes I shutdown the instance and started it again. However, EC2 IP has changed now.
How to keep an IP address of Amazon EC2 instance unchanged after stop and start it again?
回答1:
Actually, When you stop/start your instance, the IP address will change. If you reboot the instance, it will keep the same IP addresses. Unfortunately, it is not possible for us to reassign the address to your instance as that address would have been released back into the pool used by other EC2 instances.
If you want to avoid this issue in the future, depending on your needs:
- If you only need a fixed public IP address, you can assign an Elastic IP address to your instance.
- If you need both public and private IP addresses to remain the same throughout the lifetime of the instance, you can launch your instance in VPC instead. The private IP address assigned to an instance in VPC remains with the instance through to termination.
To learn more, see the aws documentation to assign elastic ip.
回答2:
If you don't want your IP adddress to be changed after stopping and starting your EC2 Instance, use Elastic IP.
For more reference on how to use Elastic IP:
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/elastic-ip-addresses-eip.html
回答3:
It depends on which IP you speak about:
Public IP: try to use an Elastic Ip, then you will not have this problem anymore. You can allocate an new one to your instance directly on AWS Console or programmatically. But if your are using an autoscaling-group you will have to do it on your user-data or cloud-init process.
Private IP: Unfortunately you cannot fix a private Ip address to an instance. The only way is to use DNS and in that case a private DNS zone for you VPC (https://docs.aws.amazon.com/fr_fr/vpc/latest/userguide/vpc-dns.html). No need to buy a domain in that case.
I would also recommend to use DNS on the first case with maybe a domain you have rather than using an IP address
来源:https://stackoverflow.com/questions/55414302/an-ip-address-of-ec2-instance-gets-changed-after-the-restart