|
| 1 | +# Workspace Build Transitions |
| 2 | + |
| 3 | +This guide explains how to perform common workspace operations like start, stop, and delete using the Coder API. Due to the nature of Terraform, these operations are executed as workspace build transitions rather than having dedicated endpoints. |
| 4 | + |
| 5 | +## Overview of Workspace Builds |
| 6 | + |
| 7 | +In Coder, all workspace operations that change the state of a workspace (e.g., start, stop, delete) are implemented as builds. A workspace build is a Terraform operation (`terraform apply` or `terraform destroy`) that provisions or destroys workspace resources. |
| 8 | + |
| 9 | +Workspace builds are created by sending a `CreateWorkspaceBuildRequest` to the `/workspaces/{workspace}/builds` endpoint. The `transition` parameter specifies which operation to perform. |
| 10 | + |
| 11 | +## Workspace State Transitions |
| 12 | + |
| 13 | +Workspaces can have the following transitions: |
| 14 | + |
| 15 | +| Transition | Description | |
| 16 | +|------------|-------------| |
| 17 | +| `start` | Start a stopped workspace | |
| 18 | +| `stop` | Stop a running workspace | |
| 19 | +| `delete` | Delete a workspace | |
| 20 | + |
| 21 | +## Common Use Cases |
| 22 | + |
| 23 | +### Starting a Workspace |
| 24 | + |
| 25 | +To start a workspace, create a build with the `start` transition: |
| 26 | + |
| 27 | +```shell |
| 28 | +curl -X POST "https://coder.example.com/api/v2/workspaces/{workspace_id}/builds" \ |
| 29 | + -H "Content-Type: application/json" \ |
| 30 | + -H "Coder-Session-Token: {your_token}" \ |
| 31 | + -d '{ |
| 32 | + "transition": "start" |
| 33 | + }' |
| 34 | +``` |
| 35 | + |
| 36 | +### Stopping a Workspace |
| 37 | + |
| 38 | +To stop a workspace, create a build with the `stop` transition: |
| 39 | + |
| 40 | +```shell |
| 41 | +curl -X POST "https://coder.example.com/api/v2/workspaces/{workspace_id}/builds" \ |
| 42 | + -H "Content-Type: application/json" \ |
| 43 | + -H "Coder-Session-Token: {your_token}" \ |
| 44 | + -d '{ |
| 45 | + "transition": "stop" |
| 46 | + }' |
| 47 | +``` |
| 48 | + |
| 49 | +### Deleting a Workspace |
| 50 | + |
| 51 | +To delete a workspace, create a build with the `delete` transition: |
| 52 | + |
| 53 | +```shell |
| 54 | +curl -X POST "https://coder.example.com/api/v2/workspaces/{workspace_id}/builds" \ |
| 55 | + -H "Content-Type: application/json" \ |
| 56 | + -H "Coder-Session-Token: {your_token}" \ |
| 57 | + -d '{ |
| 58 | + "transition": "delete" |
| 59 | + }' |
| 60 | +``` |
| 61 | + |
| 62 | +### Advanced: Deleting a Workspace Without Destroying Resources (Orphaning) |
| 63 | + |
| 64 | +In some cases, administrators might need to delete a workspace from Coder's database without destroying the underlying infrastructure resources: |
| 65 | + |
| 66 | +```shell |
| 67 | +curl -X POST "https://coder.example.com/api/v2/workspaces/{workspace_id}/builds" \ |
| 68 | + -H "Content-Type: application/json" \ |
| 69 | + -H "Coder-Session-Token: {your_token}" \ |
| 70 | + -d '{ |
| 71 | + "transition": "delete", |
| 72 | + "orphan": true |
| 73 | + }' |
| 74 | +``` |
| 75 | + |
| 76 | +**Warning**: Use this option with caution as it will leave cloud resources running without being tracked by Coder, potentially leading to unaccounted costs. |
| 77 | + |
| 78 | +### Updating a Workspace with a New Template Version |
| 79 | + |
| 80 | +To update a workspace to use a new template version: |
| 81 | + |
| 82 | +```shell |
| 83 | +curl -X POST "https://coder.example.com/api/v2/workspaces/{workspace_id}/builds" \ |
| 84 | + -H "Content-Type: application/json" \ |
| 85 | + -H "Coder-Session-Token: {your_token}" \ |
| 86 | + -d '{ |
| 87 | + "transition": "start", |
| 88 | + "template_version_id": "{new_template_version_id}" |
| 89 | + }' |
| 90 | +``` |
| 91 | + |
| 92 | +## Monitoring Build Status |
| 93 | + |
| 94 | +You can monitor the progress of a build using the returned build ID: |
| 95 | + |
| 96 | +```shell |
| 97 | +curl "https://coder.example.com/api/v2/workspacebuilds/{build_id}" \ |
| 98 | + -H "Coder-Session-Token: {your_token}" |
| 99 | +``` |
| 100 | + |
| 101 | +To fetch build logs: |
| 102 | + |
| 103 | +```shell |
| 104 | +curl "https://coder.example.com/api/v2/workspacebuilds/{build_id}/logs?follow=true" \ |
| 105 | + -H "Coder-Session-Token: {your_token}" |
| 106 | +``` |
| 107 | + |
| 108 | +## Using the Coder CLI |
| 109 | + |
| 110 | +The Coder CLI provides a more user-friendly way to manage workspace transitions: |
| 111 | + |
| 112 | +```shell |
| 113 | +# Start a workspace |
| 114 | +coder start my-workspace |
| 115 | + |
| 116 | +# Stop a workspace |
| 117 | +coder stop my-workspace |
| 118 | + |
| 119 | +# Delete a workspace |
| 120 | +coder delete my-workspace |
| 121 | +``` |
| 122 | + |
| 123 | +## Error Handling |
| 124 | + |
| 125 | +Common errors when dealing with workspace builds include: |
| 126 | + |
| 127 | +1. **Build already in progress**: Only one build can be active for a workspace at a time |
| 128 | +2. **Unauthorized**: The requesting user doesn't have access to perform the operation |
| 129 | +3. **Invalid state transition**: For example, trying to stop an already stopped workspace |
| 130 | + |
| 131 | +Always check the status code and error message in the API response to handle these cases gracefully. |
0 commit comments