Private IP of a running or stopped instance would not change. You will loose it once the instance is terminated.
If you want to retain a private IP for an EC2 instance even after termination, you could do this -
- Create a custom ENI (network interface) with a custom IPV4 address within the CIDR range of the subnet (e.g. us-east-1a) you want to launch the EC2 instance.
- Launch the ec2 instance in the same subnet (e.g. us-east-1a)
- Attach the custom ENI to your new EC2 instance (From Instances page of AWS Console - Actions --> Networking --> Attach Network Interface)
You would see 2 private ips for the instance after this.
Please keep in mind this will lock your EC2 instance to a specific Availability Zone. Also, make sure you understand the implications of doing this after considering the alternate options.
An alternate to this is to use an Elastic IP, which is a fixed public ip address that you can attach to any of your instances.
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html
The private IP address of an Amazon EC2 instance will never change while an instance is running or stopped. However, EC2 instances can have multiple IP addresses. (For more: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/MultipleIP.html). Pls, remember, that there is no way to remove the Primary IP address from any EC2 instances. Furthermore, two EC2 instances cannot have the same IP. only one IP can be assigned to one only one EC2 instance. (This is only for the private IP address, not the public IP address).
Apart from that, for public IP use Elastic IP address. For more: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/elastic-ip-addresses-eip.html
Relevant questions
How can i change the PTR(Reversr DNS) for lightsail static IP ?
asked 7 months agoconvert Dynamic IP address to Static IP
asked 2 years agoPrivate Static IP for EC2
Accepted Answerasked 11 days agoPrivate IP address change, effect(s)?
asked 2 years agoLightsail static ip issue
asked 17 days agoPrivate API Gateway static IP
asked 2 months agoCan't ssh to EC2 after assigning an additional public ip(elastic ip)
asked 7 months agoStatic IP address for EC2 Instance
asked 3 years agoAny way to specify the private IP when creating an instance?
asked 2 years agoRestoring an EC2 Instance - the Public IP is missing
Accepted Answerasked 2 months ago
You need to create a Elastic ip and associate it to the EC2
https://aws.amazon.com/premiumsupport/knowledge-center/intro-elastic-ip-addresses/#:~:text=An%20Elastic%20IP%20address%20is,Allocate%20an%20Elastic%20IP%20address.