Skip to content

Commit 0f16344

Browse files
sharkymarkghuntley
authored andcommitted
docs: make it clear the CLI must be downloaded to use templates
1 parent e7fc21e commit 0f16344

File tree

1 file changed

+133
-66
lines changed

1 file changed

+133
-66
lines changed

docs/templates.md

Lines changed: 133 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
11
# Templates
22

3-
Templates are written in standard Terraform and describe the infrastructure for
4-
workspaces (e.g., aws_instance, kubernetes_pod, or both).
3+
Templates are written in [Terraform](https://www.terraform.io/) and describe the
4+
infrastructure for workspaces (e.g., docker_container, aws_instance,
5+
kubernetes_pod).
56

6-
In most cases, a small group of users (Coder admins) manage templates. Then,
7-
other users provision their development workspaces from templates.
7+
In most cases, a small group of users (team leads or Coder administrators) [have
8+
permissions](./admin/users.md#roles) to create and manage templates. Then, other
9+
users provision their [workspaces](./workspaces.md) from templates using the UI
10+
or CLI.
11+
12+
## Get the CLI
13+
14+
The CLI and the server are the same binary. We did this to encourage virality so
15+
individuals can start their own Coder deployments.
16+
17+
From your local machine, download the CLI for your operating system from the
18+
[releases](https://github.com/coder/coder/releases) or run:
19+
20+
```sh
21+
curl -fsSL https://coder.com/install.sh | sh
22+
23+
```
24+
25+
To see the sub-commands for managing templates, run:
26+
27+
```sh
28+
coder templates --help
29+
```
30+
31+
## Login to your Coder Deployment
32+
33+
Before you can create templates, you must first login to your Coder deployment
34+
with the CLI.
35+
36+
```sh
37+
coder login <your deployment Access URL>
38+
```
39+
40+
This will open a browser and ask you to authenticate to your Coder deployment,
41+
returning an API Key.
42+
43+
> Make a note of the API Key. You can re-use the API Key in future CLI logins or
44+
> sessions.
45+
46+
```sh
47+
coder --token <your API Key> login <your deployment Access URL>
48+
```
849

950
## Add a template
1051

@@ -25,38 +66,45 @@ vim <template-name>/main.tf
2566
coder templates create <template-name>
2667
```
2768

28-
> See the documentation and source code for each example in the
69+
> See the documentation and source code for each example as well as community
70+
> templates in the
2971
> [examples/](https://github.com/coder/coder/tree/main/examples/templates)
3072
> directory in the repo.
3173
3274
## Customize templates
3375

34-
Example templates are not designed to support every use (e.g [examples/aws-linux](https://github.com/coder/coder/tree/main/examples/templates/aws-linux) does
35-
not support custom VPCs). You can add these features by editing the Terraform
36-
code once you run `coder templates init` (new) or `coder templates pull`
37-
(existing).
76+
Example templates are not designed to support every use (e.g
77+
[examples/aws-linux](https://github.com/coder/coder/tree/main/examples/templates/aws-linux)
78+
does not support custom VPCs). You can add these features by editing the
79+
Terraform code once you run `coder templates init` (new) or `coder templates
80+
pull` (existing).
3881

3982
Refer to the following resources to build your own templates:
4083

41-
- Terraform: [Documentation](https://developer.hashicorp.com/terraform/docs) and [Registry](https://registry.terraform.io)
42-
- Common [concepts in templates](#concepts-in-templates) and [Coder Terraform provider](https://registry.terraform.io/providers/coder/coder/latest/docs)
43-
- [Coder example templates](https://github.com/coder/coder/tree/main/examples/templates) code
84+
- Terraform: [Documentation](https://developer.hashicorp.com/terraform/docs) and
85+
[Registry](https://registry.terraform.io)
86+
- Common [concepts in templates](#concepts-in-templates) and [Coder Terraform
87+
provider](https://registry.terraform.io/providers/coder/coder/latest/docs)
88+
- [Coder example
89+
templates](https://github.com/coder/coder/tree/main/examples/templates) code
4490

4591
## Concepts in templates
4692

47-
While templates are written with standard Terraform, the
48-
[Coder Terraform Provider](https://registry.terraform.io/providers/coder/coder/latest/docs) is
93+
While templates are written with standard Terraform, the [Coder Terraform
94+
Provider](https://registry.terraform.io/providers/coder/coder/latest/docs) is
4995
used to define the workspace lifecycle and establish a connection from resources
5096
to Coder.
5197

5298
Below is an overview of some key concepts in templates (and workspaces). For all
53-
template options, reference [Coder Terraform provider docs](https://registry.terraform.io/providers/coder/coder/latest/docs).
99+
template options, reference [Coder Terraform provider
100+
docs](https://registry.terraform.io/providers/coder/coder/latest/docs).
54101

55102
### Resource
56103

57-
Resources in Coder are simply [Terraform resources](https://www.terraform.io/language/resources).
58-
If a Coder agent is attached to a resource, users can connect directly to the resource over
59-
SSH or web apps.
104+
Resources in Coder are simply [Terraform
105+
resources](https://www.terraform.io/language/resources). If a Coder agent is
106+
attached to a resource, users can connect directly to the resource over SSH or
107+
web apps.
60108

61109
### Coder agent
62110

@@ -93,15 +141,17 @@ resource "kubernetes_pod" "pod1" {
93141
}
94142
```
95143

96-
The `coder_agent` resource can be configured as described in the
97-
[documentation for the `coder` Terraform provider.](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent)
98-
For example, you can use the `env` property to set environment variables that will be
99-
inherited by all child processes of the agent, including SSH sessions.
144+
The `coder_agent` resource can be configured as described in the [documentation
145+
for the `coder` Terraform
146+
provider.](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent)
147+
For example, you can use the `env` property to set environment variables that
148+
will be inherited by all child processes of the agent, including SSH sessions.
100149

101150
#### startup_script
102151

103152
Use the Coder agent's `startup_script` to run additional commands like
104-
installing IDEs, [cloning dotfiles](./dotfiles.md#templates), and cloning project repos.
153+
installing IDEs, [cloning dotfiles](./dotfiles.md#templates), and cloning
154+
project repos.
105155

106156
```hcl
107157
resource "coder_agent" "coder" {
@@ -138,15 +188,15 @@ coder dotfiles -y ${var.dotfiles_uri}
138188
Templates often contain _parameters_. These are defined by `variable` blocks in
139189
Terraform. There are two types of parameters:
140190

141-
- **Admin/template-wide parameters** are set when a template is created/updated. These values
142-
are often cloud configuration, such as a `VPC`, and are annotated
191+
- **Admin/template-wide parameters** are set when a template is created/updated.
192+
These values are often cloud configuration, such as a `VPC`, and are annotated
143193
with `sensitive = true` in the template code.
144194
- **User/workspace parameters** are set when a user creates a workspace. These
145-
values are often personalization settings such as "preferred region"
146-
or "workspace image".
195+
values are often personalization settings such as "preferred region", "machine
196+
type" or "workspace image".
147197

148-
The template sample below uses _admin and user parameters_ to allow developers to
149-
create workspaces from any image as long as it is in the proper registry:
198+
The template sample below uses _admin and user parameters_ to allow developers
199+
to create workspaces from any image as long as it is in the proper registry:
150200

151201
```hcl
152202
variable "image_registry_url" {
@@ -171,14 +221,14 @@ resource "docker_image" "workspace" {
171221

172222
[Learn about resource persistence in Coder](./templates/resource-persistence.md)
173223

174-
Coder workspaces can be started/stopped. This is often used to save on cloud costs or enforce
175-
ephemeral workflows. When a workspace is started or stopped, the Coder server
176-
runs an additional
177-
[terraform apply](https://www.terraform.io/cli/commands/apply), informing the
178-
Coder provider that the workspace has a new transition state.
224+
Coder workspaces can be started/stopped. This is often used to save on cloud
225+
costs or enforce ephemeral workflows. When a workspace is started or stopped,
226+
the Coder server runs an additional [terraform
227+
apply](https://www.terraform.io/cli/commands/apply), informing the Coder
228+
provider that the workspace has a new transition state.
179229

180-
This template sample has one persistent resource (docker volume) and one ephemeral resource
181-
(docker image).
230+
This template sample has one persistent resource (docker volume) and one
231+
ephemeral resource (docker image).
182232

183233
```hcl
184234
data "coder_workspace" "me" {
@@ -228,18 +278,20 @@ resource "kubernetes_pod" "podName" {
228278

229279
### Edit templates
230280

231-
You can edit a template using the coder CLI. Only
232-
[template admins and owners](./admin/users.md) can edit a template.
281+
You can edit a template using the coder CLI. Only [template admins and
282+
owners](./admin/users.md) can edit a template.
233283

234-
Using the CLI, login to Coder and run the following command to edit a single template:
284+
Using the CLI, login to Coder and run the following command to edit a single
285+
template:
235286

236287
```sh
237288
coder templates edit <template-name> --description "This is my template"
238289
```
239290

240291
Review editable template properties by running `coder templates edit -h`.
241292

242-
Alternatively, you can pull down the template as a tape archive (`.tar`) to your current directory:
293+
Alternatively, you can pull down the template as a tape archive (`.tar`) to your
294+
current directory:
243295

244296
```sh
245297
coder templates pull <template-name> file.tar
@@ -251,36 +303,39 @@ Then, extract it by running:
251303
tar -xf file.tar
252304
```
253305

254-
Make the changes to your template then run this command from the root of the template folder:
306+
Make the changes to your template then run this command from the root of the
307+
template folder:
255308

256309
```sh
257310
coder templates push <template-name>
258311
```
259312

260-
Your updated template will now be available. Outdated workspaces will have a prompt in the dashboard to update.
313+
Your updated template will now be available. Outdated workspaces will have a
314+
prompt in the dashboard to update.
261315

262316
### Delete templates
263317

264-
You can delete a template using both the coder CLI and UI. Only
265-
[template admins and owners](./admin/users.md) can delete a template, and the template
266-
must not have any running workspaces associated to it.
318+
You can delete a template using both the coder CLI and UI. Only [template admins
319+
and owners](./admin/users.md) can delete a template, and the template must not
320+
have any running workspaces associated to it.
267321

268-
Using the CLI, login to Coder and run the following command to delete a template:
322+
Using the CLI, login to Coder and run the following command to delete a
323+
template:
269324

270325
```console
271326
coder templates delete <template-name>
272327
```
273328

274-
In the UI, navigate to the template you want to delete, and select the dropdown in
275-
the right-hand corner of the page to delete the template.
329+
In the UI, navigate to the template you want to delete, and select the dropdown
330+
in the right-hand corner of the page to delete the template.
276331

277332
![delete-template](./images/delete-template.png)
278333

279334
#### Delete workspaces
280335

281-
When a workspace is deleted, the Coder server essentially runs a
282-
[terraform destroy](https://www.terraform.io/cli/commands/destroy) to remove all
283-
resources associated with the workspace.
336+
When a workspace is deleted, the Coder server essentially runs a [terraform
337+
destroy](https://www.terraform.io/cli/commands/destroy) to remove all resources
338+
associated with the workspace.
284339

285340
> Terraform's
286341
> [prevent-destroy](https://www.terraform.io/language/meta-arguments/lifecycle#prevent_destroy)
@@ -291,18 +346,22 @@ resources associated with the workspace.
291346
### Coder apps
292347

293348
By default, all templates allow developers to connect over SSH and a web
294-
terminal. See [Configuring Web IDEs](./ides/web-ides.md) to
295-
learn how to give users access to additional web applications.
349+
terminal. See [Configuring Web IDEs](./ides/web-ides.md) to learn how to give
350+
users access to additional web applications.
296351

297352
### Data source
298353

299-
When a workspace is being started or stopped, the `coder_workspace` data source provides
300-
some useful parameters. See the [documentation for the `coder` Terraform provider](https://registry.terraform.io/providers/coder/coder/latest/docs/data-sources/workspace)
354+
When a workspace is being started or stopped, the `coder_workspace` data source
355+
provides some useful parameters. See the [documentation for the `coder`
356+
Terraform
357+
provider](https://registry.terraform.io/providers/coder/coder/latest/docs/data-sources/workspace)
301358
for more information.
302359

303-
For example, the [Docker quick-start template](https://github.com/coder/coder/tree/main/examples/templates/docker)
304-
sets a few environment variables based on the username and email address of the workspace's owner, so
305-
that you can make Git commits immediately without any manual configuration:
360+
For example, the [Docker quick-start
361+
template](https://github.com/coder/coder/tree/main/examples/templates/docker)
362+
sets a few environment variables based on the username and email address of the
363+
workspace's owner, so that you can make Git commits immediately without any
364+
manual configuration:
306365

307366
```tf
308367
resource "coder_agent" "main" {
@@ -316,12 +375,15 @@ resource "coder_agent" "main" {
316375
}
317376
```
318377

319-
You can add these environment variable definitions to your own templates, or customize them however
320-
you like.
378+
You can add these environment variable definitions to your own templates, or
379+
customize them however you like.
321380

322381
## Troubleshooting templates
323382

324-
Occasionally, you may run into scenarios where a workspace is created, but the agent is not connected. This means the agent or [init script](https://github.com/coder/coder/tree/main/provisionersdk/scripts) has failed on the resource.
383+
Occasionally, you may run into scenarios where a workspace is created, but the
384+
agent is not connected. This means the agent or [init
385+
script](https://github.com/coder/coder/tree/main/provisionersdk/scripts) has
386+
failed on the resource.
325387

326388
```sh
327389
$ coder ssh myworkspace
@@ -332,18 +394,23 @@ While troubleshooting steps vary by resource, here are some general best
332394
practices:
333395

334396
- Ensure the resource has `curl` installed
335-
- Ensure the resource can `curl` your Coder [access URL](./admin/configure.md#access-url)
336-
- Manually connect to the resource and check the agent logs (e.g., `docker exec` or AWS console)
397+
- Ensure the resource can `curl` your Coder [access
398+
URL](./admin/configure.md#access-url)
399+
- Manually connect to the resource and check the agent logs (e.g., `kubectl
400+
exec`, `docker exec` or AWS console)
337401
- The Coder agent logs are typically stored in `/var/log/coder-agent.log`
338-
- The Coder agent startup script logs are typically stored in `/var/log/coder-startup-script.log`
402+
- The Coder agent startup script logs are typically stored in
403+
`/var/log/coder-startup-script.log`
339404

340405
## Template permissions (enterprise)
341406

342-
Template permissions can be used to give users and groups access to specific templates. [Learn more about RBAC](./admin/rbac.md).
407+
Template permissions can be used to give users and groups access to specific
408+
templates. [Learn more about RBAC](./admin/rbac.md).
343409

344410
## Community Templates
345411

346-
You can see a list of community templates by our users [here](https://github.com/coder/coder/blob/main/examples/templates/community-templates.md).
412+
You can see a list of community templates by our users
413+
[here](https://github.com/coder/coder/blob/main/examples/templates/community-templates.md).
347414

348415
## Next Steps
349416

0 commit comments

Comments
 (0)