Sub topic 15
IPv6 and dual-stack VPCs
This chapter assumes you have built the IPv4 VPC from the earlier chapters: a VPC, a public and a private subnet, route tables, an internet gateway, a NAT gateway, and security groups. Here you add IPv6 to that VPC and learn where IPv6 behaves differently from IPv4.
Why add IPv6
A dual-stack VPC runs IPv4 and IPv6 side by side. Every resource keeps its private IPv4 address and can also get an IPv6 address. There are three common reasons to do this:
- Address space. IPv6 removes the need for careful CIDR planning and for avoiding overlaps between VPCs.
- Cost. Since February 2024, AWS charges for every public IPv4 address, and a NAT gateway has an hourly fee plus a per-GB processing fee. IPv6 needs neither a public address charge nor a NAT gateway.
- Client reach. Some mobile networks and partners are IPv6-only or prefer IPv6.
You cannot turn IPv4 off in a VPC. Every VPC has an IPv4 CIDR block. IPv6 is always an addition to it.
How IPv6 differs in a VPC
IPv6 addresses in a VPC differ from IPv4 in three ways:
- Sizes are fixed. An Amazon-provided VPC IPv6 block is a /56. Each dual-stack subnet gets a /64 from it, so one VPC can hold 256 IPv6 subnets.
- No private range and no NAT. Every IPv6 address is globally unique. Whether an address is reachable from the internet depends on routes, security groups, and network ACLs, not on the address itself.
- Separate rules everywhere. Routes, security group rules, and network ACL entries for IPv6 are separate from the IPv4 ones. An IPv4 rule never applies to IPv6 traffic.
| Aspect | IPv4 | IPv6 |
|---|---|---|
| VPC block | /16 to /28, you choose | /56 from Amazon (or BYOIP or IPAM) |
| Subnet size | /16 to /28 | /64 |
| Outbound-only access | NAT gateway | Egress-only internet gateway |
| Default route | `0.0.0.0/0` | `::/0` |
| Public by default | No (private ranges) | Addresses are global, so routing decides |
| Cost of outbound-only path | NAT gateway hourly and per-GB fees | No hourly fee for the egress-only gateway |
Add IPv6 to the VPC and subnets
First ask AWS for an IPv6 block for the VPC. Then read it back and derive one /64 per subnet. The script assumes the variables from the earlier chapters are still set.
aws ec2 associate-vpc-cidr-block \
--vpc-id "$VPC_ID" \
--amazon-provided-ipv6-cidr-block
V6_BLOCK=$(aws ec2 describe-vpcs --vpc-ids "$VPC_ID" \
--query 'Vpcs[0].Ipv6CidrBlockAssociationSet[0].Ipv6CidrBlock' --output text)
echo "VPC IPv6 block: $V6_BLOCK"
# 2600:1f18:1234:ab00::/56 -> 2600:1f18:1234:ab
V6_PREFIX=${V6_BLOCK%00::/56}
aws ec2 associate-subnet-cidr-block \
--subnet-id "$PUBLIC_SUBNET_ID" --ipv6-cidr-block "${V6_PREFIX}01::/64"
aws ec2 associate-subnet-cidr-block \
--subnet-id "$PRIVATE_SUBNET_ID" --ipv6-cidr-block "${V6_PREFIX}02::/64"
aws ec2 modify-subnet-attribute \
--subnet-id "$PUBLIC_SUBNET_ID" --assign-ipv6-address-on-creation
aws ec2 modify-subnet-attribute \
--subnet-id "$PRIVATE_SUBNET_ID" --assign-ipv6-address-on-creationThe last two commands make new network interfaces in those subnets receive an IPv6 address automatically. Existing instances do not get one. For those, assign an address to the network interface with aws ec2 assign-ipv6-addresses --network-interface-id <eni-id> --ipv6-address-count 1.
If the describe-vpcs output shows an empty value, the association is still in progress. Wait a few seconds and run it again.
Route IPv6 traffic
Routes decide which subnet is public for IPv6, just as they do for IPv4. The public route table sends ::/0 to the internet gateway. The same internet gateway handles both IP versions, so you do not create a second one.
The private subnet cannot use a NAT gateway for IPv6. Use an egress-only internet gateway instead. It allows instances to start outbound IPv6 connections and blocks connections that start from the internet. It is stateful, so replies to your outbound requests come back.
aws ec2 create-route \
--route-table-id "$PUBLIC_RT_ID" \
--destination-ipv6-cidr-block ::/0 \
--gateway-id "$IGW_ID"
EIGW_ID=$(aws ec2 create-egress-only-internet-gateway \
--vpc-id "$VPC_ID" \
--query EgressOnlyInternetGateway.EgressOnlyInternetGatewayId --output text)
aws ec2 create-route \
--route-table-id "$PRIVATE_RT_ID" \
--destination-ipv6-cidr-block ::/0 \
--egress-only-internet-gateway-id "$EIGW_ID"Check the result with aws ec2 describe-route-tables --route-table-ids "$PRIVATE_RT_ID". You should see two default routes, one per IP version.
Firewall rules for IPv6
Security groups and network ACLs both need IPv6 entries.
A security group rule that names 0.0.0.0/0 does not match IPv6 traffic. To allow HTTPS from any IPv6 client, add a rule with an IPv6 range:
aws ec2 authorize-security-group-ingress \
--group-id "$APP_SG" \
--ip-permissions 'IpProtocol=tcp,FromPort=443,ToPort=443,Ipv6Ranges=[{CidrIpv6=::/0}]'Rules that reference another security group, like the database rule from the security group chapter, work for both IP versions. Prefer these over CIDR ranges where you can, because you then write one rule instead of two.
Network ACLs are stateless, so IPv6 needs entries in both directions, including the ephemeral port range for return traffic:
NACL_ID=$(aws ec2 describe-network-acls \
--filters Name=association.subnet-id,Values="$PUBLIC_SUBNET_ID" \
--query 'NetworkAcls[0].NetworkAclId' --output text)
aws ec2 create-network-acl-entry --network-acl-id "$NACL_ID" \
--ingress --rule-number 120 --protocol tcp --port-range From=443,To=443 \
--ipv6-cidr-block ::/0 --rule-action allow
aws ec2 create-network-acl-entry --network-acl-id "$NACL_ID" \
--egress --rule-number 120 --protocol tcp --port-range From=1024,To=65535 \
--ipv6-cidr-block ::/0 --rule-action allowDNS and addressing in practice
Dual-stack clients choose between IPv4 and IPv6 by DNS answers. A name that has both an A record and an AAAA record is reachable either way, and most clients try IPv6 first. If you publish an AAAA record for a service, make sure its security group, network ACL, and route all allow IPv6. Otherwise some clients hit a broken path before falling back to IPv4.
Instances built on the Nitro system can reach the Amazon-provided DNS resolver over IPv6 at fd00:ec2::253. The resolver can return AAAA records to IPv4-only clients too, because the DNS query transport and the address family of the answer are independent.
VPC Flow Logs record IPv6 addresses in the srcaddr and dstaddr fields. Any filter or query you wrote that assumed dotted IPv4 addresses needs to handle colon-separated addresses as well.
IPv6-only subnets and NAT64
You can also create a subnet with no IPv4 at all by passing --ipv6-native to create-subnet. Instances there have only IPv6 addresses. To let them reach IPv4-only services, enable DNS64 on the subnet and send the NAT64 prefix to a NAT gateway:
aws ec2 modify-subnet-attribute --subnet-id "$IPV6_ONLY_SUBNET_ID" --enable-dns64
aws ec2 create-route \
--route-table-id "$IPV6_ONLY_RT_ID" \
--destination-ipv6-cidr-block 64:ff9b::/96 \
--nat-gateway-id "$NAT_ID"DNS64 synthesizes an IPv6 address inside 64:ff9b::/96 for hosts that only have an A record. The NAT gateway translates that traffic to IPv4. Use this setup only when your workloads and dependencies are tested on IPv6, since IPv4-only literals in code or config will fail there.
Common errors
- Instance has no IPv6 address. The subnet had no IPv6 block when the instance launched. Assign an address to the network interface.
- Outbound IPv6 times out from a private subnet. The route for
::/0is missing, or it points to a NAT gateway. NAT gateways handle IPv6 only for the NAT64 prefix. - Inbound IPv6 connection to a private instance works from the internet. A
::/0route to the internet gateway made the subnet public for IPv6. Use the egress-only gateway for private tiers, and keep security groups restrictive. - Rules copied from IPv4 do not apply. Add the IPv6 equivalent to security groups and network ACLs.
- Application listens only on
0.0.0.0. Bind to::(or both) so the service accepts IPv6 connections.
Exercise
An instance in a private dual-stack subnet can download packages over IPv4 through the NAT gateway, but IPv6 downloads fail. The subnet has an IPv6 block and the instance has an IPv6 address. Name the two places you would look first and what you would expect to find.
Show answer
First check the private route table for a ::/0 route. It should point to an egress-only internet gateway, and a missing route or a NAT gateway target explains the failure. Then check the outbound rules of the instance's security group and the subnet's network ACL for IPv6 entries. A custom network ACL needs explicit IPv6 allow entries in both directions, including the ephemeral return ports.