Need help trying to connect IPV6 address to VPC Subnets

0

I am trying to use terraform for provisioning a VPC with 3 public subnets connected to a public route-table which is connected to an internet gateway. Here's my terraform script. But I am constantly getting the following error:

Error: setting EC2 Subnet (subnet-0aace51149e2c402a) AssignIpv6AddressOnCreation: InvalidParameterValue: Invalid value 'true' for assign-ipv6-address-on-creation. Cannot set assign-ipv6-address-on-creation to true unless the subnet (subnet-0aace51149e2c402a) has an IPv6 CIDR block associated with it.

If I try to do it from AWS Console, everything sets up fine. Only the IPV6 provisioning is not working. I did try to find the IP addresses of the ap-south-1 region and set them manually for IPv6 CIDR Range but that also is not working. Can someone please help me?

Here's my terraform script.

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
    }
  }
}

provider "aws" {
  region     = "ap-south-1"
  access_key = "XXXXX"
  secret_key = "XXXXX"
}

resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.testvpc.id
  tags = {
    Name = "testvpc-igw"
  }
}

resource "aws_route_table" "public" {
  vpc_id = aws_vpc.testvpc.id
  tags = {
    Name = "testvpc-rtb-public"
  }

  route {
    gateway_id = aws_internet_gateway.igw.id
    cidr_block = "0.0.0.0/0"
  }

  route {
    gateway_id      = aws_internet_gateway.igw.id
    ipv6_cidr_block = "::/0"
  }
}

resource "aws_route_table_association" "name" {
  route_table_id = aws_route_table.public.id
  count          = 3
  subnet_id      = element(aws_subnet.public, count.index).id
}

resource "aws_vpc" "testvpc" {
  assign_generated_ipv6_cidr_block     = true
  cidr_block                           = "10.0.0.0/16"
  enable_dns_hostnames                 = true
  enable_dns_support                   = true
  enable_network_address_usage_metrics = true
  tags = {
    Name = "testvpc"
  }
}

variable "azs" {
  type     = string
  nullable = false
  default  = "ap-south-1a"
}

variable "ipv4_cidrs" {
  type     = list(string)
  nullable = false
  default  = ["10.0.0.0/20", "10.0.16.0/20", "10.0.32.0/20"]
}

resource "aws_subnet" "public" {
  assign_ipv6_address_on_creation = true
  vpc_id                          = aws_vpc.testvpc.id
  count                           = length(var.ipv4_cidrs)
  availability_zone               = var.azs
  tags = {
    name = "${aws_vpc.testvpc.tags.Name}-public-subnet-${count.index}"
  }
  cidr_block = element(var.ipv4_cidrs, count.index)
}
asked 4 months ago554 views
1 Answer
0

Hello.

I think the following blogs will be helpful.
You need to associate IPv6 with the subnet as shown below.
https://medium.com/@mattias.holmlund/setting-up-ipv6-on-amazon-with-terraform-e14b3bfef577

resource "aws_vpc" "eu-central-1" {
    provider = "aws.eu-central-1"
    enable_dns_support = true
    enable_dns_hostnames = true
    assign_generated_ipv6_cidr_block = true
    cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "eu-central-1" {
    provider = "aws.eu-central-1"
    vpc_id = "${aws_vpc.eu-central-1.id}"
    cidr_block = "${cidrsubnet(aws_vpc.eu-central-1.cidr_block, 4, 1)}"
    map_public_ip_on_launch = true

    ipv6_cidr_block = "${cidrsubnet(aws_vpc.eu-central-1.ipv6_cidr_block, 8, 1)}"
    assign_ipv6_address_on_creation = true
}

So in your case, wouldn't you need to set the subnet part as follows?

resource "aws_subnet" "public" {
  assign_ipv6_address_on_creation = true
  vpc_id                          = aws_vpc.testvpc.id
  count                           = length(var.ipv4_cidrs)
  availability_zone               = var.azs
  tags = {
    name = "${aws_vpc.testvpc.tags.Name}-public-subnet-${count.index}"
  }
  cidr_block = element(var.ipv4_cidrs, count.index)
  ipv6_cidr_block = "${cidrsubnet(aws_vpc.testvpc.ipv6_cidr_block, 8, 1)}"
}
profile picture
EXPERT
answered 4 months 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