Notes to Self

Alex Sokolsky's Notes on Computers and Programming

Remove a public IP from an EC2 instance

Before you proceed:

The rest assumes the public IP is NOT elastic and that even short instance downtime is not acceptable.

Reference: Amazon EC2 instance IP addressing

Summary

The described below method involves:

By the end, the instance is left with two network interfaces and no public IP. If the second network interface is removed, the public IP will come back the next time the instance is restarted.

0. Get the instance details

Assumptions:

describe-instances

Inputs:

aws ec2 describe-instances --instance-ids i-aaaaaaa

outputs:

1. Create a new network interface

create-network-interface

inputs:

aws ec2 create-network-interface --subnet-id subnet-xxx --groups sg-aaaaa sg-bbbbb

outputs:

verify no public IP is associated

2. Attach the network interface to the ec2 instance

attach-network-interface

inputs:

aws ec2 attach-network-interface --network-interface-id eni-aaaaa --instance-id i-aaaa --device-index 1

outputs:

3. Create a new (public) Elastic IP

allocate-address

Inputs:

aws ec2 allocate-address --domain vpc --network-border-group us-west-2

outputs:

4. Associate the new elastic IP with the old network interface

associate-address

Inputs:

aws ec2 associate-address --allocation-id eipalloc-newnew --network-interface-id eni-oldold

outputs:

The original public IP will be replaced by the new one.

5. Disassociate the EIP you have just added.

disassociate-address

Inputs:

aws ec2 disassociate-address --association-id eipassoc-aaaaa

6. Delete the elastic IP you created

release-address

Inputs:

aws ec2 release-address --allocation-id eipalloc-aaaaa

7. Attention!

Do not 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.