An IP address of EC2 instance gets changed after the restart
Asked Answered
L

7

38

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?

Lure answered 29/3, 2019 at 9:32 Comment(1)
Perhaps you should refer this: Using static IP address with Amazon EC2Sexpot
P
72

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.

Polymorphism answered 29/3, 2019 at 9:42 Comment(6)
Note that if you're communicating over public ip you are charged for traffic. Comms over the private ip is free.Goda
@RahulGoti, My public IP is already changed. And I associate the elastic IP to my instance as well. But the problem is the Nginx configuration is not affecting because the IP address is changed. Is there any way that it can work in the newly generated IP?Sheath
The answers here: #54129271 claim that private IPs are always the same and never changeMessage
@JaynaTanawala maybe dynamic dns can solve your problem...if your public IP change dynamic dns update the new IP with your domain name...check this linkStandardize
What do you mean by "launch your instance in VPC"? EC2 are always launched in VPC, aren't they?Montenegro
docs.aws.amazon.com/AWSEC2/latest/UserGuide/… private IPv4 address, regardless of whether it is a primary or secondary address, remains associated with the network interface when the instance is stopped and started, or hibernated and started, and is released when the instance is terminated.Telesis
C
9

Elastic IP has its limitations.

If you have reached the maximum number of Elastic IP addresses in a region, and all you want is a constant way to connect to an EC2 instance, I would recommend using a route53 record instead of using IP address.

I create a route53 record that points to the IP address of my EC2 instance. The record doesn't get changed when the EC2 is stopped.

And the way to keep the record pointing to the address of the EC2 is by running a script that changes the route53 record when the EC2 launches.

Here's the user data of my EC2:

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash

# get the public ip address
# Ref: https://mcmap.net/q/181431/-get-public-ip-address-on-current-ec2-instance
export public_ip=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)

cat <<EOF > input.json
{
  "Comment": "optional comment about the changes in this change batch request",
  "Changes": [
    {
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "my-domain.my-company.com",
        "Type": "A",
        "TTL": 300,
        "ResourceRecords": [
          {
            "Value": "${public_ip}"
          }
        ]
      }
    }
  ]
}
EOF

# change route53 record 
/usr/bin/aws route53 change-resource-record-sets \
  --hosted-zone-id <hosted_zone_of_my-company.com> \
  --change-batch file://input.json
--//

Here I use my-domain.my-company.com as the route53 record for my EC2.

Also remember to replace <hosted_zone_of_my-company.com> with the ID of your hosted zone.

By using this method, you get a route53 record that points to your EC2 instance. And the record does not change when you stop and start the EC2. So you can always use the route53 record to connect to your EC2.

Remember to assign an IAM role that has route53 permissions to the EC2 instance so that you can run the user data without errors.

And remember that the user data I provided is intended for use with Amazon Linux 2, and the commands may not work for other Linux distributions.

If curl http://169.254.169.254/latest/meta-data/public-ipv4 doesn't work for your EC2. You might try curl http://checkip.amazonaws.com.

Crucial answered 16/6, 2020 at 13:16 Comment(3)
the answers under this question: #54129271 claim that private address of EC2 instance never changes. If so, what's the point of using elastic IP or route53 ? Can't we just use the same private IP to connect to the instance ?Message
@Message you can connect to the EC2 instance with public ip from outside that VPC. I am not sure that this is the case here. the question is about connecting the instance from outside world to that specific EC2 instance with public ip.Role
@DipeshRaichana hm, in fact it's unclear if OP wanted a public access or private - he posted his question and disappeared, lolMessage
I
7

Yes, you can.

Go to Elastic IP and Create one.

Then select the newly created Elastic IP and Select Associate Elastic IP and select the Instance ID to which you want to associate it with.

That's it. The IP will now be assigned even if you reboot your EC2.

Do note that AWS charges $0.005/hr for each Elastic IP, And $0.10 per Elastic IP address remap for additional remaps over 100 per month

Innermost answered 29/2, 2020 at 6:25 Comment(0)
O
2

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

Outwards answered 29/3, 2019 at 9:48 Comment(0)
L
0

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

Lambert answered 29/3, 2019 at 9:40 Comment(0)
M
0

There are two easy ways to solve this problem:

1.Use Elastic IP. However, I don't recommend this method. Last I checked, if your instance is not running, elastic IP will cost you. This I think is to ensure you do not reserve an IP not to use it.

2.Use load balancers like application load balancer. Connect your load balancer to your EC2 instance. This way, even if you start and stop your instance, it does not matter since you are using the public DNS of your application load balancer. You can also tie your load balancer to route 53 to get a better DNS.

Michaeu answered 26/7, 2022 at 10:35 Comment(0)
O
0

For the recent documentation on EC2 from AWS you can see the following:

  1. Private IP on EC2

A private IPv4 address, regardless of whether it is a primary or secondary address, remains associated with the network interface when the instance is stopped and started, or hibernated and started, and is released when the instance is terminated.

  1. Public IP on EC2

If you require a persistent public IP address that can be associated to and from instances as you require, use an Elastic IP address instead.

Then the problem highlight for Public IP with DNS

If you use dynamic DNS to map an existing DNS name to a new instance's public IP address, it might take up to 24 hours for the IP address to propagate through the Internet. As a result, new instances might not receive traffic while terminated instances continue to receive requests. To solve this problem, use an Elastic IP address. You can allocate your own Elastic IP address, and associate it with your instance.

Link to main documentation from AWS: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html#concepts-private-addresses

Olomouc answered 8/11, 2023 at 22:7 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.