Repository navigation
Description
versions[*].directory is the only attribute in this provider that reads from the local filesystem. Everything else is configuration. That makes template contents the one part of a coderd_template that Terraform can't compute, transform, or receive as a value — it has to already exist as files on the machine running the apply.
Where this bites
Rendered templates. A common need is one template source parameterised per deployment — a different image tag, registry, namespace or agent script per environment. templatefile() produces a string, and there's no way to feed a string to directory. Today the workaround is local_file resources writing into a scratch directory, plus depends_on to force ordering. That doesn't actually work reliably: the directory is hashed during planning to decide whether a new version is needed, and local_file writes during apply. The first plan hashes a directory that doesn't exist yet or still holds the previous render, so the version either fails to update or updates one run late.
Template source outside the configuration. If the template lives in another repo, an S3 object, an artifact store, or is fetched with http/vault data sources, everything arrives as a string or as base64. Same dead end, same local_file workaround.
HCP Terraform / Terraform Enterprise. Remote runs only receive the uploaded configuration directory. Anything checked out, generated or downloaded outside it isn't there at apply time.
Modules. A module today can only take a path variable, which pushes path.root vs path.module resolution onto the caller and makes the module's behaviour depend on the caller's directory layout. Passing content as a variable is the idiomatic alternative and isn't expressible.
CDKTF. Configuration is synthesized, not checked out alongside template sources.
There's also a reproducibility argument: because the hash comes from walking a path, the same configuration can plan differently on two machines depending on what's in that directory. Every other attribute of this resource is fully determined by the config.
Proposal
A content-based alternative on versions[*], exactly-one-of with directory:
versions = [{
name = "v${var.COMMIT_SHA}"
active = true
files = {
"main.tf" = templatefile("${path.module}/tpl/main.tf.tftpl", {
image = var.image
namespace = var.namespace
})
"build/Dockerfile" = file("${path.module}/tpl/build/Dockerfile")
".terraform.lock.hcl" = file("${path.module}/tpl/.terraform.lock.hcl")
}
}]A map(string) of relative path to content. An archive_base64 string would cover binary contents and prebuilt tarballs; either alone solves my case, files is the more idiomatic of the two.
Reading an unmodified directory stays a one-liner:
files = { for f in fileset("${path.module}/tpl", "**") :
f => file("${path.module}/tpl/${f}") }Why this should be small
The resource already assembles a tar in memory and uploads it, then creates the version from the returned file ID. Only the source of the file set changes. directory can be reduced to "walk the path, produce the same file set", leaving one upload path and no second branch to maintain.
Diffing gets simpler. Contents are in the configuration, so the hash that today requires a directory walk during plan is available directly, and the existing rule that an explicit name must change whenever contents change is unaffected. Values are known at plan time, so nothing becomes unknown.
No new dependencies, no behaviour change to directory.
Alternatives considered
local_file+depends_on. The plan-time/apply-time ordering problem above makes this unsound, not merely ugly.- Render outside Terraform and commit the result. Moves environment parameterisation out of the configuration and into CI, and duplicates one template per environment.
coder templates pushfrom a pipeline, withversionsomitted. Now supported and reasonable, but version activation stops being declarative and the template is split across two systems.
Caveats I'd raise myself
- Contents land in state. Worth a docs note, as for any
file()-sourced attribute. provisionersdk.TemplateArchiveLimitcaps the archive; an explicit error when the assembled set exceeds it would beat whatever the API currently returns.- For a large template directory checked in beside the configuration,
directorystays the better choice and the docs should say so.
Happy to implement. I'd want to settle files vs archive_base64 (or both) and whether the exactly-one-of check belongs in schema validators or in Create/Update before opening a PR.
Activity
linear-code commentedon Sep 17, 2026
ENG-3355
feat(coderd_template): accept version contents as an archive file path
feat(coderd_template): accept version contents as files or an archive
feat(coderd_template): accept version contents as an archive file path