Skip to content

Publish your modules

llms.txtlists every page for an agent
Optional: hand this page to your coding agentThe steps work by hand too.
Show the whole prompt
Read https://intentius.io/terragucci/guides/publish-modules/.
Add the `modules:` block, run `npx terragucci publish --dry-run`, show me what
would be published, run `npx terragucci init`, and open a pull request. List the
registry credentials I must add; do not add them. If I ask for attested
releases, add `attest: true` and tell me to run `cosign generate-key-pair`
myself; never create, read or commit a private key.
Never apply, approve (a pull request review or `terragucci approve`), override a policy denial (`terragucci override`), use `--mode apply`, or merge; never touch `.chant/allowed_signers` or `chant/lifecycle`.

Every module that changed on the default branch released under a new number, each with a digest your roots can pin.

Each level is opt-in:

Level Config What it adds
Publish modules.publish a git tag or OCI tag per release, pinned rollouts by pull request, and version bump jobs
Registry modules.registry each release as a module registry’s files in a bucket or a Pages site, so roots call them by registry source and version
Test modules.test: true tofu test or terraform test on each module before a release of it publishes
Attest modules.attest: true as well a signature, SLSA provenance and an SBOM for each release, and a record of it in the release ledger
You need Why
Modules in one place, such as modules/network and modules/service each directory there is published on its own
Conventional commit messages they decide the version bump
An OCI registry, or permission to push git tags to origin where the versions go; Terraform has no OCI sources, so its roots pin a git tag
  1. Name the modules and where they go in terragucci.yml.

    modules:
    path: modules/*
    publish: oci://registry.example.com/acme/modules

    publish takes an oci:// registry address, git-tags, or a list of both.

    Target Roots pin Credentials
    oci:// registry OpenTofu roots, by ?tag= or ?digest=, and Terragrunt 1.2 units in terraform { source } (Use Terragrunt) the registry variables in step 4
    git-tags Terraform roots, by git tag none; the tags are pushed to origin
  2. Preview.

    Terminal window
    npx terragucci publish --dry-run

    The output lists each changed module and its next version under the version bump rules. For commits with no conventional type, see respond.version-bump.

  3. Add the publish job.

    Terminal window
    npx terragucci init

    That adds a publish job, which runs with full history after apply on each push to the default branch.

  4. Give it registry credentials; no other job gets them.

    Repository secrets.

    Variable Holds
    TERRAGUCCI_REGISTRY_USER the registry user
    TERRAGUCCI_REGISTRY_PASSWORD its password or token
    TERRAGUCCI_REGISTRY_INSECURE 1 for a registry without TLS
  5. Merge a change to a module.

    The next push to the default branch publishes, printing each version’s OCI manifest digest for a root to pin: oci://registry.example.com/acme/modules/network?digest=sha256:..., or ?tag=0.2.0 for the tag.

    Each OCI version is an OpenTofu module package: an image manifest with the artifact type application/vnd.opentofu.modulepkg, an empty config, and one archive/zip layer. The zip holds the module directory at its root, so //<dir> selects a directory inside it. The same files give the same zip and the same manifest digest.

    terragucci 0.4.40 and earlier pushed the layer as a gzipped tar, which tofu init cannot install. The publish job finds those versions and pushes each one again under its tag as a zip of the same files, with the same commit and content. It prints one line for each: network 0.1.0: republished as archive/zip to oci://.... Under modules.attest, the new manifest is signed and recorded like any release. A root that pins such a version by digest still names the tar, so pin the tag or the new digest.

    With git-tags, each version is a tag on the repo, <module>/v<version>. Here modules/service moved to 0.2.0 after a change while modules/queue stayed at 0.1.0.

    A published version’s files never change, so a rerun publishes nothing. Git-tag publishing fetches origin’s tags first; different content under an existing tag stops the run and names the tag.

With modules.test, the publish job runs the binary’s test on each module before releasing it. A module with no test files or with failing tests is refused; the other modules still publish, and the job fails at the end.

binary: tofu
modules:
path: modules/*
publish: git-tags
test: true

The test files are the binary’s own: *.tftest.hcl (and *.tofutest.hcl for OpenTofu) in the module or in its tests directory.

modules/network/tests/main.tftest.hcl
variables {
name = "dev"
}
run "names" {
command = plan
assert {
condition = terraform_data.net.input == "dev"
error_message = "the name is not passed through"
}
}

The job runs init -backend=false and then test in the module’s directory. The forge token, signing key and registry credentials are withheld from it. A refused release says why, with the end of the binary’s output when a test failed:

network 1.1.0: refused, not published to git-tags (modules/network has no tests (no *.tftest.hcl in it or its tests directory), and modules.test publishes only a release whose tests pass)

publish --dry-run runs no tests; it says which releases it would test.

With modules.registry, a bucket or a Pages site also serves each release as static files in the module registry protocol. Roots call the modules by registry address, pinned to one version:

module "network" {
source = "modules.example.com/acme/network/generic"
version = "1.0.0"
name = "dev"
}
  1. Name the bucket, the address that serves it, and the namespace.

    modules:
    path: modules/*
    publish: git-tags # optional; the registry can stand alone
    test: true
    registry:
    bucket: s3://acme-modules
    url: https://modules.example.com
    namespace: acme
    Setting Default Holds
    url required the https:// address that serves the files, with no path. Terraform and OpenTofu look for a registry at its host’s root, over HTTPS only. Its host starts each module’s source
    namespace required the namespace modules go under
    bucket or dir one is required an s3://, gs:// or az:// bucket, or a directory in the repo that a Pages site deploys
    prefix none where the files go in the bucket; url serves that prefix as its root
    endpoint none an S3-compatible store’s address
    namespaces none a tag prefix (a path in the repo, such as platform/) to the namespace its modules go under; the longest prefix wins
    system generic the third part of each address
    download tarball what each version’s download points at: a .tar.gz beside it, its git tag (git-tags, which publish must list), or its OCI artifact (oci, which publish must list; OpenTofu only)
    edge picked from bucket, endpoint or dir what sends the X-Terraform-Get header Terraform needs: cloudflare, cloudfront, pages or none; see Put an edge function in front of the bucket
    auth off token has the edge function refuse a read without a bearer token, and sign tarball URLs
    providers none mirror also serves the providers the lock files pin as a network mirror under providers/; see Serve the providers from the registry too

    A module’s address is <host>/<namespace>/<name>/<system>, where the name is its directory’s name: modules/network is modules.example.com/acme/network/generic.

  2. Give the publish job write access to the bucket.

    Terminal window
    npx terragucci init

    The publish job on GitHub and Forgejo maps the bucket’s key secrets as a job that keeps reports in a bucket does: for S3 the pair AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, for Azure AZURE_STORAGE_KEY. GitLab passes it CI/CD variables of those names.

  3. Serve the bucket over HTTPS at url, through the edge function for its host. Only the publish job writes the bucket.

  4. After the next module change merges, the publish job writes:

    File Holds
    .well-known/terraform.json service discovery: {"modules.v1": "/v1/modules/"}
    v1/modules/<namespace>/<name>/<system>/versions every published version
    v1/modules/<namespace>/<name>/<system>/<version>/download {"location": ...}, the tarball, git tag or OCI artifact
    v1/modules/<namespace>/<name>/<system>/<version>/<name>-<version>.tar.gz the module, with download: tarball
    v1/modules/<namespace>/<name>/<system>/<version>/release.json the commit and content digest the next publish compares against

    The stored download answer is a JSON body with location. OpenTofu reads that body. Terraform reads only the X-Terraform-Get header, which a bucket or a static site cannot send on its own, so Terraform installs only through the edge function or a _headers file. A version is listed in versions only after the files it points at are written.

  5. Pin a root to the registry and run init. It installs 1.0.0, the version the root names. When a newer version publishes, a rollout (terragucci rollout modules/network) moves the pin in each root that calls it.

In a monorepo, each part’s modules can go under a namespace of their own. Tags are <path>/v<version>, so the path prefix of a module is its tag prefix:

registry:
bucket: s3://acme-modules
url: https://modules.example.com
namespace: acme
namespaces:
platform/: platform
data/: data

platform/modules/network is modules.example.com/platform/network/generic. Two modules at one address stop the publish and name both.

With dir, the publish job writes the files into that directory of its checkout, beside what is there. You deploy the directory to the Pages site and keep the earlier versions in it (from the site’s branch, for example), since versions lists only what the directory holds.

A rollout of modules/network moves the version of each call to its registry address. A call pinned by a constraint such as ~> 1.0 has no one version to move, and the rollout refuses it.

Put an edge function in front of the bucket

Section titled “Put an edge function in front of the bucket”

Terraform finds a version’s location in the X-Terraform-Get header of the download answer and fails without it:

Error: Error accessing remote module registry
Failed to retrieve a download URL for modules.example.com/acme/network/generic
1.0.0 from modules.example.com: failed to get download URL for
"modules.example.com/acme/network/generic": 200 OK

An edge function on your CDN reads the stored answer and sends the same location in the header, keeping the JSON body, so both binaries install. terragucci picks the function from where the files are, and edge overrides the pick:

Files in edge Sends the header with
an R2 bucket (endpoint on r2.cloudflarestorage.com), a gs:// or an az:// bucket cloudflare a Cloudflare Worker
any other s3:// bucket cloudfront a Lambda@Edge function on CloudFront; a CloudFront Function cannot read the stored answer
dir, deployed to Cloudflare Pages or Netlify pages a _headers file the publish job writes beside the files, one rule per version
dir on GitHub Pages or GitLab Pages, or a plain bucket website none nothing: OpenTofu installs, Terraform does not

Cloudflare Pages reads at most 100 rules from _headers. Past that, the publish job says so on each new version; serve the files through edge: cloudflare or from Netlify.

  1. Write the function and its deploy config.

    Terminal window
    npx terragucci registry edge

    It writes registry-edge/ and prints the steps for your CDN. For cloudflare, the directory holds worker.mjs and a wrangler.toml that binds the R2 bucket, or names the bucket’s address in REGISTRY_ORIGIN, and routes url’s host to the Worker. For cloudfront, it holds index.mjs and registry-edge.json, which you zip into one Lambda function in us-east-1 on Node.js 20 or later.

  2. Deploy it.

    Terminal window
    cd registry-edge
    npx wrangler deploy
  3. Run init in a root that calls a module from the registry. Both binaries install.

With auth: token, the function refuses discovery, versions, download and every other file without Authorization: Bearer <token>, answering 401. A tarball’s download answer becomes a signed URL that opens that one file for five minutes, so the archive fetch needs no token of its own. Git and OCI locations go out as stored, and their own access rules apply.

modules:
registry:
bucket: s3://acme-modules
url: https://modules.example.com
namespace: acme
auth: token

auth: token needs edge: cloudflare or edge: cloudfront; a _headers file cannot check a token.

  1. Make a token and keep it in your secret manager.

    Terminal window
    openssl rand -hex 32
  2. Give it to the function.

    Terminal window
    npx wrangler secret put REGISTRY_TOKENS

    The Worker reads its tokens from that secret, one per line or comma separated.

  3. Give each reader the token. Both binaries send it for the registry’s host from TF_TOKEN_modules_example_com, or from a credentials "modules.example.com" block in the CLI config or credentials.tfrc.json. The tf-check and rollout jobs read a registry the same way, so give them the variable too.

To rotate a token, give the function both tokens, <old>,<new>, and deploy. Move every reader to the new token, then give the function the new one alone and deploy again. The tarball URLs are signed with the first token, so a URL handed out before the swap stops working; each one lasts five minutes.

OpenTofu 1.12 and later also read "use_registry_credentials": true in a download answer’s body. OpenTofu then fetches the location itself and sends the registry host’s token with it, which fits a server that keeps tarballs behind the same token check without signed URLs. Terraform has no such field, and terragucci’s files do not set it.

A registry on a private network needs no function for the token: serve it only inside a VPN or behind an IP allowlist or Cloudflare Access, and use auth: off. Terraform still needs the header, so keep the edge function or _headers file for it.

A team that already runs GitLab, HCP Terraform, Artifactory or Nexus can publish its modules there and read them with that registry’s own credentials.

A GitLab project’s Terraform module registry can take each release too:

modules:
publish:
- git-tags
- gitlab://gitlab.example.com/platform/modules

In GitLab CI, publish: gitlab names the job’s own project and uploads with its CI_JOB_TOKEN. Elsewhere, write the project’s address and put a token with the api scope in GITLAB_TOKEN. On GitHub and Forgejo the publish job reads it from the secret modules.gitlab_token names, GITLAB_TOKEN unless set.

The upload is the archive the bucket registry serves and the attestation signs, so every target has one digest. Publish reads the release back as Terraform would before it reports it. A release already there with the same content is left alone, and one with other content is refused. The host keeps no commit for what it holds, so publish takes the commit from the release record on the ledger. Without a record, as for an upload by hand, the commit comes from the git-tags tag; with neither, publish stops and says what to fetch.

Roots call the module under the project’s top-level group:

module "network" {
source = "gitlab.example.com/platform/network/generic"
version = "1.3.0"
}

The system is modules.registry.system when the repo also serves a bucket registry, else generic. To read a private project, Terraform and OpenTofu take TF_TOKEN_gitlab_example_com with the read_api scope, or a credentials block. GitLab has no deprecated flag, so a deprecation shows only in the bucket’s catalog and in the tip.

HCP Terraform’s private registry takes releases through its API, on every tier and with no VCS connection:

modules:
publish:
- git-tags
- hcp://acme # or hcp://tfe.example.com/acme for Terraform Enterprise

The publish job reads its token from TFE_TOKEN, else from TF_TOKEN_app_terraform_io or a credentials block for the host. The token’s team needs the Manage modules permission. A 404 from HCP means the team lacks it or the organization has no private registry, and the publish error says both.

Publish creates the module when HCP lacks it and then the version with its commit, and uploads the same archive as the other targets. It waits for HCP to finish reading the archive, and a release still being read when the wait ends is reported without failing. A 429 is retried after Retry-After. Roots call app.terraform.io/acme/network/generic, with the system from modules.registry.system when it is set. HCP has no deprecated flag either.

A JFrog Artifactory local Terraform repository and a Sonatype Nexus hosted Terraform repository (Nexus 3.89 and later) take releases too:

modules:
publish:
- artifactory://acme.jfrog.io/terraform-local
- nexus://nexus.example.com/terraform-hosted/platform

Each uploads the zip of the attested archive with basic credentials: JFROG_USER and JFROG_ACCESS_TOKEN for Artifactory, NEXUS_USER and NEXUS_TOKEN for Nexus. The last part of the address is the namespace; left out, it is modules.registry‘s for each module. Artifactory sources join the repository and namespace with __, as in acme.jfrog.io/terraform-local__acme/network/generic. A Nexus source starts with the label of the readers’ CLI host block, the repository’s name in lower case, as in terraform-hosted/platform/network/generic. Sonatype’s docs disagree on whether a hosted repository needs a signing key for modules, so set one when you create it.

Point the binaries at the registry by CLI config

Section titled “Point the binaries at the registry by CLI config”

A host block in the CLI config sends a host’s module requests to another address, with no service discovery. It suits CI images and machines you set up, such as a registry reached at an internal name:

host "modules.example.com" {
services = {
"modules.v1" = "https://modules.internal.example.com/v1/modules/"
}
}

Terraform reads the block from TF_CLI_CONFIG_FILE or ~/.terraformrc, OpenTofu from TF_CLI_CONFIG_FILE or ~/.tofurc. A source with no host resolves to registry.terraform.io under Terraform and registry.opentofu.org under OpenTofu, so a block for one of them does nothing for the other binary. The address it names needs a certificate the binary already trusts.

With providers: mirror, the publish job also serves every provider the repo’s lock files pin as a provider network mirror, under providers/ beside the modules:

modules:
registry:
bucket: s3://acme-modules
url: https://modules.example.com
namespace: acme
providers: mirror

After the modules publish, the job runs the binary’s providers mirror once for each distinct lock file, with each provider at exactly its locked version, so the lock file’s hashes check every download. The mirror is for linux_amd64. Each provider locked on registry.terraform.io or registry.opentofu.org is mirrored under the other host as well, so a source with no host resolves under both binaries. Archives go up first and each index.json last, so a reader never sees a version whose files are not there yet. A publish whose lock files pin nothing new mirrors nothing again.

The job’s output ends with the block each reader adds to its CLI config:

provider_installation {
network_mirror {
url = "https://modules.example.com/providers/"
include = [
"registry.opentofu.org/hashicorp/aws",
"registry.terraform.io/hashicorp/aws",
]
}
direct {
exclude = [
"registry.opentofu.org/hashicorp/aws",
"registry.terraform.io/hashicorp/aws",
]
}
}

include and exclude name the same providers. A mirrored provider then comes only from the mirror, and any other provider comes straight from its registry. With auth: token the index and version files need the token. The edge function signs each archive URL in the version files, as it signs tarballs.

A registry with modules.registry also gets a release catalog under catalog/ beside its files. Its index.html has a search box over index.json and links to a page for each module and each version; each module also gets a consumers.json. Every publish records its release on chant/lifecycle with its version, attested or not, and the catalog is built from those records and the deprecations beside them. The registry’s own versions files are never read for it.

A version’s page shows its inputs, outputs, required providers, examples and README, read from the module at the commit the version was cut from. A module with a .terraform-docs.yml gets the markdown terraform-docs writes with that file instead, when terraform-docs is on the job’s PATH; it runs on a scratch copy and prints to stdout, so the module’s README in the repo is never rewritten. The changelog groups the conventional commits since the previous version. Under it sit the attestation with the command that checks it and the roots in this repo that pin the version.

Across a control repo, each project keeps a consumers.json at its top: terragucci modules consumers writes it from the project’s roots, and --check fails when it no longer matches them. A repo with control runs --check in its check job, so a pull request that changes a pin also updates the file. A publishing repo with control reads the control repo’s projects and each one’s consumers.json, with TERRAGUCCI_CONTROL_READ_TOKEN, and the catalog lists every project’s roots on each version. Without the token it lists this repo’s roots only.

A version keeps the page it was first written with, and a file already in the bucket with the same bytes is left alone, so a rerun that publishes nothing writes nothing. terragucci publish --dry-run writes no catalog. To turn the catalog off:

modules:
registry:
bucket: s3://acme-modules
url: https://modules.example.com
namespace: acme
catalog: off

terragucci modules lists the catalog from the command line, and terragucci modules show <module> lists one module’s versions with their status and consumers:

Terminal window
terragucci modules
terragucci modules show modules/network
terragucci modules --registry https://modules.example.com --json

It reads catalog/index.json the way Terraform reads the registry, with TF_TOKEN_<host> or a credentials block, so it works against a registry with auth: token. A browser sends no token, so the edge function refuses the catalog’s pages to it; to browse them, serve the registry on a private network as in Other ways to keep a registry private, where the pages open with no token.

Anyone who can publish marks a version on the release ledger:

Terminal window
terragucci modules deprecate modules/network 1.2.0 --reason "drops the old subnet layout" --use 1.3.0
terragucci modules yank modules/network 1.1.0 --reason "opens the admin port" --use 1.3.0
terragucci modules restore modules/network 1.1.0

Each pushes one status record beside the release record. The record keeps who set it and when, with the reason and the --use version. The catalog is then written again, so the mark reaches roots in other repos. A root that pins a deprecated version gets the terragucci-deprecated-version tip on its plan and still passes. A yanked version is refused by modules.require: attested, naming the version to move to. Both stay in the registry’s versions, so an existing pin still runs init. restore clears either mark with one more record; the earlier ones stay in the ledger’s history.

With modules.attest, the publish job also signs each new release with a key you hold and records the version in the release ledger on chant/lifecycle, so a root’s pin can be checked against all of it.

  1. Make a cosign key pair, and commit the public half.

    Terminal window
    cosign generate-key-pair
    git add cosign.pub

    Keep cosign.key out of the repo. Signing never writes to Sigstore’s public transparency log.

  2. Add two more secrets, the same way as the registry credentials in step 4.

    Variable Holds
    COSIGN_PRIVATE_KEY the contents of cosign.key
    COSIGN_PASSWORD its password
  3. Turn it on, and write the pipeline again.

    modules:
    path: modules/*
    publish: git-tags
    attest: true # or attest: { key: keys/modules.pub }
    Terminal window
    npx terragucci init

    The publish job now installs cosign, checked against its release checksums, before it publishes.

  4. After a module change merges, check what was released.

    Terminal window
    npx terragucci verify-release modules/service 0.2.0

    It reads the tag as it stands now and prints verified for each target, or refused and why.

terragucci runs each version through these phases from this config, with no file of your own:

Phase Does
Archive takes the bytes the tag names: the module archive for a git tag, the manifest for an OCI tag
Sbom writes SPDX 2.3 JSON from the module’s HCL: each provider in required_providers, at its lock file’s version when there is one, and each module it calls from another source
Sign signs the bytes with cosign
Provenance attests SLSA v1 provenance naming the commit and the run
SbomAttestation attests the SBOM
Verify checks all three against cosign.pub; a job given the wrong key stops here, before it writes anything
Record makes the release record: the module’s path, the digest of those bytes, the commit, the run and the actor
What Where
The release record a line of modules/releases.jsonl on chant/lifecycle
The bundles and the SBOM modules/attest/<digest>/ beside it
An OCI tag’s signature and attestations attached to its manifest
A git tag and its record pushed together in one atomic push; an OCI tag is pushed only after its record

A tag pushed by hand has no record that matches it, and neither does one moved after its release; verify-release refuses both. To go back to an earlier version, roll out that version again.

With modules.require: attested, tf-check and tf-plan check each module pin a root makes before they plan it. A root pinning a release that does not verify fails. So does one pinning a release its publisher yanked, whose refusal names the version to move to. The check report and plan note name its module call and version and say why.

In a Terragrunt repo the pin is each unit’s terraform { source }, and for a unit an explicit stack generates from a remote source, its unit block’s source in terragrunt.stack.hcl. An oci:// source that picks a directory with // is checked as the release of the whole package. tf-check runs terragucci check-pins over every unit, and tf-plan takes a refused unit out of its wave, so it fails without planning while the rest of the wave plans.

modules:
publish: git-tags
attest: true
require: attested

Only these sources are checked; a module from anywhere else, such as the public registry, is left alone:

Source Checked when Key Ledger
This repo’s own modules modules.attest is on: the repo’s git URL for git-tags, each oci:// target, and the host of modules.registry modules.attest.key chant/lifecycle on origin
A publisher in another repo modules.trusted lists it its key its ledger
modules:
require: attested
trusted:
- source: git::https://git.example.com/platform/modules.git
key: keys/platform-modules.pub
ledger: https://git.example.com/platform/modules.git

To trust a publisher, commit their cosign.pub at the path key names. For each pin of a checked source:

Check Refused when
The pin it names no one release: a git ref that is not a tag, an oci:// source with no tag or digest, a registry source whose version is a constraint
The record the publisher’s ledger has no record of the bytes the tag names now, from the tag’s commit: a tag pushed by hand, moved, or never published
The signature, provenance and SBOM any of them is missing or does not verify against the key
The OCI artifact its manifest has no archive/zip layer, so tofu init cannot install it
Job Reads the setting and the keys from
tf-plan the base branch, as it reads the policy, so a pull request cannot turn the check off or trust a key of its own
tf-check the base branch when its checkout has it, else the checkout

A registry source is checked where its registry says the version is: the tarball’s bytes, or the git tag or OCI artifact its download points at, each checked as that kind of source is. Under download: tarball a release is signed and recorded in the ledger before versions lists it. The jobs read the registry over HTTPS, as tofu init does, so a private CA’s certificate goes in NODE_EXTRA_CA_CERTS.

Tags and the ledger are fetched with the jobs’ own git access; an oci:// source is read with no login, or with TERRAGUCCI_REGISTRY_USER and TERRAGUCCI_REGISTRY_PASSWORD when the job has them.

terragucciHelp setting it up: Intentius

These docs count page views and clicks with PostHog. They set no cookies, store nothing in your browser, and send nothing when your browser asks not to be tracked.