Skip to content

Commit ff71e83

Browse files
committed
update template main.tf
1 parent 399c830 commit ff71e83

File tree

1 file changed

+24
-40
lines changed

1 file changed

+24
-40
lines changed

docs/tutorials/template-from-scratch.md

+24-40
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,13 @@ To follow this guide, you'll need:
1414
[Docker](https://docs.docker.com/get-docker/) and [Coder](../install/index.md)
1515
installed on it.
1616

17-
> When setting up your computer or computing instance, make sure to install
18-
> Docker first, then Coder. Otherwise, you'll need to add the `coder` user to
19-
> the `docker` group.
17+
- When setting up your computer or computing instance, make sure to install Docker first, then Coder. Otherwise, you'll need to add the `coder` user to the `docker` group.
2018

2119
- The URL for your Coder instance. If you're running Coder locally, the default
2220
URL is [http://127.0.0.1:3000](http://127.0.0.1:3000).
2321

2422
- A text editor. For this tour, we use [GNU nano](https://nano-editor.org/).
2523

26-
> Haven't written Terraform before? Check out Hashicorp's
27-
> [Getting Started Guides](https://developer.hashicorp.com/terraform/tutorials).
28-
2924
## What's in a template
3025

3126
The main part of a Coder template is a [Terraform](https://terraform.io) `tf`
@@ -36,6 +31,9 @@ Coder can provision all Terraform modules, resources, and properties. The Coder
3631
server essentially runs a `terraform apply` every time a workspace is created,
3732
started, or stopped.
3833

34+
> Haven't written Terraform before? Check out Hashicorp's
35+
> [Getting Started Guides](https://developer.hashicorp.com/terraform/tutorials).
36+
3937
Here's a simplified diagram that shows the main parts of the template we'll
4038
create.
4139

@@ -47,10 +45,8 @@ On your local computer, create a directory for your template and create the
4745
`Dockerfile`.
4846

4947
```sh
50-
mkdir template-tour
51-
cd template-tour
52-
mkdir build
53-
nano build/Dockerfile
48+
mkdir -p template-tour/build && cd $_
49+
nano Dockerfile
5450
```
5551

5652
You'll enter a simple `Dockerfile` that starts with the
@@ -72,7 +68,6 @@ RUN useradd --groups sudo --no-create-home --shell /bin/bash ${USER} \
7268
&& chmod 0440 /etc/sudoers.d/${USER}
7369
USER ${USER}
7470
WORKDIR /home/${USER}
75-
7671
```
7772

7873
Notice how `Dockerfile` adds a few things to the parent `ubuntu` image, which
@@ -83,7 +78,7 @@ your template needs later:
8378

8479
## 2. Set up template providers
8580

86-
Now you can edit the Terraform file, which provisions the workspace's resources.
81+
Edit the Terraform file to provision the workspace's resources:
8782

8883
```shell
8984
nano main.tf
@@ -97,31 +92,28 @@ terraform {
9792
required_providers {
9893
coder = {
9994
source = "coder/coder"
100-
version = "~> 0.8.3"
10195
}
10296
docker = {
10397
source = "kreuzwerker/docker"
104-
version = "~> 3.0.1"
10598
}
10699
}
107100
}
108101
109-
provider "coder" {
102+
locals {
103+
username = data.coder_workspace_owner.me.name
110104
}
111105
112-
provider "docker" {
106+
data "coder_provisioner" "me" {
113107
}
114108
115-
locals {
116-
username = data.coder_workspace.me.owner
109+
provider "docker" {
117110
}
118111
119-
data "coder_provisioner" "me" {
112+
provider "coder" {
120113
}
121114
122115
data "coder_workspace" "me" {
123116
}
124-
125117
```
126118

127119
Notice that the `provider` blocks for `coder` and `docker` are empty. In a more
@@ -132,8 +124,7 @@ The
132124
[`coder_workspace`](https://registry.terraform.io/providers/coder/coder/latest/docs/data-sources/workspace)
133125
data source provides details about the state of a workspace, such as its name,
134126
owner, and so on. The data source also lets us know when a workspace is being
135-
started or stopped. We'll take advantage of this information in later steps to
136-
do these things:
127+
started or stopped. We'll take advantage of this information in later steps to:
137128

138129
- Set some environment variables based on the workspace owner.
139130
- Manage ephemeral and persistent storage.
@@ -150,26 +141,25 @@ You do not need to have any open ports on the compute aspect, but the agent
150141
needs `curl` access to the Coder server. Remember that we installed `curl` in
151142
`Dockerfile`, earlier.
152143

153-
This snippet creates the agent:
144+
Add this snippet below the last closing `}` in `main.tf` to create the agent:
154145

155146
```tf
156147
resource "coder_agent" "main" {
157148
arch = data.coder_provisioner.me.arch
158149
os = "linux"
159-
startup_script_timeout = 180
160150
startup_script = <<-EOT
161151
set -e
162152
163153
# install and start code-server
164-
curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server --version 4.11.0
154+
curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server
165155
/tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 &
166156
EOT
167157
168158
env = {
169-
GIT_AUTHOR_NAME = "${data.coder_workspace.me.owner}"
170-
GIT_COMMITTER_NAME = "${data.coder_workspace.me.owner}"
171-
GIT_AUTHOR_EMAIL = "${data.coder_workspace.me.owner_email}"
172-
GIT_COMMITTER_EMAIL = "${data.coder_workspace.me.owner_email}"
159+
GIT_AUTHOR_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name)
160+
GIT_AUTHOR_EMAIL = "${data.coder_workspace_owner.me.email}"
161+
GIT_COMMITTER_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name)
162+
GIT_COMMITTER_EMAIL = "${data.coder_workspace_owner.me.email}"
173163
}
174164
175165
metadata {
@@ -188,11 +178,10 @@ resource "coder_agent" "main" {
188178
timeout = 1
189179
}
190180
}
191-
192181
```
193182

194183
Because Docker is running locally in the Coder server, there is no need to
195-
authenticate `coder_agent`. But if your `coder_agent` were running on a remote
184+
authenticate `coder_agent`. But if your `coder_agent` is running on a remote
196185
host, your template would need
197186
[authentication credentials](../admin/external-auth.md).
198187

@@ -229,7 +218,7 @@ This is commonly used for
229218
[web IDEs](../user-guides/workspace-access/web-ides.md) such as
230219
[code-server](https://coder.com/docs/code-server), RStudio, and JupyterLab.
231220

232-
To install and code-server in the workspace, remember that we installed it in
221+
To install code-server in the workspace, remember that we installed it in
233222
the `startup_script` argument in `coder_agent`. We make it available from a
234223
workspace with a `coder_app` resource. See
235224
[web IDEs](../user-guides/workspace-access/web-ides.md) for more examples.
@@ -250,11 +239,10 @@ resource "coder_app" "code-server" {
250239
threshold = 6
251240
}
252241
}
253-
254242
```
255243

256244
You can also use a `coder_app` resource to link to external apps, such as links
257-
to wikis or cloud consoles.
245+
to wikis or cloud consoles:
258246

259247
```tf
260248
resource "coder_app" "coder-server-doc" {
@@ -264,7 +252,6 @@ resource "coder_app" "coder-server-doc" {
264252
url = "https://coder.com/docs/code-server"
265253
external = true
266254
}
267-
268255
```
269256

270257
## 5. Persistent and ephemeral resources
@@ -296,7 +283,6 @@ resource "docker_volume" "home_volume" {
296283
ignore_changes = all
297284
}
298285
}
299-
300286
```
301287

302288
For details, see
@@ -305,7 +291,7 @@ For details, see
305291
## 6. Set up the Docker container
306292

307293
To set up our Docker container, our template has a `docker_image` resource that
308-
uses `build/Dockerfile`, which we created earlier.
294+
uses `build/Dockerfile`, which we created earlier:
309295

310296
```tf
311297
resource "docker_image" "main" {
@@ -320,7 +306,6 @@ resource "docker_image" "main" {
320306
dir_sha1 = sha1(join("", [for f in fileset(path.module, "build/*") : filesha1(f)]))
321307
}
322308
}
323-
324309
```
325310

326311
Our `docker_container` resource uses `coder_workspace` `start_count` to start
@@ -331,7 +316,7 @@ resource "docker_container" "workspace" {
331316
count = data.coder_workspace.me.start_count
332317
image = docker_image.main.name
333318
# Uses lower() to avoid Docker restriction on container names.
334-
name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}"
319+
name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}"
335320
# Hostname makes the shell more user friendly: coder@my-workspace:~$
336321
hostname = data.coder_workspace.me.name
337322
# Use the docker gateway if the access URL is 127.0.0.1
@@ -349,7 +334,6 @@ resource "docker_container" "workspace" {
349334
read_only = false
350335
}
351336
}
352-
353337
```
354338

355339
## 7. Create the template in Coder

0 commit comments

Comments
 (0)