learner
learner

Reputation: 2860

Inappropriate value for the attribute cidr_block string is required

Hi my terraform code is here

main.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
  }
}
# Configure the AWS Provider
provider "aws" {
  region = var.region
}

# DATA

data "aws_availability_zones" "available" {}

data "template_file" "public_cidrsubnet" {
  count    = var.subnet_count
  template = "$${cidrsubnet(vpc_cidr,8,current_count)}"
  vars = {
    vpc_cidr      = var.network_address_space
    current_count = count.index
  }
}

resource "aws_vpc" "tf-aws-vn" {
  cidr_block = var.network_address_space
  tags       = local.common_tags
  #name       = var.name
}
resource "aws_subnet" "tf-aws-sn" {
  count             = length(data.aws_availability_zones.available.names)
  vpc_id            = aws_vpc.tf-aws-vn.id
  cidr_block        = [data.template_file.public_cidrsubnet[*].rendered]
  availability_zone = slice(data.aws_availability_zones.available.names, 0, var.subnet_count)
  tags              = local.common_tags
}

variables.tf

variable "region" {
  default = "us-east-1"
}
variable network_address_space {}

variable name {
  type = string
}
variable "subnet_count" {
  type = number
}

And finally! terraform.tfvars

network_address_space = "10.0.0.0/16"
subnet_count          = 2

I'm getting error like below:

Error: Incorrect attribute value type

  on main.tf line 36, in resource "aws_subnet" "tf-aws-sn":
  36:   cidr_block        = [data.template_file.public_cidrsubnet[*].rendered]

Inappropriate value for attribute "cidr_block": string required.

What is the issue?

I want to create n subnets for any address range I provide

My terraform version is 0.13.5

Upvotes: 2

Views: 2124

Answers (1)

Bakie
Bakie

Reputation: 46

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]

}

Upvotes: 1

Related Questions