Can I remove the public IP on my instance without terminating it? - amazon-web-services

I have several instances on a vpc that communicate with each other through their private ips. Each instance was launched sometime ago and assigned a random public IP which is not used for anything. Since anything that is public presents a vulnerability and they are not used at all, I would like to removed them.
Is it possible to Remove public Ip Addresses without terminating the instance?

Yes you can remove a public IP address from an instance.
This solution applies when your instance was launched in a subnet with the "auto-assign public IP" setting enabled.
This makes AWS add a public IP address that seems hard to remove.
Notes:
If you change the subnet auto-assign setting and stop and start the instance,
this does not work, it has no effect. The public IP remains.
If you got your public IP by associating an elastic IP then it's
a different case and easy to fix; all you need to do is right-click on
the instance or on the Elastic IP, and choose "Networking", "Disassociate
Elastic IP Address")
There is a voted-down solution here that seems to work at first, but the public IP comes back (as a different address) when you start the instance up again.
Solution: (detailed steps below)
You need to first add another Network Interface, (so there are two or more N.Is), then you can release the public address by adding and removing an
elastic IP.
Here is a detailed step-by-step guide:
Create a new network interface.
Attach the new network interface to your instance. Now there will be two
or more network interfaces attached to your instance. This is important.
There MUST be two or more for this to work.
Create a new Elastic IP (in the EC2 console).
Right-click on the new EIP and associate it to the instance whose public IP
you want to remove. The original public IP will be replaced by the new one.
Now do the reverse of step 4, disassociate the EIP you have just added.
At this point, right-click on the instance and select "Networking", "Manage
IP addresses", you will see there are no public IPs on you instance any
more. at this point. You must refresh the instances view otherwise you
will not see this.
The public IP is now gone for good.
Even if you stop and start the instance, it will not return.
Tidy up: delete the elastic IP you created earlier. Don't delete the new
extra Network Interface. This has to stay in place. If you remove the new /
second NI then the public IP will come back the next time you stop and
start the instance.
I know this sounds a bit strange, but it is in the AWS docs here and I've tested it and it works.
The relevant part from the AWS documentation on that page, is this: "If the public IP address of your instance in a VPC has been released, it will not receive a new one if there is more than one network interface attached to your instance."

