Skip to content

Commit 7cc96f5

Browse files
authored
chore(docs): add recommendations for dependency management (coder#13400)
1 parent 7a7bef0 commit 7cc96f5

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

docs/images/icons/dependency.svg

Lines changed: 4 additions & 0 deletions
Loading

docs/manifest.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@
143143
"description": "Best practices for writing templates",
144144
"path": "./templates/best-practices.md",
145145
"children": [
146+
{
147+
"title": "Template Dependencies",
148+
"description": "Manage dependencies of your templates",
149+
"path": "./templates/dependencies.md",
150+
"icon_path": "./images/icons/dependency.svg"
151+
},
146152
{
147153
"title": "Change management",
148154
"description": "Versioning templates with git and CI",

docs/templates/dependencies.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Template Dependencies
2+
3+
When creating Coder templates, it is unlikely that you will just be using
4+
built-in providers. Part of Terraform's flexibility stems from its rich plugin
5+
ecosystem, and it makes sense to take advantage of this.
6+
7+
That having been said, here are some recommendations to follow, based on the
8+
[Terraform documentation](https://developer.hashicorp.com/terraform/tutorials/configuration-language/provider-versioning).
9+
10+
Following these recommendations will:
11+
12+
- **Prevent unexpected changes:** Your templates will use the same versions of
13+
Terraform providers each build. This will prevent issues related to changes in
14+
providers.
15+
- **Improve build performance:** Coder caches provider versions on each build.
16+
If the same provider version can be re-used on subsequent builds, Coder will
17+
simply re-use the cached version if it is available.
18+
- **Improve build reliability:** As some providers are hundreds of megabytes in
19+
size, interruptions in connectivity to the Terraform registry during a
20+
workspace build can result in a failed build. If Coder is able to re-use a
21+
cached provider version, the likelihood of this is greatly reduced.
22+
23+
## Lock your provider and module versions
24+
25+
If you add a Terraform provider to `required_providers` without specifying a
26+
version requirement, Terraform will always fetch the latest version on each
27+
invocation:
28+
29+
```terraform
30+
terraform {
31+
required_providers {
32+
coder = {
33+
source = "coder/coder"
34+
}
35+
frobnicate = {
36+
source = "acme/frobnicate"
37+
}
38+
}
39+
}
40+
```
41+
42+
Any new releases of the `coder` or `frobnicate` providers will be picked up upon
43+
the next time a workspace is built using this template. This may include
44+
breaking changes.
45+
46+
To prevent this, add a
47+
[version constraint](https://developer.hashicorp.com/terraform/language/expressions/version-constraints)
48+
to each provider in the `required_providers` block:
49+
50+
```terraform
51+
terraform {
52+
required_providers {
53+
coder = {
54+
source = "coder/coder"
55+
version = ">= 0.2, < 0.3"
56+
}
57+
frobnicate = {
58+
source = "acme/frobnicate"
59+
version = "~> 1.0.0"
60+
}
61+
}
62+
}
63+
```
64+
65+
In the above example, the `coder/coder` provider will be limited to all versions
66+
above or equal to `0.2.0` and below `0.3.0`, while the `acme/frobnicate`
67+
provider will be limited to all versions matching `1.0.x`.
68+
69+
The above also applies to Terraform modules. In the below example, the module
70+
`razzledazzle` is locked to version `1.2.3`.
71+
72+
```terraform
73+
module "razzledazzle" {
74+
source = "registry.example.com/modules/razzle/dazzle"
75+
version = "1.2.3"
76+
foo = "bar"
77+
}
78+
```
79+
80+
## Use a Dependency Lock File
81+
82+
Terraform allows creating a
83+
[dependency lock file](https://developer.hashicorp.com/terraform/language/files/dependency-lock)
84+
to track which provider versions were selected previously. This allows you to
85+
ensure that the next workspace build uses the same provider versions as with the
86+
last build.
87+
88+
To create a new Terraform lock file, run the
89+
[`terraform init` command](https://developer.hashicorp.com/terraform/cli/commands/init)
90+
inside a folder containing the Terraform source code for a given template.
91+
92+
This will create a new file named `.terraform.lock.hcl` in the current
93+
directory. When you next run [`coder templates push`](../cli/templates_push.md),
94+
the lock file will be stored alongside with the other template source code.
95+
96+
> Note: Terraform best practices also recommend checking in your
97+
> `.terraform.lock.hcl` into Git or other VCS.
98+
99+
The next time a workspace is built from that template, Coder will make sure to
100+
use the same versions of those providers as specified in the lock file.
101+
102+
If, at some point in future, you need to update the providers and versions you
103+
specified within the version constraints of the template, run
104+
105+
```console
106+
terraform init -upgrade
107+
```
108+
109+
This will check each provider, check the newest satisfiable version based on the
110+
version constraints you specified, and update the `.terraform.lock.hcl` with
111+
those new versions. When you next run `coder templates push`, again, the updated
112+
lock file will be stored and used to determine the provider versions to use for
113+
subsequent workspace builds.

0 commit comments

Comments
 (0)