Home > Enterprise >  getting error while using list(string) data type in terraform module
getting error while using list(string) data type in terraform module

Time:10-01

I am trying to create 2 subnets in aws (with terraform) by passing 2 values in single variable.

Getting below error while executing "terraform validate" command

Please guide me how to correctly define list(string) variable data type in terraform module and correctly use it.

│ Error: Invalid value for input variable
│
│   on usage-test.tf line 11, in module "vpc_module":
│   11:   subnet_cidr_block = ["10.0.0.0/24","10.0.1.0/24"]
│
│ The given value is not suitable for module.vpc_module.var.subnet_cidr_block declared at vpc/var-test.tf:21,1-29: string required.
╵
╷
│ Error: Invalid value for input variable
│
│   on usage-test.tf line 12, in module "vpc_module":
│   12:   subnet_az = ["ap-south-1a","ap-south-1b"]
│
│ The given value is not suitable for module.vpc_module.var.subnet_az declared at vpc/var-test.tf:25,1-21: string required.
╵

refer terraform files below:-

variable.tf:

variable "subnet_cidr_block" {
  type = list(string)
}

variable "subnet_az" {
  type = list(string)
}

main.tf:

resource "aws_subnet" "mysubnet_public" {
  vpc_id = aws_vpc.myvpc.id
  cidr_block = var.subnet_cidr_block
  availability_zone = var.subnet_az
  map_public_ip_on_launch = "true"
  depends_on = [aws_internet_gateway.mygw]
}

usage.tf

provider "aws" {
  region = "ap-south-1"
}

module "vpc_module" {
  source = "./vpc"
  vpc_cider_block = "10.0.0.0/16"
  vpc_name = "myvpc"
  route_table_name = "myrt"
  subnet_cidr_block = ["10.0.0.0/24","10.0.1.0/24"]
  subnet_az = ["ap-south-1a","ap-south-1b"]

#  subnet_cidr_block = "10.0.0.0/24"
#  subnet_az = "ap-south-1a"
#  subnet_public_name = "mysubnet_public"

  sg_mgmt_name = "mysg_mgmt"
}

CodePudding user response:

Well, the error is pretty clear. You cannot use a list of strings, rather a single string value, as the provider documentation also shows [1]:

resource "aws_subnet" "main" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24" # <---- A single string value, not a list of strings

  tags = {
    Name = "Main"
  }
}

As a hint for the future: the argument is singular, i.e., cidr_block so that usually means it's a single value.


[1] https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet#basic-usage

CodePudding user response:

Thank you @Marko E for your suggestion

after some research found solution for this issue, refer below code.:-

main.tf

#below code is for creating multiple subnets
resource "aws_subnet" "mysubnet_public" {
  count = length(var.public_subnet_cidr)
  vpc_id = aws_vpc.myvpc.id
  cidr_block = element(var.public_subnet_cidr,count.index)
  availability_zone = element(var.azs,count.index)
  map_public_ip_on_launch = true
  tags = {
    Name = "Subnet-${count.index 1}"
  }
}

#below code is for associating above created multiple subnets to route table
resource "aws_route_table_association" "myroutetableassociation_public" {
  count = length(var.public_subnet_cidr)

  subnet_id = element(aws_subnet.mysubnet_public[*].id, count.index)
  route_table_id = aws_route_table.myroutetable_public.id
}

output.tf

output "mysubnet_public" {
  description = "List of IDs of public route tables"
  value       = aws_subnet.mysubnet_public[*].id
}

output "myroutetableassociation_public" {
  value = aws_route_table_association.myroutetableassociation_public[*].id
}

variable.tf

variable "public_subnet_cidr" {
  type    = list
}

variable "azs" {
  type = list
}

usage.tf

provider "aws" {
  region = "ap-south-1"
}

module "vpc_module" {
  source = "./vpc"
  vpc_name = "myvpc"
  public_subnet_cidr = ["10.0.0.0/24", "10.0.1.0/24"]
  azs = ["ap-south-1a", "ap-south-1b"]
}
  • Related