Creating Your Own Bundle
This guide turns an existing OpenTofu module into a bundle, then improves its params schema so the form validates input and needs fewer fields.
You will learn:
- How to create a bundle from a template and an existing module
- How
mass bundle importturns module variables into params - How to replace naming params with
md_metadata.name_prefix - How to constrain params with
enum,minimum,maximum, and$md.immutable
The example module demonstrates common patterns:
- Resource naming from project, environment, and application names
- Password generation with configurable complexity
- Encryption key creation
- Port assignment within a range
- Availability zone selection by region
Step 1: Get the example module​
The example module is in the getting-started repository. Clone it next to the catalog:
git clone https://github.com/massdriver-cloud/getting-started.git
The module is in getting-started/03-creating/module. It has main.tf, variables.tf, and outputs.tf, and creates only random resources.
Step 2: Create the bundle​
From the catalog root, point the CLI at the catalog's bundle templates and create a bundle from the opentofu template. -p copies the module's files into the bundle's src/ directory in place of the template's main.tf:
export MASSDRIVER_TEMPLATES_PATH="$PWD/templates"
mass bundle new \
--name my-first-bundle \
--description "Part 3 of the getting started guide" \
--template-name opentofu \
--output-directory my-first-bundle \
--params-directory ../getting-started/03-creating/module
The bundle has this layout:
my-first-bundle/
├── massdriver.yaml # Bundle configuration
├── operator.md # Runbook shown in the UI
├── icon.svg
└── src/
├── main.tf # From the module
├── variables.tf # From the module
├── outputs.tf # From the module
├── providers.tf # From the template
└── resources.tf # From the template, for massdriver_resource
Replace the template's params with the module's variables​
The template's massdriver.yaml has example params (instance_name, instance_count, and others) that the module does not use. Open my-first-bundle/massdriver.yaml and replace the params and ui blocks with:
params:
required: []
properties: {}
ui:
ui:order:
- "*"
Then import the module's variables as params:
mass bundle import --all my-first-bundle
mass bundle import reads the variables declared in each step's IaC and adds each one that is not already a param to params. Without --all it asks about each variable.
Step 3: Build and publish​
mass bundle build my-first-bundle
mass bundle build writes src/_massdriver_variables.tf. It declares a variable for each param and dependency that the IaC does not already declare. The module declares all of its variables in variables.tf, so the generated file holds only md_metadata, the deployment metadata Massdriver passes to every deployment:
variable "md_metadata" {
type = object({
default_tags = map(string)
deployment = object({ ... })
name_prefix = string
observability = object({ ... })
# ...
})
}
name_prefix is the instance's name prefix. With the default naming convention it is <project>-<environment>-<component>-<suffix>. See Naming convention.
Create the bundle's repository and publish it:
mass bundle create my-first-bundle
mass bundle publish my-first-bundle
Add the bundle to your environment's canvas and open its Config tab. Every module variable is a form field.
Step 4: Improve the params​
Edit my-first-bundle/massdriver.yaml and the files in my-first-bundle/src.
Replace the naming variables​
The module names resources from app_name, environment, and project_name. md_metadata.name_prefix already carries the project, environment, and component, so these params only add form fields and room for inconsistent names.
Remove
app_name,environment, andproject_namefromparams.propertiesand fromparams.required.Remove them from
src/variables.tf.Use the name prefix in
src/main.tf:hclresource "random_id" "server_id" {
byte_length = 4
prefix = "${var.md_metadata.name_prefix}-"
}
Restrict the encryption key length​
Allow only 16, 32, and 64 byte keys with an enum, and give the field a readable title:
encryption_key_length:
default: 32
description: Length of encryption key in bytes
title: Encryption Key Length
type: number
enum: [16, 32, 64]
Make the password secure by default​
Without a default, the form leaves the boolean options unchecked. Default each to true, and bound the length with minimum and maximum:
password_config:
description: Configuration for password generation
properties:
include_lower:
title: Include Lowercase
type: boolean
default: true
include_numeric:
title: Include Numeric
type: boolean
default: true
include_special:
title: Include Special Characters
type: boolean
default: true
include_upper:
title: Include Uppercase
type: boolean
default: true
length:
title: Length
type: number
minimum: 8
maximum: 64
default: 16
required:
- include_lower
- include_numeric
- include_special
- include_upper
- length
title: Password Configuration
type: object
Constrain the port range​
Bound both ports to 8000–9000 and set defaults:
port_range:
description: Port range for application
properties:
max:
title: Maximum Port Number
type: number
minimum: 8000
maximum: 9000
default: 9000
min:
title: Minimum Port Number
type: number
minimum: 8000
maximum: 9000
default: 8000
required:
- max
- min
title: Port Range
type: object
Restrict the region​
main.tf looks up availability zones by region:
locals {
availability_zones_per_region = {
"us-east-1" = ["us-east-1a", "us-east-1b", "us-east-1c", "us-east-1d", "us-east-1e", "us-east-1f"]
"us-west-2" = ["us-west-2a", "us-west-2b", "us-west-2c", "us-west-2d"]
}
}
Only us-east-1 and us-west-2 work, so make region an enum. Changing a region replaces the infrastructure, so also mark it $md.immutable:
region:
default: us-west-2
description: AWS region to deploy to
title: AWS Region
type: string
enum: ["us-east-1", "us-west-2"]
$md.immutable: true
Bound the zone count​
The shorter zone list has 4 entries, so set maximum: 4. Set minimum: 2 to keep the deployment highly available:
zone_count:
default: 2
description: Number of zones to select
title: Zone Count
type: number
minimum: 2
maximum: 4
Final params​
The finished params block:
params:
properties:
encryption_key_length:
default: 32
description: Length of encryption key in bytes
title: Encryption Key Length
type: number
enum: [16, 32, 64]
password_config:
description: Configuration for password generation
properties:
include_lower:
title: Include Lowercase
type: boolean
default: true
include_numeric:
title: Include Numeric
type: boolean
default: true
include_special:
title: Include Special Characters
type: boolean
default: true
include_upper:
title: Include Uppercase
type: boolean
default: true
length:
title: Length
type: number
minimum: 8
maximum: 64
default: 16
required:
- include_lower
- include_numeric
- include_special
- include_upper
- length
title: Password Configuration
type: object
port_range:
description: Port range for application
properties:
max:
title: Maximum Port Number
type: number
minimum: 8000
maximum: 9000
default: 9000
min:
title: Minimum Port Number
type: number
minimum: 8000
maximum: 9000
default: 8000
required:
- max
- min
title: Port Range
type: object
region:
default: us-west-2
description: AWS region to deploy to
title: AWS Region
type: string
enum: ["us-east-1", "us-west-2"]
$md.immutable: true
zone_count:
default: 2
description: Number of zones to select
title: Zone Count
type: number
minimum: 2
maximum: 4
required:
- encryption_key_length
- password_config
- port_range
- region
- zone_count
Step 5: Test the improved bundle​
Publish again. The bundle's version is 0.0.0, which can be republished:
mass bundle publish my-first-bundle
Reopen the instance's Config tab:
- The naming fields are gone.
- Encryption Key Length and AWS Region are dropdowns.
- Fields have readable titles and safe defaults.
- A maximum port of 9500 or a password length of 4 fails validation.
Click Deploy. The random_id prefix comes from md_metadata.name_prefix.
Next​
Using Bundle Deployment Metadata covers the rest of md_metadata.