Skip to content

bug(claude-code): enable_aibridge causes "Invalid count argument" on workspace creation #812

Description

@shanewhite97

Description

When using the claude-code module (v4.8.1) with enable_aibridge = true and the calling template adds depends_on to the module block (e.g. depends_on = [module.git-clone]), Terraform fails during plan with:

Error: Invalid count argument
on .terraform/modules/claude-code/main.tf line 290, in resource "coder_env" "claude_api_key":
  290:   count = local.claude_api_key != "" ? 1 : 0
The "count" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created.

Without depends_on on the module call, the plan succeeds because data.coder_workspace_owner.me.session_token is resolved during the plan phase as expected.

Root Cause

When a caller adds depends_on to the module "claude-code" block, Terraform conservatively defers all data source reads inside the module to the apply phase (HashiCorp docs: "Terraform may treat more values as unknown '(known after apply)' because it is uncertain what changes will occur on the upstream object. This is especially likely when you use depends_on for modules.").

This causes data.coder_workspace_owner.me.session_token — which is normally known at plan time — to become unknown. The derived local:

claude_api_key = var.enable_aibridge ? data.coder_workspace_owner.me.session_token : var.claude_api_key

then also becomes unknown, and its use in count (line 290) fails because Terraform requires count to be fully resolved at plan time.

Suggested Fix

Replace the count expression with one that depends only on input variables (always known at plan time):

resource "coder_env" "claude_api_key" {
  count = (var.enable_aibridge || var.claude_api_key != "") ? 1 : 0
  # ...
}

This is semantically equivalent because:

  • enable_aibridge = truesession_token is used (always non-empty) → env var should be created
  • enable_aibridge = false and claude_api_key != "" → env var should be created
  • enable_aibridge = false and claude_api_key == "" → env var should not be created

The module's existing validation already enforces that enable_aibridge and claude_api_key are mutually exclusive.

The value field can remain local.claude_api_key — only count and for_each must be deterministic at plan time.

Reproduction

  1. Use claude-code module v4.8.1 with enable_aibridge = true
  2. Add depends_on = [module.some-other-module] to the module "claude-code" block
  3. Run terraform plan — fails with the above error
  4. Remove depends_on — plan succeeds

Environment

  • Module: claude-code v4.8.1
  • Feature: enable_aibridge = true
  • Trigger: depends_on on the module call

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions