Skip to content

bug: referencing agent tuple in module throws confusing error #583

Open
@stirby

Description

@stirby

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

This is likely a case where we need to improve docs/error message.

There's a strange indexing error that gets thrown when the coder_agent is assigned a count and must be referenced with a tuple, but a module referencing it does contain a count attribute.

With the following agent and module definition...

resource "coder_agent" "dev" {
  count              = data.coder_workspace.me.start_count
  arch               = "amd64"
  auth               = "token"
  ...
}

module "claude-code" {
  source              = "registry.coder.com/modules/claude-code/coder"
  version             = "1.0.31"

  agent_id            = coder_agent.dev[0].id

  folder              = "/home/coder"
  install_claude_code = true
  claude_code_version = "latest"
}

Terraform throws the following error:

Error: Invalid index
on main.tf line 339, in module "claude-code":
  339:   agent_id                = coder_agent.dev[0].id
    ├────────────────
    │ coder_agent.dev is empty tuple
The given key does not identify an element in this collection value: the collection has no elements.

Unless you specify count in the module, then no error is thrown:

module "claude-code" {
  count               = data.coder_workspace.me.start_count

  source              = "registry.coder.com/modules/claude-code/coder"
  version             = "1.0.31"

  agent_id            = coder_agent.dev[0].id

  folder              = "/home/coder"
  install_claude_code = true
  claude_code_version = "latest"
}

Relevant Log Output

Image

Expected Behavior

I did not realize that we had to define the count, so this may be a discoverability issue.

Steps to Reproduce

  1. Define coder_agent with count = data.workspace.<me>.start_count
  2. Define module that requires `agent_id = coder_agent.[0].id
  3. Build
  4. Add count = data.workspace.<me>.start_count to module to resolve.

Environment

  • Host OS: linux
  • Coder version: v2.21.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    spikeShort investigation task

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions