Skip to content

Commit 08e68a2

Browse files
AndreasSko35C4n0r
andauthored
Don't create CLAUDE_API_KEY coder_env if not set (#686)
## Description At the moment, the `CLAUDE_API_KEY` coder_env will always be created, even if the variable itself is not. This can lead to the environment variable being unset if it has been set outside of Terraform. With this PR, we make the `claude_api_key` coder_env conditional, so it will only be created if an API key has been set. ## Type of Change - [ ] New module - [ ] New template - [x] Bug fix - [ ] Feature/enhancement - [ ] Documentation - [ ] Other ## Module Information <!-- Delete this section if not applicable --> **Path:** `registry/coder/modules/claude-code/main.tf` **New version:** `v4.7.4` **Breaking change:** [ ] Yes [x] No ## Testing & Validation - [x] Tests pass (`bun test`) - [x] Code formatted (`bun fmt`) - [x] Changes tested locally ## Related Issues None --------- Co-authored-by: 35C4n0r <70096901+35C4n0r@users.noreply.github.com>
1 parent 66662db commit 08e68a2

3 files changed

Lines changed: 40 additions & 15 deletions

File tree

registry/coder/modules/claude-code/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Run the [Claude Code](https://docs.anthropic.com/en/docs/agents-and-tools/claude
1313
```tf
1414
module "claude-code" {
1515
source = "registry.coder.com/coder/claude-code/coder"
16-
version = "4.7.3"
16+
version = "4.7.4"
1717
agent_id = coder_agent.main.id
1818
workdir = "/home/coder/project"
1919
claude_api_key = "xxxx-xxxxx-xxxx"
@@ -47,7 +47,7 @@ By default, when `enable_boundary = true`, the module uses `coder boundary` subc
4747
```tf
4848
module "claude-code" {
4949
source = "registry.coder.com/coder/claude-code/coder"
50-
version = "4.7.3"
50+
version = "4.7.4"
5151
agent_id = coder_agent.main.id
5252
workdir = "/home/coder/project"
5353
enable_boundary = true
@@ -68,7 +68,7 @@ For tasks integration with AI Bridge, add `enable_aibridge = true` to the [Usage
6868
```tf
6969
module "claude-code" {
7070
source = "registry.coder.com/coder/claude-code/coder"
71-
version = "4.7.3"
71+
version = "4.7.4"
7272
agent_id = coder_agent.main.id
7373
workdir = "/home/coder/project"
7474
enable_aibridge = true
@@ -97,7 +97,7 @@ data "coder_task" "me" {}
9797
9898
module "claude-code" {
9999
source = "registry.coder.com/coder/claude-code/coder"
100-
version = "4.7.3"
100+
version = "4.7.4"
101101
agent_id = coder_agent.main.id
102102
workdir = "/home/coder/project"
103103
ai_prompt = data.coder_task.me.prompt
@@ -120,7 +120,7 @@ This example shows additional configuration options for version pinning, custom
120120
```tf
121121
module "claude-code" {
122122
source = "registry.coder.com/coder/claude-code/coder"
123-
version = "4.7.3"
123+
version = "4.7.4"
124124
agent_id = coder_agent.main.id
125125
workdir = "/home/coder/project"
126126
@@ -176,7 +176,7 @@ Run and configure Claude Code as a standalone CLI in your workspace.
176176
```tf
177177
module "claude-code" {
178178
source = "registry.coder.com/coder/claude-code/coder"
179-
version = "4.7.3"
179+
version = "4.7.4"
180180
agent_id = coder_agent.main.id
181181
workdir = "/home/coder/project"
182182
install_claude_code = true
@@ -198,7 +198,7 @@ variable "claude_code_oauth_token" {
198198
199199
module "claude-code" {
200200
source = "registry.coder.com/coder/claude-code/coder"
201-
version = "4.7.3"
201+
version = "4.7.4"
202202
agent_id = coder_agent.main.id
203203
workdir = "/home/coder/project"
204204
claude_code_oauth_token = var.claude_code_oauth_token
@@ -271,7 +271,7 @@ resource "coder_env" "bedrock_api_key" {
271271
272272
module "claude-code" {
273273
source = "registry.coder.com/coder/claude-code/coder"
274-
version = "4.7.3"
274+
version = "4.7.4"
275275
agent_id = coder_agent.main.id
276276
workdir = "/home/coder/project"
277277
model = "global.anthropic.claude-sonnet-4-5-20250929-v1:0"
@@ -328,7 +328,7 @@ resource "coder_env" "google_application_credentials" {
328328
329329
module "claude-code" {
330330
source = "registry.coder.com/coder/claude-code/coder"
331-
version = "4.7.3"
331+
version = "4.7.4"
332332
agent_id = coder_agent.main.id
333333
workdir = "/home/coder/project"
334334
model = "claude-sonnet-4@20250514"

registry/coder/modules/claude-code/main.tf

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,11 @@ resource "coder_env" "claude_code_oauth_token" {
276276
}
277277

278278
resource "coder_env" "claude_api_key" {
279+
count = local.claude_api_key != "" ? 1 : 0
280+
279281
agent_id = var.agent_id
280282
name = "CLAUDE_API_KEY"
281-
value = var.enable_aibridge ? data.coder_workspace_owner.me.session_token : var.claude_api_key
283+
value = local.claude_api_key
282284
}
283285

284286
resource "coder_env" "disable_autoupdater" {
@@ -324,7 +326,8 @@ locals {
324326
start_script = file("${path.module}/scripts/start.sh")
325327
module_dir_name = ".claude-module"
326328
# Extract hostname from access_url for boundary --allow flag
327-
coder_host = replace(replace(data.coder_workspace.me.access_url, "https://", ""), "http://", "")
329+
coder_host = replace(replace(data.coder_workspace.me.access_url, "https://", ""), "http://", "")
330+
claude_api_key = var.enable_aibridge ? data.coder_workspace_owner.me.session_token : var.claude_api_key
328331

329332
# Required prompts for the module to properly report task status to Coder
330333
report_tasks_system_prompt = <<-EOT

registry/coder/modules/claude-code/main.tftest.hcl

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ run "test_claude_code_with_api_key" {
4242
}
4343

4444
assert {
45-
condition = coder_env.claude_api_key.value == "test-api-key-123"
45+
condition = coder_env.claude_api_key[0].value == "test-api-key-123"
4646
error_message = "Claude API key value should match the input"
4747
}
4848
}
@@ -298,6 +298,13 @@ run "test_aibridge_enabled" {
298298
enable_aibridge = true
299299
}
300300

301+
override_data {
302+
target = data.coder_workspace_owner.me
303+
values = {
304+
session_token = "mock-session-token"
305+
}
306+
}
307+
301308
assert {
302309
condition = var.enable_aibridge == true
303310
error_message = "AI Bridge should be enabled"
@@ -314,12 +321,12 @@ run "test_aibridge_enabled" {
314321
}
315322

316323
assert {
317-
condition = coder_env.claude_api_key.name == "CLAUDE_API_KEY"
324+
condition = coder_env.claude_api_key[0].name == "CLAUDE_API_KEY"
318325
error_message = "CLAUDE_API_KEY environment variable should be set"
319326
}
320327

321328
assert {
322-
condition = coder_env.claude_api_key.value == data.coder_workspace_owner.me.session_token
329+
condition = coder_env.claude_api_key[0].value == data.coder_workspace_owner.me.session_token
323330
error_message = "CLAUDE_API_KEY should use workspace owner's session token when aibridge is enabled"
324331
}
325332
}
@@ -370,7 +377,7 @@ run "test_aibridge_disabled_with_api_key" {
370377
}
371378

372379
assert {
373-
condition = coder_env.claude_api_key.value == "test-api-key-xyz"
380+
condition = coder_env.claude_api_key[0].value == "test-api-key-xyz"
374381
error_message = "CLAUDE_API_KEY should use the provided API key when aibridge is disabled"
375382
}
376383

@@ -379,3 +386,18 @@ run "test_aibridge_disabled_with_api_key" {
379386
error_message = "ANTHROPIC_BASE_URL should not be set when aibridge is disabled"
380387
}
381388
}
389+
390+
run "test_no_api_key_no_env" {
391+
command = plan
392+
393+
variables {
394+
agent_id = "test-agent-no-key"
395+
workdir = "/home/coder/test"
396+
enable_aibridge = false
397+
}
398+
399+
assert {
400+
condition = length(coder_env.claude_api_key) == 0
401+
error_message = "CLAUDE_API_KEY should not be created when no API key is provided and aibridge is disabled"
402+
}
403+
}

0 commit comments

Comments
 (0)