How to fix Error: Cannot access value of type list without an index in Terraform

TerraformBEGINNERMEDIUMUpdated Dec 31, 2025· 103 views

This error occurs when you try to reference a list, tuple, or set value directly without specifying which element you want to access. Terraform requires an explicit index (like [0]) to access individual list elements.

Quick fix

Fixes Error: Cannot access value of type list without an index

# ❌ Wrong - accessing list without index
variable "servers" {
  type    = list(string)
  default = ["web-1", "web-2", "web-3"]
}

output "server_name" {
  value = var.servers  # Error: Cannot access without index
}

# ✓ Correct - specify which element to access
output "server_name" {
  value = var.servers[0]  # Returns "web-1"
}
  1. Add index notation to access list elements
  2. Convert sets to lists before indexing
  3. Use splat syntax to extract attributes from resource lists
  4. Check data source results with length() before indexing
terraform — error
# ❌ Wrong - accessing list without index
variable "servers" {
type = list(string)
default = ["web-1", "web-2", "web-3"]
}
output "server_name" {
value = var.servers # Error: Cannot access without index
}
# ✓ Correct - specify which element to access
output "server_name" {
value = var.servers[0] # Returns "web-1"
}
Error: Cannot access value of type list without an index

What this error means

In Terraform's HCL language, lists and tuples are ordered collections that must be accessed by index using bracket notation. When you reference a list variable or attribute without an index, Terraform cannot determine which element you want and throws this error. Additionally, sets (unordered collections) cannot be indexed at all - they must be converted to lists first using tolist() before accessing individual elements.

Typical symptoms

  • Error message: 'Cannot access value of type list without an index'
  • Using a list variable directly in an argument: var.my_list instead of var.my_list[0]
  • Attempting to index a set directly without converting to list first
  • Passing a list to a function that expects a single value
  • Using a resource attribute that returns a list without specifying an index

Common causes

  1. Attempting to access a list element without specifying an index number
  2. Forgetting to use bracket notation [0], [1], etc. to select from a list
  3. Trying to index a set value directly (sets are unordered and don't support indexing)
  4. Resource or data source attribute returns a list but code treats it as a single value
  5. Passing a list to a module variable or resource argument that expects a string or single value

How to fix "Error: Cannot access value of type list without an index"

1Add index notation to access list elements

Use bracket notation with the element index (starting at 0) to access list values:

hcl
# ❌ Wrong - accessing list without index
variable "servers" {
  type    = list(string)
  default = ["web-1", "web-2", "web-3"]
}

output "server_name" {
  value = var.servers  # Error: Cannot access without index
}

# ✓ Correct - specify which element to access
output "server_name" {
  value = var.servers[0]  # Returns "web-1"
}
2Convert sets to lists before indexing

If accessing a set attribute, use tolist() to convert it to a list first:

hcl
# ❌ Wrong - trying to index a set
resource "aws_security_group" "example" {
  # ...
}

output "first_tag" {
  value = aws_security_group.example.tags[0]  # Error: sets can't be indexed
}

# ✓ Correct - convert set to list then index
output "first_tag" {
  value = tolist(aws_security_group.example.tags)[0]
}
3Use splat syntax to extract attributes from resource lists

When you have multiple resources created with count and need to extract an attribute from all of them, use splat syntax:

hcl
# ❌ Wrong - trying to access list without index
resource "aws_instance" "servers" {
  count = 3
  # ...
}

output "all_ips" {
  value = aws_instance.servers.private_ip  # Error
}

# ✓ Correct - use splat syntax to get all values
output "all_ips" {
  value = aws_instance.servers[*].private_ip  # Returns ["10.0.1.1", "10.0.1.2", "10.0.1.3"]
}

# Or access a specific instance
output "first_ip" {
  value = aws_instance.servers[0].private_ip
}
4Check data source results with length() before indexing

When using data sources that return lists, verify the list isn't empty before accessing elements:

hcl
# ❌ Wrong - assuming list has elements
data "aws_subnets" "available" {
  filter {
    name   = "vpc-id"
    values = [aws_vpc.example.id]
  }
}

output "subnet_id" {
  value = data.aws_subnets.available.ids[0]  # Fails if no subnets found
}

# ✓ Correct - verify with length() or use try()
output "subnet_id" {
  value = length(data.aws_subnets.available.ids) > 0 ? data.aws_subnets.available.ids[0] : null
}

# Or use try() for graceful fallback
output "subnet_id" {
  value = try(data.aws_subnets.available.ids[0], null)
}
5Use for_each when you need to reference list values in resource arguments

When passing list elements to resources that don't accept list types, use for_each to iterate:

hcl
variable "availability_zones" {
  type    = list(string)
  default = ["us-east-1a", "us-east-1b", "us-east-1c"]
}

# ❌ Wrong - resource expects single AZ, not list
resource "aws_subnet" "example" {
  availability_zone = var.availability_zones  # Error
}

# ✓ Correct - use for_each to create one subnet per AZ
resource "aws_subnet" "example" {
  for_each          = toset(var.availability_zones)
  availability_zone = each.value  # References current AZ
  # ...
}
6Reference specific list indices in count.index scenarios

When using count to create multiple resources, use count.index to access the corresponding list element:

hcl
variable "instance_names" {
  type    = list(string)
  default = ["web-1", "web-2", "web-3"]
}

# ❌ Wrong - passing entire list
resource "aws_instance" "servers" {
  count = length(var.instance_names)
  tags = {
    Name = var.instance_names  # Error: expects string, not list
  }
}

# ✓ Correct - use count.index to access specific element
resource "aws_instance" "servers" {
  count = length(var.instance_names)
  tags = {
    Name = var.instance_names[count.index]  # Returns one name per instance
  }
}

Advanced notes

Lists in Terraform are zero-indexed, meaning the first element is at index [0], the second at [1], etc. Sets are unordered collections that don't support indexing at all - always convert sets to lists with tolist() before accessing by index. The splat syntax [*] is particularly powerful for extracting attributes from multiple resources created with count. When working with dynamic blocks, remember that dynamic blocks create nested blocks within a resource, not new resources, so they follow different syntax rules. Use the element() function as an alternative way to access list items: element(var.my_list, 0) is equivalent to var.my_list[0].

Frequently asked questions

What does "Error: Cannot access value of type list without an index" mean?
In Terraform's HCL language, lists and tuples are ordered collections that must be accessed by index using bracket notation. When you reference a list variable or attribute without an index, Terraform cannot determine which element you want and throws this error. Additionally, sets (unordered collections) cannot be indexed at all - they must be converted to lists first using tolist() before accessing individual elements.
How do I fix "Error: Cannot access value of type list without an index"?
This error occurs when you try to reference a list, tuple, or set value directly without specifying which element you want to access. Terraform requires an explicit index (like [0]) to access individual list elements.
What causes "Error: Cannot access value of type list without an index"?
Attempting to access a list element without specifying an index number Forgetting to use bracket notation [0], [1], etc. to select from a list Trying to index a set value directly (sets are unordered and don't support indexing)
Is the "Error: Cannot access value of type list without an index" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading