I am trying to deploy the following resource while cycling through my subnets.
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = element(var.public_subnets_cidr, count.index)
availability_zone = element(var.`your text`availability_zones_public, count.index)
count = length(var.public_subnets_cidr)
However, I’m receiving the following error.
│ Error: Error in function call
│ on vpc/main.tf line 33, in resource "aws_subnet" "private":
│ 33: cidr_block = element(var.private_subnets_cidr, count.index)
│ │ while calling element(list, index)
│ │ count.index is 30
││ var.private_subnets_cidr is "[\"192.168.4.0/24\",\"192.168.5.0/24\",\"192.168.6.0/24\"]"
│ Call to function "element" failed: cannot read elements from string.`
Any idea why this is? From the look of things, this should be fine?
public_subnets_cidr = ["192.168.0.0/24", "192.168.1.0/24", "192.168.2.0/24"]
private_subnets_cidr = ["192.168.4.0/24", "192.168.5.0/24", "192.168.6.0/24"]
availability_zones_public = ["us-east-1a", "us-east-1b", "us-east-1c"]
availability_zones_private = ["us-east-1d", "us-east-1b", "us-east-1f"]
>Solution :
The error is clear, var.private_subnets_cidr is "[\"192.168.4.0/24\",\"192.168.5.0/24\",\"192.168.6.0/24\"]" which is literal string, not a list ["192.168.4.0/24", "192.168.5.0/24", "192.168.6.0/24"].
You have to check your variables, probably you are overwriting private_subnets_cidr somewhere. Make sure you use a list, not a string as value for private_subnets_cidr.