CloudFormation: how to create a VPC ipv6-only and 3 subnets ?

0

I have a VPC ipv4 with 3 subnets where each task has a public ip because the need to use cdn and load balancer etc.

now, public ipv4 address are subject to fee

so I want to create a VPC ipv6-only, and 3 ipv6-only subnets

i am unable to find resources/docs/tutorials about how to do this using CloudFormation yml files

5 Answers
6

To create an IPv6-only VPC with three IPv6-only subnets using CloudFormation YAML files, you can follow these steps:

Step 1: Define Parameters and Resources

**Parameters: VpcCIDRBlock: Type: String Default: "fd00:10:0::/56" Description: CIDR block for the VPC IPv6 address space

Resources: MyVPC: Type: AWS::EC2::VPC Properties: CidrBlock: !Ref VpcCIDRBlock EnableIpv6: true EnableDnsSupport: true EnableDnsHostnames: true

PublicSubnet1: Type: AWS::EC2::Subnet Properties: VpcId: !Ref MyVPC CidrBlock: "fd00:10:0:1::/64" MapPublicIpOnLaunch: false

PublicSubnet2: Type: AWS::EC2::Subnet Properties: VpcId: !Ref MyVPC CidrBlock: "fd00:10:0:2::/64" MapPublicIpOnLaunch: false

PublicSubnet3: Type: AWS::EC2::Subnet Properties: VpcId: !Ref MyVPC CidrBlock: "fd00:10:0:3::/64" MapPublicIpOnLaunch: false**

Step 2: Deploy the CloudFormation Stack Deploy the CloudFormation stack using the AWS Management Console, AWS CLI, or SDK.

Step 3: Access Resources Once the stack is created successfully, you will have an IPv6-only VPC with three IPv6-only subnets. Resources within these subnets won't be assigned public IPv4 addresses, thus avoiding additional fees associated with public IPv4 addresses.

Additional Considerations Ensure that your VPC's route tables are properly configured to route traffic to the internet gateway or other necessary destinations.

Adjust the CIDR blocks and other properties as needed for your specific requirements.

This CloudFormation template creates an IPv6-only VPC and subnets, helping you avoid fees associated with public IPv4 addresses while enabling connectivity using IPv6. Adjustments can be made based on your specific needs and preferences.

profile picture
answered 10 days ago
4

Hi follow the below steps

  1. Create the VPC: Define a VPC resource with an IPv6 CIDR block.
  2. Create IPv6-Only Subnets: Define subnet resources within the VPC, specifying only IPv6 CIDR blocks.
  3. Set Up Internet Gateway (Optional): If you need internet access for your IPv6-only subnets, attach an internet gateway.
  4. Configure Route Tables: Create route tables for your subnets to route IPv6 traffic properly.
  5. Associate Subnets with Route Tables: Associate each subnet with its corresponding route table.

Look at the sample yaml template which helps to you to change the Script according your configuration.

Resources:
  MyVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: "fd00:10:20::/48" # Example IPv6 CIDR block
      EnableDnsSupport: true
      EnableDnsHostnames: true

  PublicSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MyVPC
      CidrBlock: "fd00:10:20:1::/64" # Example IPv6 CIDR block

  PublicSubnet2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MyVPC
      CidrBlock: "fd00:10:20:2::/64" # Example IPv6 CIDR block

  PublicSubnet3:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MyVPC
      CidrBlock: "fd00:10:20:3::/64" # Example IPv6 CIDR block

  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: MyInternetGateway

  AttachGateway:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref MyVPC
      InternetGatewayId: !Ref InternetGateway

  PublicRouteTable1:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref MyVPC

  PublicRoute1:
    Type: AWS::EC2::Route
    DependsOn: AttachGateway
    Properties:
      RouteTableId: !Ref PublicRouteTable1
      DestinationIpv6CidrBlock: "::/0"
      GatewayId: !Ref InternetGateway

  Subnet1RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicSubnet1
      RouteTableId: !Ref PublicRouteTable1

  PublicRouteTable2:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref MyVPC

  PublicRoute2:
    Type: AWS::EC2::Route
    DependsOn: AttachGateway
    Properties:
      RouteTableId: !Ref PublicRouteTable2
      DestinationIpv6CidrBlock: "::/0"
      GatewayId: !Ref InternetGateway

  PublicRouteTable3:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref MyVPC

  PublicRoute3:
    Type: AWS::EC2::Route
    DependsOn: AttachGateway
    Properties:
      RouteTableId: !Ref PublicRouteTable3
      DestinationIpv6CidrBlock: "::/0"
      GatewayId: !Ref InternetGateway

  Subnet2RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicSubnet2
      RouteTableId: !Ref PublicRouteTable2

  Subnet3RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicSubnet3
      RouteTableId: !Ref PublicRouteTable3

