Skip to content

Commit 7119be1

Browse files
committed
add diagram
1 parent 80215cf commit 7119be1

File tree

1 file changed

+46
-39
lines changed

1 file changed

+46
-39
lines changed

docs/templates/writing/index.md

+46-39
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
# Writing Coder templates
1+
# Template structure
22

33
Coder templates are written in [Terraform](https://terraform.io). All Terraform modules, resources, and properties can be provisioned by Coder. The Coder server essentially runs a `terraform apply` every time a workspace is created/started/stopped.
44

55
Haven't written Terraform before? Check out Hashicorp's [Getting Started Guides](https://developer.hashicorp.com/terraform/tutorials).
66

7-
## Key concepts in templates
7+
## Architecture
88

9-
There are some key concepts you should consider when writing templates.
9+
This is a simplified diagram of our [Kubernetes example template](https://github.com/coder/coder/tree/main/examples/templates/kubernetes). Keep reading for a breakdown of each concept.
1010

11-
### Coder Terraform Provider
11+
![Template architecture](https://user-images.githubusercontent.com/22407953/257021139-8c95a731-c131-4c4d-85cc-eed6a52e015c.png)
1212

13-
The [Coder Terraform provider](https://registry.terraform.io/providers/coder/coder/latest) makes it possible for standard Terraform resources (e.g. `docker_container`) to connect to Coder. Additionally, the provider lets you to customize the behavior of workspaces using your template.
13+
## Coder Terraform Provider
14+
15+
The [Coder Terraform provider](https://registry.terraform.io/providers/coder/coder/latest) makes it possible for standard Terraform resources (e.g. `kubernetes_deployment`) to connect to Coder. Additionally, the provider lets you to customize the behavior of workspaces using your template.
1416

1517
```hcl
1618
terraform {
@@ -22,54 +24,55 @@ terraform {
2224
}
2325
```
2426

25-
### coder_workspace
27+
## coder_agent
2628

27-
This data source provides details about the state of a workspace, such as its name, owner, and whether the workspace is being started or stopped.
29+
All templates need to create & run a Coder agent in order for developers to connect to their workspaces. The Coder agent is a service that runs inside the compute aspect of your workspace (typically a VM or container). You do not need to have any open ports, but the compute will need `curl` access to the Coder server.
2830

29-
The following snippet will create a container when the workspace is being started, and delete the container when it is stopped using Terraform's [count](https://developer.hashicorp.com/terraform/language/meta-arguments/count) meta-argument.
31+
This snippet creates the agent, runs it inside the container via the `entrypoint`, and authenticates to Coder via the agent's token.
3032

3133
```hcl
32-
data "coder_workspace" "me" {}
34+
resource "coder_agent" "main" {
35+
os = "linux"
36+
arch = "amd64"
37+
}
3338
34-
# Delete the container when workspace is stopped (count = 0)
35-
resource "docker_container" "workspace" {
36-
count = data.coder_workspace.me.transition == "start" ? 1 : 0
39+
resource "kubernetes_deployment" "workspace" {
40+
entrypoint = ["sh", "-c", coder_agent.main.init_script]
41+
env = ["CODER_AGENT_TOKEN=${coder_agent.main.token}"]
3742
# ...
3843
}
39-
40-
# Persist the volume, even if stopped
41-
resource "docker_volume" "projects" {}
4244
```
4345

44-
### coder_agent
46+
Agents can also run startup scripts, set environment variables, and provide [metadata](../agent-metadata.md) about the workspace (e.g. CPU usage). Read the [coder_agent docs](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent#startup_script) for more details.
4547

46-
All templates need to create & run a Coder agent in order for developers to connect to their workspaces. The Coder agent is a service that runs inside the compute aspect of your workspace (typically a VM or container).
48+
## coder_workspace
4749

48-
This snippet creates the agent, runs it inside the container via the `entrypoint`, and authenticates to Coder via the agent's token.
50+
This data source provides details about the state of a workspace, such as its name, owner, and whether the workspace is being started or stopped.
51+
52+
The following snippet will create a container when the workspace is being started, and delete the container when it is stopped using Terraform's [count](https://developer.hashicorp.com/terraform/language/meta-arguments/count) meta-argument.
4953

5054
```hcl
51-
resource "coder_agent" "main" {
52-
os = "linux"
53-
arch = "amd64"
54-
}
55+
data "coder_workspace" "me" {}
5556
56-
resource "docker_container" "workspace" {
57-
entrypoint = ["sh", "-c", coder_agent.main.init_script]
58-
env = ["CODER_AGENT_TOKEN=${coder_agent.main.token}"]
57+
# Delete the container when workspace is stopped (count = 0)
58+
resource "kubernetes_deployment" "workspace" {
59+
count = data.coder_workspace.me.transition == "start" ? 1 : 0
5960
# ...
6061
}
61-
```
6262
63-
Agents can also run startup scripts, set environment variables, and provide metadata about the workspace (e.g. CPU usage). Read the [coder_agent docs](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent#startup_script) for more details.
63+
# Persist the volume, even if stopped
64+
resource "docker_volume" "projects" {}
65+
```
6466

65-
### coder_app
67+
## coder_app
6668

67-
Web apps that are running inside the workspace (e.g. `http://localhost:8080`) can be forwarded to the Coder dashboard with the `coder_app` resource. This is commonly used for [web IDEs](../ides/web-ides.md) such as code-server, RStudio, and JupyterLab. External apps, such as links to internal wikis or cloud consoles can also be embedded here.
69+
Web apps that are running inside the workspace (e.g. `http://localhost:8080`) can be forwarded to the Coder dashboard with the `coder_app` resource. This is commonly used for [web IDEs](../../ides/web-ides.md) such as code-server, RStudio, and JupyterLab. External apps, such as links to internal wikis or cloud consoles can also be embedded here.
6870

6971
Apps are rendered on the workspace page:
7072

71-
![]()
73+
![Apps in Coder workspace](https://user-images.githubusercontent.com/22407953/257020191-97a19ff0-83ca-4275-a699-113f6c97a9ab.png)
7274

75+
The apps themselves have to be installed & running on the workspace. This can be done via the agent's startup script. See [web IDEs](../ides/web-ides.md) for some examples.
7376

7477
```hcl
7578
# coder_agent will install and start code-server
@@ -83,18 +86,22 @@ resource "coder_agent" "main" {
8386
8487
# expose code-server on workspace via a coder_app
8588
resource "coder_app" "code-server" {
86-
icon = "/icon/code.svg"
87-
name = "code-server"
88-
slug = "code"
89-
url = "http://localhost:8080"
89+
agent_id = coder_agent.main.id
90+
icon = "/icon/code.svg"
91+
display_name = "code-server"
92+
slug = "code"
93+
url = "http://localhost:8080"
9094
}
9195
9296
# link to an external site
9397
resource "coder_app" "getting-started" {
94-
icon = "/icon/help.svg"
95-
name = "getting-started"
96-
slug = "getting-started"
97-
url = "https://wiki.example.com/coder/quickstart"
98-
external = true
98+
agent_id = coder_agent.main.id
99+
icon = "/emojis/1f4dd.png"
100+
display_name = "getting-started"
101+
slug = "getting-started"
102+
url = "https://wiki.example.com/coder/quickstart"
103+
external = true
99104
}
100105
```
106+
107+
Lorem ipsum

0 commit comments

Comments
 (0)