Skip to content

Commit

Permalink
fix(codebuild): workspace select arg parsing in tf wrapper
Browse files Browse the repository at this point in the history
The tf wrapper's workspace select handler hardcoded args[2] as the
workspace NAME. When the buildspec passed -or-create=true as an
explicit flag, args[2] was the flag string, not the name, causing
terraform to receive no NAME argument -> 'Expected a single argument'.

Fixes:
- buildspec: remove -or-create=true from the call (wrapper handles it)
- tf wrapper: skip flag args when finding the workspace name; use
  -or-create=true (=true required, bare -or-create may not parse in
  all Terraform 1.x patch versions)
  • Loading branch information
Your Name committed Mar 17, 2026
1 parent d94daef commit 03aefd5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion codebuild/buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ phases:
echo "No workspace-specific backend config found; using backend.tf"
tf init -input=false
fi
- tf workspace select -or-create=true "$TF_WORKSPACE"
- tf workspace select "$TF_WORKSPACE"
- echo "Active workspace $(tf workspace show)"

build:
Expand Down
7 changes: 6 additions & 1 deletion codebuild/tf
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,12 @@ def run_terraform(args):
return result.returncode
elif args[1] == 'select':
# Try to select or create workspace
cmd = [terraform_bin, 'workspace', 'select', '-or-create', args[2]]
# Find the workspace name (last non-flag argument)
ws_name = next((a for a in args[2:] if not a.startswith('-')), None)
if not ws_name:
print("Error: workspace select requires a NAME argument", file=sys.stderr)
return 1
cmd = [terraform_bin, 'workspace', 'select', '-or-create=true', ws_name]
result = subprocess.run(cmd, env=env)
if result.returncode == 0:
setup_workspace_directories(args[2])
Expand Down

0 comments on commit 03aefd5

Please sign in to comment.