-
Notifications
You must be signed in to change notification settings - Fork 937
chore(docs): add recommendations for dependency management #13400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# Template Dependencies | ||
|
||
When creating Coder templates, it is unlikely that you will just be using | ||
built-in providers. Part of Terraform's flexibility stems from its rich plugin | ||
ecosystem, and it makes sense to take advantage of this. | ||
|
||
That having been said, here are some recommendations to follow, based on the | ||
[Terraform documentation](https://developer.hashicorp.com/terraform/tutorials/configuration-language/provider-versioning). | ||
|
||
Following these recommendations will: | ||
|
||
- **Prevent unexpected changes:** Your templates will use the same versions of | ||
Terraform providers each build. This will prevent issues related to changes in | ||
providers. | ||
- **Improve build performance:** Coder caches provider versions on each build. | ||
If the same provider version can be re-used on subsequent builds, Coder will | ||
simply re-use the cached version if it is available. | ||
- **Improve build reliability:** As some providers are hundreds of megabytes in | ||
size, interruptions in connectivity to the Terraform registry during a | ||
workspace build can result in a failed build. If Coder is able to re-use a | ||
cached provider version, the likelihood of this is greatly reduced. | ||
|
||
## Lock your provider and module versions | ||
|
||
If you add a Terraform provider to `required_providers` without specifying a | ||
version requirement, Terraform will always fetch the latest version on each | ||
invocation: | ||
|
||
```terraform | ||
terraform { | ||
required_providers { | ||
coder = { | ||
source = "coder/coder" | ||
} | ||
frobnicate = { | ||
source = "acme/frobnicate" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Any new releases of the `coder` or `frobnicate` providers will be picked up upon | ||
the next time a workspace is built using this template. This may include | ||
breaking changes. | ||
|
||
To prevent this, add a | ||
[version constraint](https://developer.hashicorp.com/terraform/language/expressions/version-constraints) | ||
to each provider in the `required_providers` block: | ||
|
||
```terraform | ||
terraform { | ||
required_providers { | ||
coder = { | ||
source = "coder/coder" | ||
version = ">= 0.2, < 0.3" | ||
} | ||
frobnicate = { | ||
source = "acme/frobnicate" | ||
version = "~> 1.0.0" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
In the above example, the `coder/coder` provider will be limited to all versions | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
above or equal to `0.2.0` and below `0.3.0`, while the `acme/frobnicate` | ||
provider will be limited to all versions matching `1.0.x`. | ||
|
||
The above also applies to Terraform modules. In the below example, the module | ||
`razzledazzle` is locked to version `1.2.3`. | ||
|
||
```terraform | ||
module "razzledazzle" { | ||
source = "registry.example.com/modules/razzle/dazzle" | ||
version = "1.2.3" | ||
foo = "bar" | ||
} | ||
``` | ||
|
||
## Use a Dependency Lock File | ||
|
||
Terraform allows creating a | ||
[dependency lock file](https://developer.hashicorp.com/terraform/language/files/dependency-lock) | ||
to track which provider versions were selected previously. This allows you to | ||
ensure that the next workspace build uses the same provider versions as with the | ||
last build. | ||
|
||
To create a new Terraform lock file, run the | ||
[`terraform init` command](https://developer.hashicorp.com/terraform/cli/commands/init) | ||
inside a folder containing the Terraform source code for a given template. | ||
|
||
This will create a new file named `.terraform.lock.hcl` in the current | ||
directory. When you next run [`coder templates push`](../cli/templates_push.md), | ||
the lock file will be stored alongside with the other template source code. | ||
|
||
> Note: Terraform best practices also recommend checking in your | ||
> `.terraform.lock.hcl` into Git or other VCS. | ||
|
||
The next time a workspace is built from that template, Coder will make sure to | ||
use the same versions of those providers as specified in the lock file. | ||
|
||
If, at some point in future, you need to update the providers and versions you | ||
specified within the version constraints of the template, run | ||
|
||
```console | ||
terraform init -upgrade | ||
``` | ||
|
||
This will check each provider, check the newest satisfiable version based on the | ||
version constraints you specified, and update the `.terraform.lock.hcl` with | ||
those new versions. When you next run `coder templates push`, again, the updated | ||
lock file will be stored and used to determine the provider versions to use for | ||
subsequent workspace builds. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could explicitly state that cache utilization is only possible when a lock-file is present. I could be misremembering, but providers will always be re-downloaded otherwise, even if cached.