Sub topic 15
IPv6 and dual-stack VPCs
This chapter assumes you have built a VPC with public and private subnets, route tables, an internet gateway, and a NAT gateway, as in the earlier chapters. You should also be comfortable with CIDR notation and security group rules.
What dual-stack means
A dual-stack VPC runs IPv4 and IPv6 side by side. Every subnet keeps its IPv4 CIDR block, and you add an IPv6 CIDR block next to it. Resources can then talk over either protocol.
IPv4 is always required for a VPC. You cannot create an IPv6-only VPC, though you can create IPv6-only subnets inside a dual-stack VPC.
Three facts shape everything else in this chapter:
- AWS assigns the VPC a /56 IPv6 block, and each subnet gets a /64 carved from it.
- IPv6 addresses from Amazon are globally unique and publicly routable. There is no private IPv6 range and no NAT.
- Because there is no NAT, a private IPv6 subnet needs a different tool to block inbound traffic.
Adding IPv6 to an existing VPC
The commands below reuse the variables from the earlier chapters. They request an Amazon-provided block for the VPC, then assign a /64 to the subnet.
aws ec2 associate-vpc-cidr-block \
--vpc-id "$VPC_ID" \
--amazon-provided-ipv6-cidr-block
VPC_IPV6=$(aws ec2 describe-vpcs --vpc-ids "$VPC_ID" \
--query 'Vpcs[0].Ipv6CidrBlockAssociationSet[0].Ipv6CidrBlock' --output text)
echo "$VPC_IPV6"
# Replace the last hextet of the VPC block with 00::/64 for the first subnet
SUBNET_IPV6="${VPC_IPV6%00::/56}00::/64"
aws ec2 associate-subnet-cidr-block \
--subnet-id "$PUBLIC_SUBNET_ID" \
--ipv6-cidr-block "$SUBNET_IPV6"If the block is not yet listed when you query it, wait a few seconds and run the query again. Association is not instant.
Routing IPv6 traffic
IPv6 needs its own routes. The IPv6 default route is ::/0. The public route table points it at the internet gateway, which handles both protocols.
aws ec2 create-route \
--route-table-id "$PUBLIC_RT_ID" \
--destination-ipv6-cidr-block ::/0 \
--gateway-id "$IGW_ID"A common mistake is to add the IPv6 block to the subnet and forget this route. The instance gets an address but cannot reach anything.
Private subnets and the egress-only internet gateway
A NAT gateway does not translate IPv6. To let a private subnet start outbound IPv6 connections while refusing inbound ones, use an egress-only internet gateway. It is stateful: it permits replies to connections the instance opened and drops connections started from outside.
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"Security groups and network ACLs
Security groups and network ACLs do not copy IPv4 rules to IPv6. You must add IPv6 rules yourself. A rule that allows 0.0.0.0/0 on port 443 does nothing for IPv6 clients. You need a second rule for ::/0.
Common errors
| Symptom | Likely cause |
|---|---|
| Instance has an IPv6 address but cannot reach the internet | Missing `::/0` route |
| IPv4 works, IPv6 clients time out | Security group or network ACL lacks an IPv6 rule |
| Private instance is reachable over IPv6 from outside | Route points `::/0` at the internet gateway instead of the egress-only gateway |
| Subnet cannot get an IPv6 CIDR | The VPC has no IPv6 block yet |
Exercise
A private subnet in a dual-stack VPC routes 0.0.0.0/0 to a NAT gateway. Instances there have IPv6 addresses but cannot make outbound IPv6 connections. What do you add, and why is NAT not the answer?
Show answer
Create an egress-only internet gateway and add a ::/0 route to it in the private route table. NAT gateways handle IPv4 only, and IPv6 addresses are globally unique, so no translation is needed. The egress-only gateway allows outbound connections and replies while blocking inbound connections started from the internet.