Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 205f0fd

Browse files
committed
template dependencies
1 parent 1344df1 commit 205f0fd

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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
94+
[`coder templates push`](../reference/cli/templates_push.md), the lock file will
95+
be stored alongside with the other template source code.
96+
97+
> Note: Terraform best practices also recommend checking in your
98+
> `.terraform.lock.hcl` into Git or other VCS.
99+
100+
The next time a workspace is built from that template, Coder will make sure to
101+
use the same versions of those providers as specified in the lock file.
102+
103+
If, at some point in future, you need to update the providers and versions you
104+
specified within the version constraints of the template, run
105+
106+
```console
107+
terraform init -upgrade
108+
```
109+
110+
This will check each provider, check the newest satisfiable version based on the
111+
version constraints you specified, and update the `.terraform.lock.hcl` with
112+
those new versions. When you next run `coder templates push`, again, the updated
113+
lock file will be stored and used to determine the provider versions to use for
114+
subsequent workspace builds.

docs/manifest.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,11 @@
319319
"title": "Devcontainers",
320320
"description": "Learn about using devcontainers in templates",
321321
"path": "./admin/templates/managing-templates/devcontainers.md"
322+
},
323+
{
324+
"title": "Dependencies",
325+
"description": "Learn how to manage template dependencies",
326+
"path": "./admin/templates/managing-templates/dependencies.md"
322327
}
323328
]
324329
},

0 commit comments

Comments
 (0)