Skip to content

Commit ecfd855

Browse files
feat(template): documentation for coder templates
1 parent 4095330 commit ecfd855

File tree

1 file changed

+314
-0
lines changed

1 file changed

+314
-0
lines changed

docs/about/template.md

Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
# Template
2+
3+
Coder templates are **Terraform-based configurations** that define the infrastructure and environment setup for developer workspaces. They serve as the blueprint for provisioning consistent, reproducible development environments across your organization.
4+
5+
![Templates in Coder Dashboard](../images/admin/templates/starter-templates.png)
6+
7+
## What are templates?
8+
9+
A Coder template is a [Terraform](https://terraform.io) configuration that uses the [Coder Terraform Provider](https://registry.terraform.io/providers/coder/coder/latest) to:
10+
11+
- **Provision infrastructure**: Define compute resources (VMs, containers, etc.) where workspaces run
12+
- **Configure environments**: Set up development tools, IDEs, and workspace apps
13+
- **Manage lifecycle**: Control how workspaces start, stop, and update
14+
- **Enforce standards**: Ensure consistent development environments across teams
15+
16+
Templates are the foundation of Coder's **Infrastructure as Code** approach to developer environments.
17+
18+
## How templates work
19+
20+
When a developer creates a workspace, Coder:
21+
22+
1. **Applies the template**: Runs `terraform apply` using the template configuration
23+
2. **Provisions infrastructure**: Creates the underlying compute resources (VM, container, etc.)
24+
3. **Starts the agent**: Deploys the Coder agent to enable connections and apps
25+
4. **Configures environment**: Runs startup scripts and initializes development tools
26+
27+
When a workspace is stopped or restarted, Coder manages the infrastructure lifecycle according to the template's resource persistence configuration.
28+
29+
## Template structure
30+
31+
### Core components
32+
33+
Every Coder template contains several key Terraform resources:
34+
35+
#### 1. **Coder Agent** (`coder_agent`)
36+
The agent runs inside the workspace and enables SSH access, port forwarding, and IDE connections:
37+
38+
```tf
39+
resource "coder_agent" "main" {
40+
os = "linux"
41+
arch = "amd64"
42+
dir = "/home/coder"
43+
startup_script = <<-EOT
44+
#!/bin/bash
45+
# Install development tools
46+
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
47+
sudo apt-get install -y nodejs
48+
EOT
49+
}
50+
```
51+
52+
#### 2. **Infrastructure Resources**
53+
Standard Terraform resources that provision the underlying compute:
54+
55+
```tf
56+
# Example: Docker container
57+
resource "docker_container" "workspace" {
58+
count = data.coder_workspace.me.start_count
59+
image = "ubuntu:22.04"
60+
name = "coder-${data.coder_workspace_owner.me.name}-${data.coder_workspace.me.name}"
61+
env = ["CODER_AGENT_TOKEN=${coder_agent.main.token}"]
62+
command = ["sh", "-c", coder_agent.main.init_script]
63+
}
64+
```
65+
66+
#### 3. **Workspace Data Sources**
67+
Provide information about the current workspace and user:
68+
69+
```tf
70+
data "coder_workspace" "me" {}
71+
data "coder_workspace_owner" "me" {}
72+
data "coder_provisioner" "me" {}
73+
```
74+
75+
#### 4. **Parameters** (`coder_parameter`)
76+
Allow users to customize workspace creation:
77+
78+
```tf
79+
data "coder_parameter" "instance_type" {
80+
name = "instance_type"
81+
display_name = "Instance Type"
82+
description = "Select the VM size for your workspace"
83+
default = "t3.micro"
84+
option {
85+
name = "Small (t3.micro)"
86+
value = "t3.micro"
87+
}
88+
option {
89+
name = "Medium (t3.small)"
90+
value = "t3.small"
91+
}
92+
}
93+
```
94+
95+
#### 5. **Registry Modules**
96+
Add development tools and functionality using community modules:
97+
98+
```tf
99+
# VS Code via registry module (most common approach)
100+
module "code-server" {
101+
source = "registry.coder.com/modules/code-server/coder"
102+
version = "~> 1.0"
103+
agent_id = coder_agent.main.id
104+
}
105+
106+
# JetBrains IDEs via registry module
107+
module "jetbrains_gateway" {
108+
source = "registry.coder.com/modules/jetbrains-gateway/coder"
109+
version = "~> 1.0"
110+
agent_id = coder_agent.main.id
111+
}
112+
```
113+
114+
Note: While templates *can* include `coder_app` and `coder_script` resources directly, it's more common to use registry modules that provide this functionality in a reusable way.
115+
116+
### File organization
117+
118+
A typical template directory contains:
119+
120+
```
121+
my-template/
122+
├── main.tf # Main Terraform configuration
123+
└── README.md # Template documentation
124+
```
125+
126+
Some templates may include additional files:
127+
128+
```
129+
advanced-template/
130+
├── main.tf # Main Terraform configuration
131+
├── README.md # Template documentation
132+
├── architecture.svg # Architecture diagram (optional)
133+
└── cloud-init/ # Cloud-init templates (optional)
134+
├── cloud-config.yaml.tftpl
135+
└── userdata.sh.tftpl
136+
```
137+
138+
## Template types
139+
140+
Coder provides templates for different deployment scenarios and organizational needs:
141+
142+
1. **Starter Templates** - Pre-built templates for common platforms:
143+
- [Docker](https://registry.coder.com/templates/docker): Containerized workspaces
144+
- [Kubernetes](https://registry.coder.com/templates/kubernetes): Pod-based workspaces
145+
- [AWS EC2](https://registry.coder.com/templates/aws-linux): Virtual machines on AWS
146+
- [Azure VMs](https://registry.coder.com/templates/azure-linux): Virtual machines on Azure
147+
- [GCP Compute](https://registry.coder.com/templates/gcp-linux): Virtual machines on Google Cloud
148+
149+
2. **Community Templates** - Templates shared by the Coder community for specialized use cases like specific frameworks, tools, or development stacks.
150+
151+
3. **Custom Templates** - Organization-specific templates tailored to your infrastructure, security requirements, and development workflows.
152+
153+
## Template registry integration
154+
155+
The [Coder Registry](https://registry.coder.com) provides reusable components to simplify template development:
156+
157+
1. **Templates** - Complete workspace configurations for various platforms
158+
2. **Modules** - Reusable Terraform components that provide `coder_app`, `coder_script`, and other functionality
159+
160+
Templates leverage registry modules to add development tools and features:
161+
162+
```tf
163+
# Add VS Code to any workspace
164+
module "code-server" {
165+
source = "registry.coder.com/modules/code-server/coder"
166+
version = "~> 1.0"
167+
agent_id = coder_agent.main.id
168+
}
169+
170+
# Clone Git repositories
171+
module "git-clone" {
172+
source = "registry.coder.com/modules/git-clone/coder"
173+
version = "~> 1.0"
174+
agent_id = coder_agent.main.id
175+
url = "https://github.com/myorg/myproject"
176+
}
177+
178+
# Configure dotfiles
179+
module "dotfiles" {
180+
source = "registry.coder.com/modules/dotfiles/coder"
181+
version = "~> 1.0"
182+
agent_id = coder_agent.main.id
183+
url = "https://github.com/myuser/dotfiles"
184+
}
185+
```
186+
187+
## Template lifecycle
188+
189+
Templates follow a structured lifecycle from creation to workspace deployment:
190+
191+
1. **Creation and versioning**
192+
- Templates are created from Terraform configurations
193+
- Each template push creates a new version
194+
- Coder tracks version history and allows rollbacks
195+
196+
2. **Workspace provisioning**
197+
- Users create workspaces from templates
198+
- Parameters are collected during workspace creation
199+
- Infrastructure is provisioned via Terraform
200+
201+
3. **Updates and maintenance**
202+
- Template admins can push new versions
203+
- Users receive notifications about available updates
204+
- Updates can be applied manually or automatically
205+
206+
4. **Resource persistence**
207+
Templates define which resources persist across workspace restarts:
208+
- **Ephemeral**: Recreated on each start (compute instances)
209+
- **Persistent**: Preserved across restarts (home directories, databases)
210+
211+
## Advanced features
212+
213+
### **Workspace tags**
214+
Control provisioner routing and resource placement:
215+
216+
```tf
217+
data "coder_workspace_tags" "custom" {
218+
tags = {
219+
"region" = data.coder_parameter.region.value
220+
"team" = "developers"
221+
"project" = data.coder_parameter.project.value
222+
}
223+
}
224+
```
225+
226+
### **Metadata and monitoring**
227+
Display resource information and metrics:
228+
229+
```tf
230+
resource "coder_metadata" "info" {
231+
resource_id = coder_agent.main.id
232+
item {
233+
key = "CPU Usage"
234+
value = "coder stat cpu"
235+
script = true
236+
}
237+
}
238+
```
239+
240+
### **Pre-built workspaces** (Premium)
241+
Maintain pools of ready-to-use workspaces for faster provisioning:
242+
243+
```tf
244+
resource "coder_workspace_preset" "production" {
245+
name = "production-env"
246+
parameters = {
247+
instance_type = "c5.xlarge"
248+
region = "us-west-2"
249+
}
250+
prebuilds {
251+
count = 3
252+
}
253+
}
254+
```
255+
256+
## Template administration
257+
258+
### Permissions
259+
260+
Template access is controlled through role-based permissions:
261+
262+
1. **Template Admin** - Can create and modify templates
263+
2. **Template User** - Can create workspaces from templates
264+
3. **Organization roles** - Control template access across organizations
265+
266+
### Best practices
267+
268+
1. Start with starter templates and customize for your needs
269+
2. Use version control for template source code
270+
3. Implement CI/CD for template testing and deployment
271+
4. Leverage modules for common functionality
272+
5. Document template parameters and requirements
273+
274+
### Management tools
275+
276+
1. **Web UI** - Edit templates directly in the Coder dashboard
277+
2. **CLI** - Use `coder templates` commands for programmatic management
278+
3. **API** - Integrate template management into existing tools
279+
280+
## Template ecosystem
281+
282+
### Provider support
283+
284+
Templates can use any Terraform provider:
285+
286+
1. **Cloud providers** - AWS, Azure, GCP, DigitalOcean
287+
2. **Container platforms** - Docker, Kubernetes, OpenShift
288+
3. **Virtualization** - VMware, Proxmox, Hyper-V
289+
4. **Specialized providers** - Coder, Envbuilder, custom providers
290+
291+
### Integration points
292+
293+
1. **Identity providers** - OIDC, SAML, GitHub, GitLab
294+
2. **Secret management** - HashiCorp Vault, cloud secret managers
295+
3. **Container registries** - Docker Hub, ECR, ACR, GCR
296+
4. **Source control** - GitHub, GitLab, Bitbucket, Azure DevOps
297+
298+
## Getting started
299+
300+
Choose your approach based on your needs and experience level:
301+
302+
1. **Start with a starter template** - Use pre-built templates for common platforms
303+
2. **Customize existing templates** - Modify starter templates for your needs
304+
3. **Build from scratch** - Create templates for specialized infrastructure
305+
306+
Templates are the foundation of effective developer environment management with Coder. They enable organizations to provide consistent, secure, and productive development environments while maintaining the flexibility to adapt to different team needs and infrastructure requirements.
307+
308+
### Next steps
309+
310+
- [Creating Templates](../admin/templates/creating-templates.md) - Learn how to create your first template
311+
- [Template Tutorial](../tutorials/template-from-scratch.md) - Step-by-step guide to building a template
312+
- [Extending Templates](../admin/templates/extending-templates/index.md) - Add advanced features and customization
313+
- [Managing Templates](../admin/templates/managing-templates/index.md) - Best practices for template lifecycle management
314+
- [Coder Registry](https://registry.coder.com) - Browse available templates and modules

0 commit comments

Comments
 (0)