Nope that is not possible without terminating the instance. If it was Elastic Ip then it would have worked.
The only option is Take AMI of the instance, terminate the instance and launch the AMI in the VPC without enabling Public Ip address for that instance.
Once you terminate the instance with that private IP you can assign that private ip again to a new Instance you created from the Image. (As long as it's in the same VPC and subnet) This way you don't need to update the private IPs off all the "instances on a vpc that communicate with each other through their private ips"

Steps worked for me:
Disable auto assign public IP setting on vpc subnet.
Stop the ec2 instance.
Open Network Interface Manage IP address settings and assign a secondary private IP.
Open Elastic IPs and associate an elastic IP to the ec2 with secondary private IP.
Start the Ec2 Instace.
Open Network Interface Manage IP address settings and unassign the secondary IP.
Done, now you can release the previously associated elastic IP.

Below worked
create temp-priv-ni same subnet private interface use default security group
eni-xx-temp-priv-ni
allocate new public elastic ip
associate new public elastic ip to instance
attach eni-xx-temp-priv-ni to same instance
check instance has two private ips and new public elastic ip and past public eip is gone
then
disassociate public eip
go to instance network interfaces and detach eni-xx-temp-priv-ni
server will be now with same private ip and its public ip gone
the new public ip and new private ips can be deleted

It is a very old thread but the easiest way of removing a public ip associated via VPC settings is:
create an elastic IP
assign the elastic ip to the host owning
the public ip that you want to release (the ip is released at this
step)
disassociate the ip address from the elasic ip management
screen.

Related

Attach public ip address to an AWS EC2 instance having multiple interfaces

I have an EC2 instance having 3 interfaces attached to it.
I want to attach a public IP address for management but it looks like AWS does not allow auto-assign public IP address and asks to attach an elastic IP address.
Is there any way to add an auto-assign public IP address to an EC2 instance having multiple interfaces for instance management (not elastic ip)?
I just tried it myself and, after adding multiple Elastic Network Interfaces, it displayed this message:
We can no longer assign a public IP address to your instance
The auto-assign public IP address feature for this instance is disabled because you specified multiple network interfaces. Public IPs can only be assigned to instances with one network interface. To re-enable the auto-assign public IP address feature, please specify only the eth0 network interface.

How to Detect if a Powered Down EC2 Instance will have an AWS Public Pool IP When Powered Back on

I am writing a Powershell script to locate all EC2 Instances with a Public IP. I know how to get Elastic IP and Public Pool IP Associations in the NetworkInterfaces. However, when EC2 Instances that have been configured for a Public Pool IP (NOT an EIP) are powered down, the Public IP is released back into the pool and the Association is deleted from the NetworkInterface. I can't seem to find any flag or other configuration anywhere that would indicate that when the EC2 is powered back on it would request a new Public Pool IP. I need to identify powered down EC2 Instances that may come up with Public IP's for a security report without having to power them up to find out.
For example, this EC2 has a Public Pool IP when it is powered up. However, when it is powered down, as it is now, the Association object has been removed along with the IP. Is ther not some configuration somewhere that will indicate it will procure a new Public Pool IP upon powering up?
PS X:\projects\GACM\cloudsecurity> (get-ec2instance -instanceid i-99999999999999999 -region us-east-1).instances.networkinterfaces|select *
Association :
Attachment : Amazon.EC2.Model.InstanceNetworkInterfaceAttachment
Description : Primary network interface
Groups : {launch-wizard-4}
Ipv6Addresses : {}
...
The only way you can determine if an instance will have a pooled public ip address is by analyzing the CloudTrail logs.
On instance creation, a log entry is made. requestParameters in the log has a JSON Object called networkInterfaceSet which has an array of items, these items have the key associatePublicIpAddress which is either true or false.
At this time you can't determine if an EC2 will have a public IP Address when it is in the stopped state. Unless you have linked an Elastic IP to the instance.
When an EC2 instance is launched it can either inherit the allocation of a public IP address from the subnet settings or you can set it as public IP enabled, or disabled. This is recorded in the cloud trail log.
Currently there is no way to change that behavior or view the behavior other than the cloud trail log.
For example, if the subnet has public ip enabled, and when launching an EC2 instance and you explicitly disable it then the only way your instance can get a public IP address is to either recreate the instance from a snapshot, or assign an Elastic IP. If the instance is initially launched with a public IP, there is no way to remove the public IP.
Neither describe-network-interfaces or describe-instances provides any metadata pertaining to enabled or disabled public IP address excluding elastic IPs.
As you mentioned the only way to determine if an instance will have a public IP is to start the instance and view the running instance metadata.
The Amazon EC2 instance IP addressing documentation states:
You can control whether your instance receives a public IP address as follows:
Modifying the public IP addressing attribute of your subnet. For more information, see Modifying the public IPv4 addressing attribute for your subnet in the Amazon VPC User Guide.
Enabling or disabling the public IP addressing feature during launch, which overrides the subnet's public IP addressing attribute. For more information, see Assigning a public IPv4 address during instance launch.
A public IP address is assigned to your instance from Amazon's pool of public IPv4 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 IPv4 address pool, and you cannot reuse it.
You cannot manually associate or disassociate a public IP 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. Your stopped or hibernated instance receives a new public IP address when it is started.
We release your instance's public IP address when you associate an Elastic IP address with it. When you disassociate the Elastic IP address from your instance, it receives a new public IP address.
If the public IP address of your instance in a VPC has been released, it will not receive a new one if there is more than one network interface attached to your instance.
If your instance's public IP address is released while it has a secondary private IP address that is associated with an Elastic IP address, the instance does not receive a new public IP address.
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.
Experiment
I created an EC2 instance in a subnet where the assignment of a public IP address was enabled. I set the EC2 instance to assign a public IP address on launch. Changing the subnet auto-assign value had no affect. I repeated the process this time allowing the EC2 instance to be created with the setting of the subnet. I disabled auto assign within the subnet. The instance was created without a public IP as expected. I then enabled auto assign on the subnet, and stopped and started the instance. No public IP address was assigned.
Thus based on the documentation above and my experiment. Allocation of a pooled public IP address is decided at EC2 instance launch. This cannot be changed. I ran [describe-network-interfaces, describe-instances] CLI commands to get details on the ENI, and no metadata provides a flag to indicate that a Public IP address will be assigned when the instance is started. The only place I could find any information was in the CloudTrail log.
If you are using AWS best practice, EC2 instances should be started using CloudFormation. Here you will know if the EC2 instance will have a public IP.
Alternatively you do not allow the assigning of pooled public IP addresses by using service control policies or policies assigned to users. Then use alternative means to expose the machines either using elastic IP addresses or Load Balancers.
I think it is important to revisit your use case, to understand what you are trying to achieve.

AWS elastic IP vs public IP

I am new to AWS and tried to create an EC2 instance.
I have a domain and ready to modify the A record to the associated EC2 instance.
I found an article that said an elastic IP is required for associating a production domain.
But AWS provides a public IP and it is accessible on the public internet too (i know it changes after restart, just assuming its okay to modify the A record after the machine is restarted - actually it is not restart very often).
In this case, is it a must that to assign an elastic IP to the instance (this instance contains no load balancing, it is only a simple single instance)?
If yes, why is it necessary?
An Elastic IP (EIP) is not necessary provided that you understand the limitations of public IPs. You may not reboot your instance, but AWS might for any number of reasons. This means that the public IP address could change when you are not expecting it.
When an EIP is assigned to a running EC2 instance, there are no charges for the EIP e.g. it is free. Therefore why go thru the hassle of needing to monitor your public IP address.

AWS Elastic IP Unable to Send Traffic

I have an EC2 instance that had a dynamic public IP. Due to changing requirements I assigned a new Elastic IP. I shut down the instance and restarted it. The console now shows the new IP and no longer the old, dynamic one.
Problem is now I can not ping nor curl/wget to the public Internet any longer from this instance. I do have an outgoing rule for port 80 and 443 setup. I also allow ICMP in both directions.
I have a load balancer setup in another instance. That instance can access the problematic instance over the private IP no problem.
I did notice that while all my other instances list their public IP (dynamic or Elastic) the instance in question lists "ec2-x-x-x-x.compute-1.amazonaws.com". Not sure what to make of that...
What could be my issue?
If you are not seeing the public IP show up in the console for the system, but instead are seeing the hostname as you mention, it means that your public IP is NOT assigned to the instance. Suggest you go to the Elastic IPs section of the console and re-attach the EIP to the instance.
Note, if you are not running in a VPC, when you reboot the instance, the EIP will drop off the instance.

Transferring an IP From One Server to Another in AWS EC2 - Elastic IP

I have a server that is up and running with an auto-assigned public IP address. This IP address was not created with Elastic IP. Is there anyway to transfer this IP address onto a new instance in my account?
I'm afraid you can't.
The auto assigned public IPs are not yours to deassociate and reassociate.
You can't tell them "I changed my mind, I want to use this IP as if it was an Elastic IP". It doesn't work like that. You can't really pick your elastic IP. You just ask for one, and then operate with whatever IP they gave you.
You cannot manually associate or disassociate a non-elastic IP address from your instance. Instead, in certain cases, aws release the public IP address from your instance, or assign it a new one for below cases:
1. They release the public IP address for your instance when it's stopped or terminated. Your stopped instance receives a new public IP address when it's restarted.
They release the public IP address for your instance when you associate an Elastic IP address (EIP) with your instance, or when you associate an EIP with the primary network interface (eth0) of your instance in a VPC. When you disassociate the EIP from your instance, it receives a new public IP address.
If the public IP address of your instance in a VPC has been released, it will not receive a new one if there is more than one network interface attached to your instance.
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html#concepts-public-addresses
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, please see Elastic IP Addresses (EIP).
Easy, setup a HTTP or TCP transparant proxy, with HAProxy or Nginx.