Skip to main content

Mapping Terraform and OpenTofu Module Patterns to Massdriver

Where each kind of shared Terraform or OpenTofu module goes in Massdriver: naming utilities, shared child modules, decorators such as diagnostic settings and VM extensions, and organization-wide configuration. It builds on bundles, resources, and dependencies. New to these? Start with the getting started guides.

Summary​

Traditional Module PatternMassdriver PatternWhat Happens to the Module
Naming / tagging utilitiesEliminated by md_metadataDelete it
Shared child modules (IAM, secrets, firewall)Child modules referenced by git sourceKeep it, reference it via git
Decorator modules (diagnostics, extensions)Child modules baked into bundles, or standalone bundles with dependenciesMove or promote it
Meta / org config (Entra, DNS, SSO, Policy)Full standalone bundlesPromote it

Pattern 1: Naming and Tagging Utilities​

Delete these.

Modules that generate resource names, apply standard tags, or pass project and environment identifiers around are replaced by md_metadata, which every deployment receives in its params.

What md_metadata provides​

  • name_prefix: a unique identifier in the form {project}-{environment}-{component}-{suffix}, such as ecomm-prod-api-abc1. Use it wherever you concatenated project, environment, and app names.
  • default_tags: tags for every cloud resource. They always include managed-by and md-instance. The other keys, such as md-project and md-environment, come from the organization's default tags setting.

Before and after​

Before: the bundle calls a naming module:

hcl
module "naming" {
source = "git::https://github.com/your-org/terraform-modules.git//naming"
project = var.project_name
environment = var.environment
app_name = var.app_name
}

resource "azurerm_storage_account" "main" {
name = module.naming.storage_account_name
tags = module.naming.default_tags
# ...
}

After: the bundle reads md_metadata:

hcl
resource "azurerm_storage_account" "main" {
# Azure storage account names: lowercase alphanumeric only, no hyphens
name = replace("${var.md_metadata.name_prefix}sa", "-", "")
tags = var.md_metadata.default_tags
# ...
}

The project, environment, and app name params leave the bundle's params, so they are not in the form and cannot be set wrong.

For every md_metadata field, see Using Bundle Deployment Metadata.

Pattern 2: Shared Child Modules​

Keep these as modules, referenced by git source.

Child modules are called from inside other modules:

  • IAM and RBAC helpers: role assignments, service principals, managed identities
  • Secret readers: fetch secrets from Key Vault or Secrets Manager
  • Firewall rule builders: open network access based on dependency data
  • Provider configuration: cloud provider authentication

They are not deployable on their own, so they do not become bundles.

Why local paths don't work​

mass bundle publish packages only the bundle directory and its subdirectories. A relative module source outside it, such as source = "../../../modules/opentofu/azure-role-assignment", does not exist in the published bundle, and the deployment fails when the provisioner resolves the module.

Reference modules with git sources​

Reference shared child modules with a git source and a pinned ref:

hcl
module "role_assignment" {
source = "git::https://github.com/your-org/your-modules.git//path/to/module?ref=v1.2.0"

...
}

The provisioner clones the module at deploy time. Pinning ref to a tag or commit SHA versions the module per bundle, so you can roll a module change out one bundle at a time.

The modules can live in their own repository or next to your bundles:

massdriver-catalog/
├── resource-types/
├── bundles/
│ ├── azure-storage-account/
│ ├── azure-cognitive-search/
│ └── ...
└── modules/
├── opentofu/
│ ├── azure-diagnostic-settings/
│ ├── azure-role-assignment/
│ └── azure-keyvault-secret-reader/
├── terraform/
│ └── ...
├── charts/
│ └── ...
├── bicep/
│ └── ...
└── cloudformation/
└── ...

Even in the same repository, reference the module through git, not a relative path:

hcl
module "diagnostics" {
source = "git::https://github.com/your-org/massdriver-catalog.git//modules/opentofu/azure-diagnostic-settings?ref=v2.0.1"
# ...
}

Private module repositories​

For a private git repository, the Terraform and OpenTofu provisioners take an SSH key in ssh.private_key. config values are jq expressions over the bundle's params and dependencies, so the key can come from either:

massdriver.yamlyaml
steps:
- path: src
provisioner: opentofu
config:
ssh:
private_key: .dependencies.landing_zone.git_ssh_key

With ssh.private_key set, the provisioner rewrites HTTPS git URLs to SSH, so git::https://... module sources work against private repositories unchanged.

Child modules that take a resource​

A bundle's dependency receives the full payload of another bundle's resource: infrastructure IDs, IAM details, credentials, and network configuration. A child module can take that payload as one input.

A VM-based API that needs a PostgreSQL database declares the dependency:

massdriver.yamlyaml
dependencies:
database:
resource_type: postgresql@~1
required: true

A shared child module takes the resource:

hcl
module "postgres_access" {
source = "git::https://github.com/your-org/massdriver-catalog.git//modules/opentofu/azure-postgres-access?ref=v1.4.0"

postgres = var.database

# The identity to grant access to
principal_id = azurerm_user_assigned_identity.app.principal_id
}

The module reads secrets from Key Vault, creates the role assignment, and opens the firewall. Your security team writes it once, and every bundle that depends on a postgresql resource calls it. RBAC helpers, firewall managers, and secret readers become modules like this.

Pattern 3: Decorator Modules​

Bake these into bundles, or make them standalone bundles.

Decorator modules attach configuration to existing cloud resources and produce nothing other bundles use:

  • Azure diagnostic settings: forward logs and metrics from a resource
  • VM extensions: install monitoring agents, custom scripts, or security tools
  • Resource locks: prevent deletion of critical resources
  • Backup policies: attach backup configuration to databases or VMs

Option A: Bake into the resource bundle​

If every instance needs the decorator, such as diagnostic settings on every storage account, call it as a child module from the bundle:

bundles/azure-storage-account/src/main.tfhcl
resource "azurerm_storage_account" "main" {
name = replace("${var.md_metadata.name_prefix}sa", "-", "")
resource_group_name = var.resource_group_name
# ...
}

module "diagnostics" {
source = "git::https://github.com/your-org/massdriver-catalog.git//modules/opentofu/azure-diagnostic-settings?ref=v2.0.1"

target_resource_id = azurerm_storage_account.main.id
log_analytics_workspace_id = var.log_analytics.infrastructure.workspace_id
}

The storage account cannot be deployed without diagnostics.

Option B: Standalone bundle with a dependency​

If the decorator is optional or differs by environment, such as a security extension on production VMs only, make it a bundle that depends on the resource it decorates:

massdriver.yamlyaml
name: azure-vm-monitoring-extension
description: Attaches Azure Monitor Agent to a virtual machine

version: 1.0.0

dependencies:
virtual_machine:
resource_type: azure-virtual-machine@~1
required: true

params:
properties:
collection_interval:
type: integer
title: Collection Interval (seconds)
default: 60
minimum: 10
maximum: 300

steps:
- path: src
provisioner: opentofu

ui: {}

A bundle does not have to produce resources. A decorator bundle only consumes dependencies and provisions cloud resources.

Add the extension to the production environment and link it to the VM. Leave it out of development.

Which option to choose​

ConsiderationBake In (Option A)Standalone (Option B)
Always required?YesNo, varies by environment
Needs its own lifecycle?NoYes (update independently)
Different teams manage it?NoYes
Needs its own audit trail?NoYes

Pattern 4: Meta and Organizational Config​

Make these standalone bundles.

These modules manage organization-wide resources that belong to no single application:

  • SSO and Entra ID apps: SAML and OIDC configuration for SaaS integrations
  • DNS zones and records
  • Automation and service accounts: shared service principals or mailboxes
  • Organizational groups: Azure AD groups, AWS Organizations OUs
  • Azure Policy assignments: governance rules across subscriptions
  • Management groups: Azure management group hierarchies
  • Resource locks: subscription-level deletion protection
  • Budgets and cost alerts

As bundles they get:

  • An audit trail: every change is a versioned deployment.
  • Rollback to a previous deployment.
  • Preview environments to test a change, such as to an Entra app, before production.
  • Operator guides in operator.md, next to the bundle.
  • Resources other bundles depend on. An Entra app bundle can produce a resource that application bundles use for SSO.
DNS zones

Make the DNS zone a bundle that produces a resource, and set it as an environment default. Bundles in that environment then depend on the zone to manage their own records in it.

Example: Entra ID Application Bundle​

massdriver.yamlyaml
name: azure-entra-app
description: Manages an Azure Entra ID application registration for SSO integration

params:
required:
- display_name
- redirect_uris
properties:
display_name:
type: string
title: Application Display Name
redirect_uris:
type: array
title: Redirect URIs
items:
type: string
format: uri
token_lifetime_minutes:
type: integer
title: Token Lifetime (minutes)
default: 60
minimum: 5
maximum: 1440

dependencies:
azure_authentication:
resource_type: azure-service-principal@~1
required: true

resources:
entra_app:
resource_type: azure-entra-application@~1
required: true

steps:
- path: src
provisioner: opentofu

ui: {}

SSO changes then go through the bundle's form, with validation, an audit trail, and a preview environment to test them first.