Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
stirby opened this issue Apr 10, 2025 · 5 comments
Open
1 task done

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

stirby opened this issue Apr 10, 2025 · 5 comments
Labels
spike Short investigation task

Comments

@stirby
Copy link

stirby commented Apr 10, 2025

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
@matifali
Copy link
Member

matifali commented Apr 11, 2025

@stirby this is expected terraform behavior as count required for all resources and modules making use of a count.

@stirby
Copy link
Author

stirby commented Apr 14, 2025

I am moving this to internal as I agree with the expected behavior.

But @Emyrk wanted to investigate why that was the resulting error.

Copy link

github-actions bot commented Apr 14, 2025

❌ Failed to start the workspace. Please check the action logs for details.

@stirby stirby transferred this issue from coder/coder Apr 14, 2025
@matifali matifali added the spike Short investigation task label Apr 14, 2025
@Emyrk
Copy link
Member

Emyrk commented Apr 14, 2025

@matifali Wait, if you reference a block that uses count, the calling block needs to have count as well?

Can you link to the TF docs on that?

@matifali
Copy link
Member

@Emyrk I could not find any explicit docs on this, but this is my understanding from count in Terraform.
I guess that without a count, the dependent module block can not know if the coder_agent is created or not (i.e., the count can be "0" for coder_agent), which causes this error the collection has no elements.. As Terraform may be trying to create both the module and the coder_agent in parallel, and coder_agent may not have been created yet.

I am not sure if I was able to explain this or made it more confusing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
spike Short investigation task
Projects
None yet
Development

No branches or pull requests

3 participants