Home > Enterprise >  How can I correct a Terraform, Error: Incorrect attribute value type error?
How can I correct a Terraform, Error: Incorrect attribute value type error?

Time:11-30

Here is a the snippets of relevant code:

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

data "aws_availability_zones" "available" {
  state     = "available"
}

resource "aws_subnet" "private_subnet" {
  count                   = length(data.aws_availability_zones.available.names)
  vpc_id                  = aws_vpc.main.id
  cidr_block              = "10.0.${20 count.index}.0/24"
  availability_zone       = data.aws_availability_zones.available.names[count.index]
  map_public_ip_on_launch = false
}

resource "aws_autoscaling_group" "prod" {
  name                      = "prod-web"
  count                     = length(data.aws_availability_zones.available.names)
....
  vpc_zone_identifier       = aws_subnet.private_subnet[count.index] 

Above, on the vpc_zone_identifier line, it returns

Error: Incorrect attribute value type, four times, aws_subnet.private_subnet is tuple with 4 elements
│     │ count.index is 0

indexed 0, 1, 2, and 3. If 4 subnets are the result, what is wrong with that?

CodePudding user response:

You need to obtain id of the subnet:

vpc_zone_identifier       = [aws_subnet.private_subnet[count.index].id' 

or without count:

resource "aws_autoscaling_group" "prod" {
  name                      = "prod-web"
  vpc_zone_identifier       = aws_subnet.private_subnet[*].id
 

CodePudding user response:

The attribute for aws_autoscaling_group, the vpc_zone_identifier, accepts a list of subnet ids, while you are passing a full resource, hence the error type you are getting.

Also, you are creating multiple autoscaling groups with that count, you should remove it.

This would look something like this:

resource "aws_autoscaling_group" "prod" {
  name                      = "prod-web"
  vpc_zone_identifier       = aws_subnet.private_subnet[*].id
  ...
}

CodePudding user response:

As the terraform doc suggests, the vpc_zone_identifier expects a list, but you are passing tuple. You can use something like this -

vpc_zone_identifier = [x, y, z] 
  • Related