DEV Community

Praveen Kumar K
Praveen Kumar K

Posted on

How to setup IPv6 Only EC2 Instance in AWS

i. Create a VPC with an IPv4 CIDR block and an Amazon-provided IPv6 CIDR block:

VPC_ID=$(aws ec2 create-vpc \
    --cidr-block 10.0.0.0/24 \
    --amazon-provided-ipv6-cidr-block \
    --query 'Vpc.VpcId' \
    --output text)
Enter fullscreen mode Exit fullscreen mode

ii. Retrieve the IPv6 CIDR block and IPv4 CIDR block for the VPC:

Ipv6CidrBlock=$(aws ec2 describe-vpcs --vpc-ids $VPC_ID --query 'Vpcs[*].Ipv6CidrBlockAssociationSet[*].Ipv6CidrBlock' --output text)
Enter fullscreen mode Exit fullscreen mode
Ipv6CidrBlockforDualStackSubnet="${Ipv6CidrBlock%/*}/64"
Enter fullscreen mode Exit fullscreen mode
Ipv4CidrBlock=$(aws ec2 describe-vpcs --vpc-ids $VPC_ID --query 'Vpcs[*].CidrBlockAssociationSet[*].CidrBlock' --output text)
Enter fullscreen mode Exit fullscreen mode

iii. Create a dual-stack subnet:

dualStackSubnetID=$(aws ec2 create-subnet \
    --vpc-id $VPC_ID \
    --cidr-block $Ipv4CidrBlock \
    --ipv6-cidr-block $Ipv6CidrBlockforDualStackSubnet)
dualStackSubnetID=$(echo $dualStackSubnetID | jq -r '.Subnet.SubnetId')
Enter fullscreen mode Exit fullscreen mode

vi. Create an internet gateway and attach it to the VPC:

internet_gateway_id=$(aws ec2 create-internet-gateway --query 'InternetGateway.InternetGatewayId' --output text)
Enter fullscreen mode Exit fullscreen mode
aws ec2 attach-internet-gateway \
    --internet-gateway-id $internet_gateway_id \
    --vpc-id $VPC_ID
Enter fullscreen mode Exit fullscreen mode

v. Retrieve the default route table of the newly created VPC and add routes for IPv4 and IPv6 traffic:

route_table_id=$(aws ec2 describe-route-tables --filters "Name=vpc-id,Values=$VPC_ID" "Name=association.main,Values=true" --query "RouteTables[*].RouteTableId" --output text)
Enter fullscreen mode Exit fullscreen mode
aws ec2 create-route --route-table-id $route_table_id --destination-cidr-block 0.0.0.0/0 --gateway-id $internet_gateway_id
Enter fullscreen mode Exit fullscreen mode
aws ec2 create-route --route-table-id $route_table_id --destination-ipv6-cidr-block ::/0 --gateway-id $internet_gateway_id
Enter fullscreen mode Exit fullscreen mode

vi. Launch an EC2 instance in the dual-stack subnet with both IPv4 and IPv6 addresses:
NOTE: Change the image ID ami-xxxxxx, replace <your_Key> with your desired key pair name, and replace <IPv6-only-subnet-id> with the subnet ID of the IPv6-only subnet you created in the last step.

aws ec2 run-instances --image-id ami-xxxxxx --count 1 --instance-type t3.micro --key-name <your_Key> --subnet-id $dualStackSubnetID --associate-public-ip-address --ipv6-address-count 1 --private-dns-name-options HostnameType=ip-name,EnableResourceNameDnsARecord=true,EnableResourceNameDnsAAAARecord=true
Enter fullscreen mode Exit fullscreen mode

vii. Create an IPv6-only subnet in the same VPC using the AWS Management Console or AWS CLI.

viii. Once the IPv6-only subnet is created, use the subnet ID of the newly created IPv6-only subnet in the following command to launch an EC2 instance with only an IPv6 address:
NOTE: Change the image ID ami-xxxxxx , and replace <IPv6-only-subnet-id> with the subnet ID of the IPv6-only subnet you created in last setp.

aws ec2 run-instances --image-id ami-xxxxxx --count 1 --instance-type t3.micro --key-name <your_Key> --subnet-id <IPv6onlySubentIdHere> --ipv6-address-count 1 --private-dns-name-options HostnameType=resource-name,EnableResourceNameDnsARecord=false,EnableResourceNameDnsAAAARecord=true
Enter fullscreen mode Exit fullscreen mode

After this entire process, allow the security group SSH from the dual-stack instance to the IPv6-only instance. Then, connect from your local instance to the dual-stack instance using IPv4, and from the dual-stack instance, connect to the IPv6-only instance using its IPv6 address. I assume you have uploaded the required key into the dual-stack instance to connect to the IPv6 instance.

NOTE: Here, we are using the dual-stack instance as a bastion host if your local network does not have proper IPv6 routing. If you have proper IPv6 routing, the dual-stack instance and subnet are not required; you can directly connect to the IPv6-only instance.

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay