Blog

Nomatron Terraform modules: onboarding as small, reviewable pull requests

Dec 3, 2026 By Nomatron

Five Terraform modules for Nomatron: organizations, app stacks, OIDC, Nomad clusters and service accounts, and how to compose them per workspace.

Title: Terraform Modules, beside a stack of five blocks, from the bottom: Organization, OIDC, Service account, Nomad cluster and App stack.

If you’ve followed this series, you’ve already seen a Terraform block at the end of most posts: an organization here, an OIDC mapping there, an approval policy for good measure. Each one works on its own. But when a new team turns up and needs an organization, members, an app, three environments, some variables and a cluster to deploy to, copying those blocks into a fresh file every time gets old fast. And it’s easy to get a detail wrong, like an approval rule that points at an environment by slug when the provider wants an ID.

So we’ve built five Terraform modules that wrap the Nomatron provider:

  1. organization — an organization, its members and its groups;
  2. app-stack — an application with its jobs, environments, variables and approval policy;
  3. oidc — an OIDC provider, IdP group-to-role mappings and optional custom roles;
  4. nomad-cluster — a Nomad cluster registration, with an optional network agent and agent token; and
  5. service-account — a service account for automation, its role assignments and an optional token.

A note on names: in the code, the resource and inputs say “network agent”, and that resource registers and manages the nomatron-connectivity-agent, the agent you run on your hosts, which reads its config from /etc/nomatron/agent.hcl.

In this post, you’ll see why they’re split the way they are, what each one creates, a realistic example of each, and the sharp edges worth knowing before your first apply. At the end, we’ll put four of them together.

Why modules, and why five of them?

The Nomatron Terraform provider already covers the building blocks. What modules add is a shape. A module call says “this is what onboarding a team looks like here,” with the details settled once: which inputs are required, which values are secrets, and what has to exist before what.

That shape matters most in code review. Onboarding a team should be a short pull request that a reviewer can read in a minute: one module call, a handful of inputs, and a plan that shows exactly what changes. Compare that with forty hand-written resources, where the interesting change is buried somewhere in the middle.

Why not one big “bootstrap Nomatron” module? Because the things it would create have different owners and change at different speeds.

  • The platform team owns sign-in, shared clusters and automation identities. Those change rarely, and a mistake affects everyone.
  • Each team owns its organization: who’s a member and which groups exist. That changes every time someone joins or leaves.
  • Each application owns its jobs, environments, variables and approval rules. That changes whenever the app does.

Put all of that in one module and one state, and every new hire’s pull request plans against your OIDC provider and your clusters. A typo in a team’s member list sits in the same apply as your production sign-in configuration. Splitting by workspace keeps each plan small, and keeps changes to shared pieces in the hands of the people who own them.

So each module maps to one of those jobs, and modules pass plain values to each other: an organization name, an organization ID, a cluster ID. You can call them all from one root module while you’re getting started, or spread them across workspaces as your setup grows. They don’t care which.

A few things are true of all five:

  • They use the 0.6 series of the nomatronio/nomatron provider, and none of them configures the provider for you. The app-stack module requires provider v0.6.10 or later, which fixes planning for variables with per-environment values. You set address and token once in your root module.
  • Secrets only ever go in through write-only attributes or variables, never as command-line flags or literal values in your code.
  • Any module that takes a write-only secret or mints an ephemeral token needs Terraform 1.11 or later. That’s every module except organization, which works with 1.5 or later.

Here’s the provider setup every example in this post assumes:

hcl
terraform {
  required_version = ">= 1.11.0"

  required_providers {
    nomatron = {
      source  = "nomatronio/nomatron"
      version = "~> 0.6"
    }
  }
}

variable "nomatron_address" {
  type = string
}

variable "nomatron_token" {
  type      = string
  sensitive = true
}

provider "nomatron" {
  address = var.nomatron_address
  token   = var.nomatron_token
}

Set the token from your secret store with export TF_VAR_nomatron_token="$NOMATRON_TOKEN", so the value itself isn’t written to a file or your shell history.

The organization module

What it creates: one organization, a set of organization members, and a map of organization-scoped groups, each with an owner and members.

This is the module you’ll call most often: once per team or tenant. Here’s a team called payments, with an owner, two members and two groups:

hcl
module "payments_org" {
  source  = "nomatronio/organization/nomatron"
  version = "0.1.1"

  name           = "payments"
  description    = "Payments platform team"
  owner_username = "platform.admin"

  members = ["alex.kim", "sam.ortiz"]

  groups = {
    "release-managers" = {
      description = "Approves production releases"
      members     = ["alex.kim"]
    }
    "developers" = {
      description = "Payments developers"
      members     = ["alex.kim", "sam.ortiz"]
    }
  }
}

Key inputs:

  • name — the organization name. Changing it replaces the organization, so pick it carefully.
  • owner_username — the owner, set when the organization is created. It also replaces the organization if you change it, so transfer ownership in Nomatron instead of editing this.
  • members — usernames of users who already exist in Nomatron.
  • groups — a map keyed by group name. Each group’s owner defaults to the organization owner, and each group member must also be an organization member, or the owner.

Key outputs: id and name for the organization, member_user_ids (a map of username to user ID), and groups (a map of group name to ID).

Gotchas, in plain words:

  • This module doesn’t create users. Members have to exist already, either as local users or as people who’ve signed in through your identity provider at least once. If you rely on OIDC auto-provisioning, someone who has never signed in isn’t a user yet.
  • Don’t list the owner as a member. The owner is set at creation. The module rejects the duplicate rather than guessing what the server will do with it.
  • Groups don’t grant roles. A Nomatron group names a team, for example as approvers in an approval policy. It doesn’t hold roles. Team-wide roles come from the OIDC module, below.
  • Membership changes are replacements. Adding someone creates a membership, and removing them deletes it. There’s no in-place edit, and that’s fine.
  • Importing an existing organization uses its name, not its UUID.

The app-stack module

What it creates: one application, a map of jobs, a map of environments, an optional approval policy, and a map of variables, including per-environment values.

This is the delivery onboarding story: one module call per application. It doesn’t create the organization or the cluster. You pass in org_name and cluster_id, from the other modules or from things that already exist.

hcl
variable "payments_api_db_password" {
  type      = string
  sensitive = true
}

module "payments_api" {
  source  = "nomatronio/app-stack/nomatron"
  version = "0.1.1"

  org_name     = "payments"
  name         = "Payments API"
  slug         = "payments-api"
  cluster_id   = "00000000-0000-0000-0000-000000000000" # replace with your cluster ID
  repo_url     = "https://github.com/example/payments-api"
  git_provider = "github"
  trigger_mode = "branch_commit"
  ref          = "main"

  jobs = {
    "api" = {
      name         = "API"
      jobspec_type = "hcl"
      jobspec_path = "deploy/api.nomad.hcl"
      priority     = 10
    }
  }

  environments = {
    "staging" = {
      name      = "Staging"
      namespace = "payments-staging"
      priority  = 10
    }
    "prod" = {
      name      = "Production"
      namespace = "payments-prod"
      priority  = 20
    }
  }

  variables = {
    "LOG_LEVEL" = {
      scope = "app"
      value = "info"
      environment_values = {
        "prod" = { value = "warn" }
      }
    }
    "DB_PASSWORD" = {
      scope     = "job"
      job_key   = "api"
      sensitive = true
      environment_values = {
        "prod" = { value_wo = var.payments_api_db_password }
      }
    }
  }

  approval_policy = {
    default_rule = {
      required_approvals = 1
      groups             = ["release-managers"]
    }
    environment_rules = {
      "prod" = {
        required_approvals = 2
        groups             = ["release-managers"]
      }
    }
  }
}

Key inputs:

  • org_name, name, cluster_id, repo_url and environments are required. Set slug too, so jobs, environments and variables have a stable value to refer to.
  • trigger_mode is branch_commit (deploy from pushes to ref) or tag (deploy from matching Git tags, with tag_pattern_type and tag_pattern).
  • jobs is a map keyed by job slug. Each job needs a jobspec_type and either a file (jobspec_path) or a directory (source_mode = "directory" with source_directory).
  • environments is a map keyed by environment slug. Each needs a name, a namespace and a priority, and uses the app’s cluster_id unless you override it.
  • variables is a map of app-, job- or organization-scoped variables, with optional per-environment values keyed by environment slug.
  • approval_policy is optional. Leave it out and the module doesn’t touch the policy the app already has.

Key outputs: application_id and application_slug, plus jobs and environments maps (IDs, slugs and priorities), variable_ids (IDs only, never values), and the approval policy’s ID and version.

Gotchas, in plain words:

  • An app needs at least one environment. Without one, there’s nothing to plan or apply into. The module refuses to plan with an empty environments map, so you find out at terraform plan, not later.
  • Secrets go in value_wo, not value. Mark a variable sensitive = true and give it value_wo, and the value is sent to Nomatron but never written to Terraform state. Write-only attributes need Terraform 1.11 or later. The module rejects a sensitive variable with a plain value, and a non-sensitive one with value_wo.
  • Approval rules use environment slugs here, not IDs. The provider wants an environment ID on each rule. The module looks it up from the environments it created, so you key rules by slug, and a typo fails at plan time.
  • Approver groups name who may approve; they don’t grant permission to. Approvers still need a role that allows approval, from the OIDC module or a direct assignment. And service accounts can’t approve deployments at all.
  • Don’t set is_primary on a job. It’s worked out from priority: a job with priority = 10 is primary. If you want a job in the first deployment group, give it priority 10.
  • Adopting an existing policy needs an import. Every app gets a default policy when it’s created. If you hand an existing app’s policy to this module, import it first, by org_name/app_slug.
  • A few values are deliberately loose. The provider doesn’t list every allowed jobspec_type or git_provider value, so the module passes them through. Our examples stick to hcl and github.

The OIDC module

What it creates: one OIDC provider, optional custom roles, and OIDC group mappings from an IdP group claim to a Nomatron role in a domain.

This is the module that gives teams their roles. If you read the IAM post, you’ll remember the rule: in Nomatron, OIDC group mappings are the only way to give a group of people a role. You can’t assign a role to a Nomatron group directly, so this module is where team-wide access lives.

hcl
variable "oidc_client_secret" {
  type      = string
  sensitive = true
}

module "sso" {
  source  = "nomatronio/oidc/nomatron"
  version = "0.1.1"

  slug             = "example-idp"
  display_name     = "Example IdP"
  issuer_url       = "https://idp.example.com"
  client_id        = "nomatron"
  client_secret_wo = var.oidc_client_secret
  groups_claim     = "groups"
  sync_groups      = true

  roles = {
    "payments-planner" = {
      description = "Read apps and plan payments app job environments"
      permissions = [
        "organization.apps.read",
        "organization.app_jobs.environments.plan",
      ]
    }
  }

  mappings = {
    "payments-admins" = {
      external_group  = "payments-admins"
      role            = "organization-admin"
      organization_id = module.payments_org.id
    }
    "payments-planners" = {
      external_group  = "payments-developers"
      role            = "payments-planner"
      organization_id = module.payments_org.id
    }
    "security-auditors" = {
      external_group = "nomatron-security-auditors"
      role           = "security-auditor"
      domain         = "global"
    }
  }
}

Key inputs:

  • The provider settings: slug, display_name, issuer_url, client_id, and client_secret_wo for the secret. The claim fields (username_claim, email_claim, name_claim, groups_claim) and the toggles (sync_groups, auto_provision, enabled, is_default) are optional.
  • roles creates custom roles, keyed by name, each with an explicit permission list.
  • mappings maps an external_group (the claim value from your IdP) to a role, in exactly one of domain (global or org:<uuid>) or organization_id. If you give it organization_id, the module builds the org:<uuid> domain for you.

Key outputs: provider_id, provider_slug, a roles map and a mappings map. The client secret is never an output.

Gotchas, in plain words:

  • Mappings take effect at the next sign-in, and only when Sync Groups is on. A mapping isn’t a live change. When someone next signs in through this provider, Nomatron reads their group claims and adds or removes mapped roles. Until then, their current session, which lasts a fixed 24 hours, keeps the roles it started with. So if you’re removing access urgently, don’t rely on a mapping change alone. The examples set sync_groups = true, and you should too.
  • Mappings don’t add organization membership. A mapping grants a role, not a seat in the organization. People still need to be organization members, and that’s the organization module’s job. Forget this and you get users who hold a role in an organization they can’t see.
  • The client secret is write-only. client_secret_wo is sent to Nomatron and never stored in state, which means Terraform 1.11 or later. Leave it out on applies where you aren’t setting or rotating the secret.
  • Any change to a mapping replaces it. Mappings can’t be edited in place, so changing a group, role, domain, description or the enabled flag shows up as a replacement in the plan.
  • Domains have exactly two forms. global, or org: followed by the organization’s UUID. The module rejects anything else, including a bare UUID. That’s one reason to prefer organization_id.
  • Custom roles are always global. They can hold organization.* permissions, and those apply only in the organization where the role is mapped or assigned. Don’t reuse a built-in role name for a custom role; the server rejects duplicates.

The nomad-cluster module

What it creates: one Nomad cluster registration, either global or organization-scoped, in direct or agent connectivity mode. Optionally, it also creates a network agent and mints a token for it.

That’s the whole scope. This module registers a cluster with Nomatron and, if you ask, creates the network agent record and its token. It doesn’t install anything on your hosts, including the connectivity agent, or configure networking inside the cluster.

Here’s an organization-scoped cluster that Nomatron reaches directly:

hcl
variable "nomad_acl_token" {
  type      = string
  sensitive = true
}

module "payments_cluster" {
  source  = "nomatronio/nomad-cluster/nomatron"
  version = "0.1.1"

  name              = "payments-prod"
  description       = "Payments production Nomad cluster"
  scope             = "organization"
  org_name          = module.payments_org.name
  connectivity_mode = "direct"
  address           = "https://nomad.example.internal:4646"
  acl_token_wo      = var.nomad_acl_token
}

And here’s a global cluster in agent mode, with the agent token written straight to a secret store in the same apply:

hcl
module "shared_cluster" {
  source  = "nomatronio/nomad-cluster/nomatron"
  version = "0.1.1"

  name                 = "shared-batch"
  scope                = "global"
  connectivity_mode    = "agent"
  create_network_agent = true
  network_agent = {
    name = "shared-batch-agent"
  }
  mint_agent_token = true
  agent_token_name = "shared-batch-agent"
}

# Illustrative only: use your own secret store's resource with a
# write-only value attribute. Never print or output the token.
# resource "example_secret" "agent_token" {
#   name     = "nomatron-shared-batch-agent-token"
#   value_wo = module.shared_cluster.agent_token
# }

Key inputs:

  • name, plus scope (global or organization). An organization-scoped cluster also needs org_name.
  • connectivity_mode (direct or agent). Set it explicitly; don’t rely on a server default.
  • For direct mode: address, and the write-only acl_token_wo, ca_cert_wo, tls_cert_wo and tls_key_wo as needed.
  • For agent mode: either agent_id for an existing network agent, or create_network_agent = true with a network_agent name. Not both.
  • mint_agent_token, with an optional agent_token_name, agent_token_expires_at (RFC 3339) and revoke_existing_agent_tokens.

Key outputs: the cluster id (this is what you pass to the app-stack module as cluster_id), name, scope, connectivity_mode and agent_id. When minting, there’s also an ephemeral agent_token, with its ID and prefix.

Gotchas, in plain words:

  • Agent tokens are ephemeral, so store them in the same apply. The token is minted during the apply and isn’t kept in Terraform state. If you don’t write it somewhere in that same apply, it’s gone, and you mint a new one. Pass it to a write-only attribute on your secret store’s resource, as in the example. Don’t write it to a file or a regular output.
  • Write-only cluster secrets need Terraform 1.11 or later. The ACL token, CA certificate, client certificate and key are sent to Nomatron and never stored in state.
  • Rotation can revoke. Minting a new token with revoke_existing_agent_tokens = true revokes the existing tokens for that network agent. Make sure the connectivity agent that uses the old token picks up the new one from your secret store.
  • The module checks your mode. The provider doesn’t check that direct has an address or that agent has a network agent, but the module does, so a half-configured cluster fails at plan time.
  • Pick scope deliberately. As the Organizations post recommends, scope clusters to the organization by default, and keep global ones for infrastructure that’s deliberately shared. Changing name or org_name replaces the registration.
  • Imports use names. Import a global cluster by name and an organization cluster by org_name/name. A network agent is imported by its ID.

The service-account module

What it creates: one service account, optional role assignments to that service account only, and optionally an ephemeral token for it.

Automation shouldn’t borrow a person’s login. As the IAM post put it, automation uses service accounts. This module creates one, gives it roles, and mints its token without ever writing the token to state.

hcl
module "ci_deployer" {
  source  = "nomatronio/service-account/nomatron"
  version = "0.1.1"

  name        = "payments-ci"
  description = "CI pipeline for the payments organization"

  assignments = [
    {
      role            = "payments-planner"
      organization_id = module.payments_org.id
    },
  ]

  mint_token       = true
  token_name       = "payments-ci"
  token_expires_at = "2027-03-31T23:59:59Z"
}

# Illustrative only: write the token to your CI system's secret store
# through a write-only attribute, in the same apply.
# resource "example_secret" "ci_token" {
#   name     = "NOMATRON_TOKEN"
#   value_wo = module.ci_deployer.token
# }

Key inputs:

  • name, plus optional description and is_active.
  • assignments is a list of roles. Each one uses exactly one of domain (global or org:<uuid>) or organization_id. The module always sets the subject to this service account, so you can’t use it to assign roles to anyone else.
  • mint_token, with an optional token_name and token_expires_at (RFC 3339).

Key outputs: id, name, is_active, assignment_ids, and, when minting, an ephemeral token with its ID and prefix.

Gotchas, in plain words:

  • Role assignment drift isn’t detected yet. Terraform can create and delete these role assignments, but it can’t yet read them back from Nomatron. If someone changes an assignment in the UI or the CLI, terraform plan won’t notice. Until that changes, make every assignment change through Terraform and nowhere else.
  • Tokens are ephemeral, so store them in the same apply. It’s the same pattern as the cluster module: the token goes straight into your secret store’s write-only attribute and is never kept in state. Set an expiry, and rotate by minting a new token.
  • Service accounts can’t approve deployments. Approvals come from people, not service accounts. A service account can plan and apply if its role allows, but it can’t sign off on its own work.
  • Assignments are replaced, not edited. Changing a role or domain deletes the old assignment and creates a new one. The module rejects duplicate role and domain pairs.

Putting it together

Here’s how four of the modules fit together for one new team: an organization, sign-in with team roles, a cluster and an app. We’ve written it as one root module so you can read it top to bottom. In practice, the OIDC provider usually lives in a platform workspace and each app in its own, passing the same values across.

hcl
module "org" {
  source  = "nomatronio/organization/nomatron"
  version = "0.1.1"

  name           = "payments"
  owner_username = "platform.admin"
  members        = ["alex.kim", "sam.ortiz"]

  groups = {
    "release-managers" = { members = ["alex.kim"] }
  }
}

module "sso" {
  source  = "nomatronio/oidc/nomatron"
  version = "0.1.1"

  slug             = "example-idp"
  display_name     = "Example IdP"
  issuer_url       = "https://idp.example.com"
  client_id        = "nomatron"
  client_secret_wo = var.oidc_client_secret
  groups_claim     = "groups"
  sync_groups      = true

  mappings = {
    "payments-release" = {
      external_group  = "payments-release-managers"
      role            = "release-manager"
      organization_id = module.org.id
    }
    "payments-developers" = {
      external_group  = "payments-developers"
      role            = "developer"
      organization_id = module.org.id
    }
  }
}

module "cluster" {
  source  = "nomatronio/nomad-cluster/nomatron"
  version = "0.1.1"

  name              = "payments-prod"
  scope             = "organization"
  org_name          = module.org.name
  connectivity_mode = "direct"
  address           = "https://nomad.example.internal:4646"
  acl_token_wo      = var.nomad_acl_token
}

module "app" {
  source  = "nomatronio/app-stack/nomatron"
  version = "0.1.1"

  org_name   = module.org.name
  name       = "Payments API"
  slug       = "payments-api"
  cluster_id = module.cluster.id
  repo_url   = "https://github.com/example/payments-api"

  jobs = {
    "api" = {
      name         = "API"
      jobspec_type = "hcl"
      jobspec_path = "deploy/api.nomad.hcl"
      priority     = 10
    }
  }

  environments = {
    "staging" = { name = "Staging", namespace = "payments-staging", priority = 10 }
    "prod"    = { name = "Production", namespace = "payments-prod", priority = 20 }
  }

  approval_policy = {
    default_rule = {
      required_approvals = 1
      groups             = [module.org.groups["release-managers"].name]
    }
    environment_rules = {
      "prod" = {
        required_approvals = 2
        groups             = [module.org.groups["release-managers"].name]
      }
    }
  }
}

A few things to notice:

  • Values flow one way. The organization’s id feeds the OIDC mappings, its name feeds the cluster and the app, its groups output feeds the approval policy, and the cluster’s id feeds the app. Terraform works out the order from those references, so the release-managers group exists before the policy names it.
  • Access comes from two places, on purpose. The organization module makes people members, and the OIDC module gives their IdP groups roles in that organization. Neither one does the other’s job.
  • The release-managers group does one job here. It names the approvers in the policy. The permission to approve comes from the release-manager role, via the mapping.
  • Secrets stay out of the code. The OIDC client secret and the Nomad ACL token come in as sensitive variables and go out as write-only attributes. Neither ends up in state.

Onboarding the next team is a copy of the org block with a different name, a couple of mappings, and one app block per application. That’s the reviewable pull request we were after.

Next steps

The modules are on the Terraform Registry at registry.terraform.io/namespaces/nomatronio, each with a basic and a complete example. A good order to try them in:

  1. Start with the organization module for one team, and run a plan against a test server.
  2. Add the OIDC module, map one IdP group, and sign in again to see the role arrive.
  3. Register a cluster and onboard one application with the app-stack module.
  4. Move your CI pipeline to a service account from the service-account module.

For the concepts behind each module, see the earlier posts in this series on organizations, IAM and approval policies. For every provider resource, see Manage Nomatron with Terraform in the Nomatron docs.

Ready to try it on your own Nomad clusters? Get early access

HashiCorp, Nomad and Terraform are trademarks of HashiCorp. Nomatron is not affiliated with or endorsed by HashiCorp. Other names may be trademarks of their respective owners.