Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Terraform/AWS – Call to function "element" failed: cannot read elements from string

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading