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 = true → session_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
- Use
claude-code module v4.8.1 with enable_aibridge = true
- Add
depends_on = [module.some-other-module] to the module "claude-code" block
- Run
terraform plan — fails with the above error
- Remove
depends_on — plan succeeds
Environment
- Module:
claude-code v4.8.1
- Feature:
enable_aibridge = true
- Trigger:
depends_on on the module call
Description
When using the
claude-codemodule (v4.8.1) withenable_aibridge = trueand the calling template addsdepends_onto the module block (e.g.depends_on = [module.git-clone]), Terraform fails duringplanwith:Without
depends_onon the module call, the plan succeeds becausedata.coder_workspace_owner.me.session_tokenis resolved during the plan phase as expected.Root Cause
When a caller adds
depends_onto themodule "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:then also becomes unknown, and its use in
count(line 290) fails because Terraform requirescountto be fully resolved at plan time.Suggested Fix
Replace the
countexpression with one that depends only on input variables (always known at plan time):This is semantically equivalent because:
enable_aibridge = true→session_tokenis used (always non-empty) → env var should be createdenable_aibridge = falseandclaude_api_key != ""→ env var should be createdenable_aibridge = falseandclaude_api_key == ""→ env var should not be createdThe module's existing validation already enforces that
enable_aibridgeandclaude_api_keyare mutually exclusive.The
valuefield can remainlocal.claude_api_key— onlycountandfor_eachmust be deterministic at plan time.Reproduction
claude-codemodule v4.8.1 withenable_aibridge = truedepends_on = [module.some-other-module]to themodule "claude-code"blockterraform plan— fails with the above errordepends_on— plan succeedsEnvironment
claude-codev4.8.1enable_aibridge = truedepends_onon the module call