answered 10 days ago
  • I'd find usefull a sample yaml templated, if possible.

4

Hi,

Creating an IPv6-only VPC and subnets using CloudFormation YAML can be once you understand the required configurations. could you please find the AWS CloudFormation documentation.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-cidr.html#intrinsic-function-reference-cidr-examples.

Example yaml script: -

AWSTemplateFormatVersion: '2010-09-09' Resources: MyVPC: Type: AWS::EC2::VPC Properties: CidrBlock: 'fd00::/56' # IPv6 CIDR block for the VPC EnableDnsSupport: true EnableDnsHostnames: true InstanceTenancy: default

MySubnet1: Type: AWS::EC2::Subnet Properties: VpcId: !Ref MyVPC CidrBlock: 'fd00:1::/64' # IPv6 CIDR block for Subnet 1 MapPublicIpOnLaunch: false

MySubnet2: Type: AWS::EC2::Subnet Properties: VpcId: !Ref MyVPC CidrBlock: 'fd00:2::/64' # IPv6 CIDR block for Subnet 2 MapPublicIpOnLaunch: false

MySubnet3: Type: AWS::EC2::Subnet Properties: VpcId: !Ref MyVPC CidrBlock: 'fd00:3::/64' # IPv6 CIDR block for Subnet 3 MapPublicIpOnLaunch: false

answered 10 days ago
4

Please find the solution script below

AWSTemplateFormatVersion: '2010-09-09'
Description: Create an IPv6-only VPC with 3 IPv6-only subnets

Parameters:
  VpcCidrBlock:
    Type: String
    Default: "fd00:10:20::/64"  # Update with your desired IPv6 CIDR block for the VPC
    Description: CIDR block for the IPv6 VPC

Resources:
  MyVPC:
    Type: 'AWS::EC2::VPC'
    Properties:
      CidrBlock: !Ref VpcCidrBlock
      EnableDnsSupport: true
      EnableDnsHostnames: true
      InstanceTenancy: default
      AssignIpv6AddressOnCreation: true  # Enable IPv6 support for the VPC

  Subnet1:
    Type: 'AWS::EC2::Subnet'
    Properties:
      VpcId: !Ref MyVPC
      CidrBlock: !Select [0, !Cidr [!Ref VpcCidrBlock, 3, 64]]  # Adjust the CIDR block based on your VPC's CIDR block
      Ipv6CidrBlock: !Select [0, !Cidr [!Ref VpcCidrBlock, 3, 64]]
      MapPublicIpOnLaunch: false  # Disable auto-assignment of public IPv4 addresses

  Subnet2:
    Type: 'AWS::EC2::Subnet'
    Properties:
      VpcId: !Ref MyVPC
      CidrBlock: !Select [1, !Cidr [!Ref VpcCidrBlock, 3, 64]]  # Adjust the CIDR block based on your VPC's CIDR block
      Ipv6CidrBlock: !Select [1, !Cidr [!Ref VpcCidrBlock, 3, 64]]
      MapPublicIpOnLaunch: false  # Disable auto-assignment of public IPv4 addresses

  Subnet3:
    Type: 'AWS::EC2::Subnet'
    Properties:
      VpcId: !Ref MyVPC
      CidrBlock: !Select [2, !Cidr [!Ref VpcCidrBlock, 3, 64]]  # Adjust the CIDR block based on your VPC's CIDR block
      Ipv6CidrBlock: !Select [2, !Cidr [!Ref VpcCidrBlock, 3, 64]]
      MapPublicIpOnLaunch: false  # Disable auto-assignment of public IPv4 addresses

answered 10 days ago
1

Hello.

To add IPv6 to a VPC with CloudFormation, you need to add an IPv6 CIDR using "AWS::EC2::VPCCidrBlock".

Resources:
  Vpc:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.128.0.0/16

# Add IPv6
  VpcCidrBlock:
    Type: AWS::EC2::VPCCidrBlock
    Properties:
      AmazonProvidedIpv6CidrBlock: true
      VpcId: !Ref Vpc

  Subnet:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: 10.128.0.0/24
      Ipv6CidrBlock: !Select
        - 0
        - !Cidr
          - !Select
            - 0
            - !GetAtt Vpc.Ipv6CidrBlocks
          - 1
          - 64
      VpcId: !Ref Vpc
profile picture
EXPERT
answered 10 days ago
profile pictureAWS
EXPERT
reviewed 10 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions