hashicorp/terraform · error

error loading variables: %w

Error message

error loading variables: %w

What it means

In FetchVariables (backend_context.go:206-209), b.client.Variables.ListAll(ctx, remoteWorkspaceID, nil) failed with an error other than tfe.ErrResourceNotFound (404 is explicitly ignored so a workspace with no variables still works). This is the actual variable-list API call failing due to network, auth, server, or permission issues on the variables endpoint.

Source

Thrown at internal/cloud/backend_context.go:208

		diags = diags.Append(fmt.Errorf("error finding remote workspace: %w", err))
		return nil, diags
	}

	w, err := b.fetchWorkspace(ctx, b.Organization, workspace)
	if err != nil {
		diags = diags.Append(fmt.Errorf("error loading workspace: %w", err))
		return nil, diags
	}

	if isLocalExecutionMode(w.ExecutionMode) {
		log.Printf("[TRACE] cloud: skipping variable fetch for workspace %s/%s (%s), workspace is in Local Execution mode", b.getRemoteWorkspaceName(workspace), b.Organization, remoteWorkspaceID)
		return nil, nil
	}

	log.Printf("[TRACE] cloud: retrieving variables from workspace %s/%s (%s)", b.getRemoteWorkspaceName(workspace), b.Organization, remoteWorkspaceID)
	tfeVariables, err := b.client.Variables.ListAll(ctx, remoteWorkspaceID, nil)
	if err != nil && err != tfe.ErrResourceNotFound {
		diags = diags.Append(fmt.Errorf("error loading variables: %w", err))
		return nil, diags
	}

	result := make(map[string]arguments.UnparsedVariableValue)
	if tfeVariables != nil {
		for _, v := range tfeVariables.Items {
			if v.Category == tfe.CategoryTerraform {
				result[v.Key] = &remoteStoredVariableValue{
					definition: v,
				}
			}
		}
	}

	return result, nil
}

// remoteStoredVariableValue is a backendrun.UnparsedVariableValue implementation

View on GitHub (pinned to c9def3e214)

Solutions

  1. Grant the token's team permission to read workspace variables (or 'Manage Workspaces').
  2. Retry for transient/network errors.
  3. Inspect the wrapped error for HTTP status (403 => permissions, 5xx => server, 429 => rate limit).
  4. Verify the workspace still exists and its ID is current (re-run init).
Defensive patterns

Strategy: retry

Validate before calling

func canListVariables(ctx context.Context, c *tfe.Client, wsID string) bool {
    _, err := c.Variables.List(ctx, wsID, nil)
    return err == nil || errors.Is(err, tfe.ErrResourceNotFound)
}

Try / catch

// Retry non-404 variable-list failures with backoff.
for i := 0; i < 3; i++ {
    vars, err := c.Variables.ListAll(ctx, wsID, nil)
    if err == nil || errors.Is(err, tfe.ErrResourceNotFound) { return vars }
    if !isRetryable(err) { return err }
    time.Sleep(backoff(i))
}

Prevention

When it happens

Trigger: Variables.ListAll at backend_context.go:206 returns err != nil && err != tfe.ErrResourceNotFound. Occurs when listing workspace Terraform variables fails for a non-'not found' reason.

Common situations: Token lacks permission to list variables on the workspace. Transient API/network error. TFE variables service degraded. Rate limiting. Workspace ID stale (workspace recreated) so the variables endpoint 500s.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/eab2119180c99329. Report an issue: GitHub.