an inappropriate value of the string attribute cidrblock is required

You are passing an array to the cidr_block which results in the given error. You need to pass a string to the cidr block.

cidr_block = data.template_file.public_cidrsubnet[count.index].rendered

You also need to change the * to the actual count. Otherwise you will get an error complaining that it is a tuple with 2 elements. With your code the result of data.template_file.public_cidrsubnet[*].rendered is

cidr_output = [
  "10.0.0.0/24",
  "10.0.1.0/24",
]

Then the next issue you will face is with the count itself. You are using the total number of availability zones as count, but you want the count to be the subnet_count. You only have 2 subnets created in the public_cidrsubnet template, so you can’t create a subnet in each availability zone. Then you also need to pass the count.index to the slice for the availability_zone argument.

The correct aws_subnet resource that should work

resource "aws_subnet" "tf-aws-sn" {
  count             = var.subnet_count
  vpc_id            = aws_vpc.tf-aws-vn.id
  cidr_block        = data.template_file.public_cidrsubnet[count.index].rendered
  availability_zone = slice(data.aws_availability_zones.available.names, 0, var.subnet_count)[count.index]

}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top