Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An IP address of EC2 instance gets changed after the restart

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?

like image 263
S_K Avatar asked Mar 29 '19 09:03

S_K


People also ask

Do AWS EC2 IP addresses change?

The private IP address of an Amazon EC2 instance will never change. It will not change while an instance is running. It will not change while an instance is stopped. You cannot change a private IP address.

What happens when you restart an EC2 instance?

When you reboot an instance, it keeps its public DNS name (IPv4), private and public IPv4 address, IPv6 address (if applicable), and any data on its instance store volumes. Rebooting an instance doesn't start a new instance billing period (with a minimum one-minute charge), unlike stopping and starting your instance.

Does Elastic IP change when instance is stopped?

All instances retain their associated Elastic IP addresses when stopped.

Can you lose the public IP address associated with your EC2 instance?

You cannot manually associate or disassociate a public IP (IPv4) address from your instance. Instead, in certain cases, we release the public IP address from your instance, or assign it a new one: We release your instance's public IP address when it is stopped, hibernated, or terminated.


2 Answers

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.

like image 74
Rahul Goti Avatar answered Oct 08 '22 07:10

Rahul Goti


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://stackoverflow.com/questions/38679346/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.con> \
  --change-batch file://input.json >
--//

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

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.

like image 44
Brian Avatar answered Oct 08 '22 05:10

Brian