This guide documents best practices and workflows for developing and iterating on Coder templates.
Coder templates are written in Terraform and define the infrastructure required for workspaces. These templates create VMs, containers, and other resources that developers use as their development environments.
-
Create a new template from scratch or existing examples:
coder templates init
-
Develop the template locally: Edit the
main.tf
file to define your workspace infrastructure. -
Push the template to the Coder server:
coder templates push my-template --directory /path/to/template
-
Create a test workspace:
coder create test-workspace --template my-template
When making changes to templates, follow these steps:
-
Make incremental changes: Start with a known working template and make small, incremental changes.
-
Track template versions: Push new versions and note which ones work correctly.
coder templates versions list my-template
-
Revert to working versions if needed:
coder templates versions promote --template=my-template --template-version=happy_feynman3
-
Add logging: Use logging in startup scripts to help diagnose issues.
-
Add meaningful logging:
- Use simple file-based logging rather than transcript logging in PowerShell
- Include timestamps and clear step identifiers
- Log both success and error conditions
-
Verify paths and conditions before use:
if (-not (Test-Path -Path $SomePath)) { # Log and handle missing path }
-
Use absolute paths: Always prefer absolute paths over relative paths.
-
Structure scripts for diagnostics:
- Add status scripts to check workspace health
- Include self-healing measures where possible
-
Simplify path handling: Avoid complex path manipulation in scripts.
-
Develop incrementally:
- Start with a minimal working template
- Add features one at a time, testing each addition
-
Agent connection issues:
- Check agent logs (e.g.,
C:\Windows\Temp\coder_agent.log
) - Verify network connectivity
- Check agent authentication method
- Check agent logs (e.g.,
-
Startup script failures:
- Review startup logs (e.g.,
C:\Windows\Temp\coder_startup.log
) - Test scripts outside of Coder in a similar environment
- Add more detailed logging
- Review startup logs (e.g.,
-
Resource provisioning problems:
- Check Terraform state and logs
- Verify IAM permissions
- Test resource creation outside of Coder
-
SSH into a workspace:
coder config-ssh # Configure SSH for Coder workspaces ssh coder.my-workspace # Connect to workspace
-
View workspace details:
coder show my-workspace
-
Monitor logs:
ssh coder.my-workspace cat /path/to/logfile
-
Check workspace agent status:
coder show my-workspace
-
List template versions:
coder templates versions list my-template
-
Download a specific template version:
coder templates pull my-template --version=happy_feynman3 /path/to/destination
-
Promote a template version to active:
coder templates versions promote --template=my-template --template-version=happy_feynman3
-
Export a template:
coder templates pull my-template /path/to/export
-
Version control: Store templates in Git repositories for better collaboration and version tracking.
-
Path handling: Windows uses backslashes (
\
) while Terraform uses forward slashes. Be careful with escaping. -
PowerShell vs CMD: Prefer PowerShell for complex scripts, but be aware of execution policy restrictions.
-
Script logging: Use file output instead of transcript logging:
"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - Your log message" | Out-File -Append $LogFile
-
Disk initialization: Be thorough with disk detection and mounting logic:
$disk = Get-Disk | Where-Object { $_.Number -eq 1 } if ($disk -and $disk.PartitionStyle -eq "RAW") { # Initialize and format disk }
-
Software installation: Always verify paths and use error handling with installers.
Developing Coder templates requires careful planning, incremental changes, and good logging practices. By following these guidelines, you can create robust templates that provide consistent development environments for your team.