AWS EC2 IPv6 migration

My notes on migrating to IPv6
February 07, 2024
AWS has begun charging approximately $4 per month for each public IPv4 address in AWS accounts. This has led to an additional few hundred dollars in AWS expenses in our case. So, I spent a day to configuring most of our EC2 instances to use IPv6.
A Few Points to Remember Before Transitioning Your EC2 Instances to IPv6-Only:
  • IPv6-only instances cannot connect to IPv4-only websites (usually). For instance, if you call an API with a domain pointing to an IPv4 address, it will fail. This situation may apply to many third-party SDKs/APIs, so exercise caution before proceeding.
  • Your ISP must support IPv6 to access IPv6 addresses. Mine did not, but fortunately, Cloudflare WARP (a free VPN) does.
  • Even after completing all these steps, an additional step is required in Ubuntu 16. Refer to the end of this article for details.

Step 1:

Follow this AWS Guide for a one-time setup per region.

Step 2

  • Navigate to the EC2 dashboard, select the instance, then under Actions, click Networking, and then Manage IP addresses. Next, under IPv6 addresses, click Assign new IP address. If you do not see this option, carefully review if you followed Step 1 correctly.
At this point, your EC2 instance will have an associated IPv6 address. However, your IPv4 address will still be present. Let's remove it.

Step 3

  • Allocate an Elastic IP address and attach it to the instance (if one is not already associated).
Before we proceed to the next step, there are a few things to understand:
  • If we add an Elastic IP to our EC2 instance and then remove it, the instance loses the IPv4 address. However, if we stop the instance and then start it again, a public IPv4 is automatically attached to the instance.
  • An IP address (either v4 or v6) does not technically attach directly to an EC2 instance; instead, it gets associated with an Elastic Network Interface, and that network interface gets associated with the instance.
  • We can add multiple Elastic Network Interfaces to an EC2 instance.
  • If multiple Elastic Network Interfaces are attached to an EC2 instance, it does not know which one to assign the public IPv4 address to, so it does not attach any IPv4 address at all. This is what we want.

Step 4

  • Go to the EC2 dashboard, then select Elastic Network Interface from the left sidebar.
  • Add a new Elastic Network Interface without assigning any IPv4 or IPv6 address.
  • Select the newly created Elastic Network Interface, click Actions, and attach it to the EC2 instance.

Step 5

  • Dissociate and release the Elastic IP Address attached in Step 2.
Done!
Repeat steps 2 - 5 for all of your instances.

Extra step for Ubuntu 16

  • SSH into your EC2 instance.
  • Open /etc/network/interfaces in an editor (nano/vim).
  • Add the following line at the end of the file and save it:
iface eth0 inet6 dhcp
  • Restart the networking service:
sudo service networking restart