Skip to content

Read terragucci's trust with Terraform

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/manage-the-control-repo-with-terraform/.
Read the terragucci.yml of the repo I name and write a Terraform configuration that reads its environments' trust with the terragucci provider's data sources, and run `tofu plan` with the provider built as the page shows. Show me the plan.
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`.

A Terraform or OpenTofu configuration that reads, from a repo’s terragucci.yml, what its jobs present to a cloud, and checks the live forge and cloud setup against it. The provider writes nothing: a control repo keeps its projects and defaults as chant’s entries on its chant/lifecycle, which terragucci control records writes.

You need Why
Go 1.25 or later you build the provider from the terragucci repository
Terraform or OpenTofu to plan and apply
The terragucci command, from npm install -g @intentius/terragucci the data sources run it
Terminal window
git clone https://github.com/intentius/terragucci
cd terragucci/terraform-provider-terragucci
go build -o "$HOME/.terragucci/provider/terraform-provider-terragucci" .

Put this in a CLI configuration file, such as ~/.terragucci/dev.tfrc:

provider_installation {
dev_overrides {
"intentius/terragucci" = "/home/you/.terragucci/provider"
}
direct {}
}

Use the full path of the directory you built into, and export TF_CLI_CONFIG_FILE=$HOME/.terragucci/dev.tfrc. Terraform and OpenTofu then load the provider from that directory without init, and print a warning saying so.

terraform {
required_providers {
terragucci = {
source = "intentius/terragucci"
}
}
}

The provider block takes one attribute, terragucci_path, the command the data sources run (default: terragucci on PATH).

Two data sources read a terragucci.yml and give back what each environment’s jobs present to a cloud. They run terragucci config identities --json and pass its values through, so they say what terragucci itself prints. They need the terragucci command on the machine that plans: install it with npm install -g @intentius/terragucci, or set terragucci_path = "npx -y @intentius/terragucci" in the provider block.

Data source Gives
terragucci_environments environments: each environment by name, and default for the roots in none, with its roots and its plan, decide, confirm, apply and drift stages. Each stage has runner_labels, runner_group, forge_environment, issuer, subject, claims, and aws, gcp and azure with each identity, its audience and its trust.
terragucci_trust_policy for one environment, stage and cloud: aws_assume_role_policy (JSON), gcp_attribute_condition (CEL), or azure_issuer, azure_subject and azure_audiences. The AWS policy and the GCP condition accept every job that takes the stage’s role or service account, which trusts lists: a plan role’s accepts the plan, deciding, confirm and drift jobs

Both take path, a local terragucci.yml, and read the roots beside it. project names one of its projects when it lists projects. repository and default_branch say which repository the jobs run in when the project key or the origin remote does not; default_branch defaults to main.

Your own resources make the forge’s environments and runners and the clouds’ roles; the data sources only give them the names and conditions.

data "terragucci_environments" "infra" {
path = "${path.module}/../infra/terragucci.yml"
}
data "terragucci_trust_policy" "prod_apply" {
path = "${path.module}/../infra/terragucci.yml"
environment = "prod"
stage = "apply"
cloud = "aws"
}
locals {
prod = data.terragucci_environments.infra.environments["prod"]
}
# The prod apply job's subject names this environment, not a branch: its deployment branch rule keeps other branches out.
resource "github_repository_environment" "prod" {
repository = "infra"
environment = local.prod.apply.forge_environment
deployment_branch_policy {
protected_branches = true
custom_branch_policies = false
}
}
resource "github_actions_runner_group" "prod" {
name = local.prod.apply.runner_group
visibility = "selected"
}
resource "aws_iam_role" "prod_apply" {
name = "prod-apply"
assume_role_policy = data.terragucci_trust_policy.prod_apply.aws_assume_role_policy
}

The account also needs an aws_iam_openid_connect_provider for the issuer, with each audience the roles’ policies name in its client_id_list.

data "terragucci_environments" "infra" {
path = "${path.module}/../infra/terragucci.yml"
}
data "terragucci_trust_policy" "prod_apply" {
path = "${path.module}/../infra/terragucci.yml"
environment = "prod"
stage = "apply"
cloud = "gcp"
}
locals {
prod = data.terragucci_environments.infra.environments["prod"]
}
# The condition requires environment_protected and ref_protected: protect the environment, and keep the default branch protected.
resource "gitlab_project_protected_environment" "prod" {
project = "acme/platform/infra"
environment = local.prod.apply.forge_environment
deploy_access_levels {
access_level = "maintainer"
}
}
# A variable only the prod environment's jobs see.
resource "gitlab_project_variable" "prod_only" {
project = "acme/platform/infra"
key = "PROD_ONLY"
value = var.prod_only
environment_scope = local.prod.apply.forge_environment
protected = true
}
resource "google_iam_workload_identity_pool_provider" "prod_apply" {
workload_identity_pool_id = google_iam_workload_identity_pool.terragucci.workload_identity_pool_id
workload_identity_pool_provider_id = "gitlab-prod-apply"
attribute_mapping = { "google.subject" = "assertion.sub" }
attribute_condition = data.terragucci_trust_policy.prod_apply.gcp_attribute_condition
oidc {
issuer_uri = data.terragucci_trust_policy.prod_apply.issuer
}
}

On GitLab, AWS reads only a token’s sub and aud, so aws_assume_role_policy checks those two.

data "terragucci_trust_policy" "apply" {
path = "${path.module}/../infra/terragucci.yml"
repository = "acme/infra"
environment = "default"
stage = "apply"
cloud = "aws"
forgejo_owner_id = "3"
forgejo_repo_id = "17"
}
resource "aws_iam_role" "apply" {
name = "apply"
assume_role_policy = data.terragucci_trust_policy.apply.aws_assume_role_policy
}

Forgejo puts the owner’s and the repository’s numeric ids in the subject. Without forgejo_owner_id and forgejo_repo_id the subject holds <owner-id> and <repo-id>, and a policy with them accepts no token. Forgejo has no deployment environments, so every apply job on the default branch presents the same subject: keep environments apart with a repository each.

data "terragucci_trust_policy" "prod_apply" {
path = "${path.module}/../infra/terragucci.yml"
environment = "prod"
stage = "apply"
cloud = "azure"
}
resource "azuread_application_federated_identity_credential" "prod_apply" {
application_id = azuread_application.prod_apply.id
display_name = "terragucci-prod-apply"
issuer = data.terragucci_trust_policy.prod_apply.azure_issuer
subject = data.terragucci_trust_policy.prod_apply.azure_subject
audiences = data.terragucci_trust_policy.prod_apply.azure_audiences
}

A GitLab plan job’s subject is a pattern, since a merge request pipeline’s token names its source branch. A federated credential takes no pattern, so for that stage azure_subject is null and azure_claims_matching_expression holds the expression for a flexible federated credential.

The terragucci_forge_check data source runs terragucci forge check --json and returns its rows: each setting the contract expects of the forge and the clouds, what it found, and status (match, mismatch, unknown or not-checked). It takes the same path, project, repository and default_branch as the other data sources, and require_clouds. It reads with the forge and cloud credentials in the environment Terraform runs in, and needs only read-only scopes. A check block reports a mismatch as a warning on every plan and apply, without stopping it:

check "forge_setup" {
data "terragucci_forge_check" "infra" {
path = "${path.module}/../infra/terragucci.yml"
}
assert {
condition = data.terragucci_forge_check.infra.counts["mismatch"] == 0
error_message = join("\n", [
for r in data.terragucci_forge_check.infra.rows :
"${r.where} ${r.check} ${r.subject}: expected ${r.expected}, found ${r.found}"
if r.status == "mismatch"
])
}
}

ok is false on a mismatch, and with require_clouds = true also when a cloud the config configures was not checked for want of credentials. json holds the whole result.

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.