Daniel Haviv
Daniel Haviv

Reputation: 1036

AWS EC2: Keeping a public IP of an already running instance


I have some machines provisioned which I want to take down but I want them to keep the public IP address they were assigned so next time I start them up they will stay at the same address. I'm aware of the notion of Elastic IP addresses but I don't want to re-allocate ip addresses and then remap my environment.
is it possible to keep the current IP addresses ?



Thanks, Daniel

Upvotes: 3

Views: 5035

Answers (3)

Brian
Brian

Reputation: 13571

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.

Upvotes: 0

Michael - sqlbot
Michael - sqlbot

Reputation: 178974

This is not possible.

A public IP address is assigned to your instance from Amazon's pool of public IP addresses, and is not associated with your AWS account.

When a public IP address is disassociated from your instance, it is released back into the public IP address pool, and you cannot reuse it.

If you require a persistent public IP address that can be associated to and from instances as you require, use an Elastic IP address (EIP) instead. You can allocate your own EIP, and associate it to your instance. For more information, see Elastic IP Addresses (EIP).

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html

Note that there is no charge for elastic IP addresses unless you associate more than one elastic IP to the same instance (in which case, only the first one provided at no charge) or you allcate elastic IPs to your account but leave them attached to a stopped instance or to no instance at all (e.g, for later reuse) or you heavily remap an IP (over 100 remaps per month per IP) but these charges are very small.

Upvotes: 6

Mathias
Mathias

Reputation: 1

i don't think so.. if it would be possible amazon will not make money with buying elastic ip addresses...

Upvotes: -3

Related Questions