Skip to main content

Brownfield: Choose a Jumping-Off Point

A Terraform monolith is one state file and one pipeline for the network, the clusters, the databases and the IAM of an environment. Every change plans all of it, and every new service makes it bigger.

Moving all of it into bundles at once means rewriting it and splitting its state. Instead, leave the monolith in charge of what it already owns, and have it publish the parts developers build on as Massdriver resources. Developers then deploy new work as bundles on top of them. New work stops landing in the monolith, and each piece moves out later, when there is a reason to change it.

TERRAFORM MONOLITHIMPORTEDNEW BUNDLESs3://tf-state/prodaws_vpc.mainaws_subnet.private[*]aws_eks_cluster.mainaws_db_instance.ordersaws_iam_role.*…400 more unchanged, still appliedby the same pipelineoutputsRESOURCEaws-vpcmass resource createorigin: importedORDERS-PRODdefault: networkecs-clusterALB in the public subnetstasks in the private subnetspostgresRDS in the internal subnetsown client security group

Jumping-off points​

Pick how much of the existing estate developers start from. Each one is imported by the Terraform that already owns it, with massdriver_imported_resource, and every apply keeps the resources current.

Jumping-off pointThe monolith importsDevelopers deploy in Massdriver
NetworkThe VPC or VNet and its subnetsVMs, databases, caches, queues and clusters, in the existing subnets
KubernetesCredentials for one namespace of each cluster, as the official massdriver-kubernetes-cluster typeHelm charts and the Kubernetes automation bundles, in that namespace
Landing zoneEverything one environment needs together: the cloud credential, network, DNS zone, cluster, key vault or log workspaceTheir app on a runtime, and the few data bundles it uses
CloudNetwork typeKubernetes typeLanding zone typesCatalog
AWSaws-vpcmassdriver-kubernetes-clusteraws-iam-role, aws-vpc, aws-dns-zone, aws-ecs-clustermassdriver-catalogs/aws
Azurevirtual-networkmassdriver-kubernetes-clusterazure-service-principal, virtual-network, private-dns-zone, key-vault, log-workspacemassdriver-catalogs/azure
GCPvirtual-network (pets catalog)massdriver-kubernetes-clustergcp-service-account, virtual-networkNo GCP catalog yet. Start from the pets catalog.

Import with the same resource type the catalog's bundles depend on. A bundle cannot tell an imported resource from one a bundle deployed, so everything that connects to an aws-vpc works with both.

Every import has the same shape​

Add the Massdriver provider to the monolith. It reads MASSDRIVER_ORGANIZATION_ID and MASSDRIVER_API_KEY from the pipeline's environment; use a service account that can create resources and grants.

terraform {
required_providers {
massdriver = {
source = "massdriver-cloud/massdriver"
version = "~> 2.5"
}
}
}

provider "massdriver" {}

Each imported resource is a massdriver_imported_resource with a JSON payload that matches the type's schema, and a massdriver_resource_grant that lets the environments that use it connect to it:

resource "massdriver_imported_resource" "network" {
name = "prod network (monolith)"
resource_type = "aws-vpc"
resource = jsonencode({ /* the payload */ })

# The API returns the type with its version (aws-vpc@0.0.0). Without this, every plan replaces the resource.
lifecycle {
ignore_changes = [resource_type]
}
}

resource "massdriver_resource_grant" "network" {
resource_id = massdriver_imported_resource.network.id
recipient_conditions = jsonencode({ "md-environment" = ["prod"] })
}

Massdriver checks the payload against the schema and refuses one that does not match. The provider has no environment default resource, so set defaults once with the CLI:

mass environment default <project>-prod <resource-id>

Network​

The smallest jumping-off point. The monolith keeps owning the network, routing and gateways; developers deploy everything that sits in a subnet.

aws-vpc holds the VPC, its availability zones, and every subnet with its tier:

TierRoute for 0.0.0.0/0
publicAn internet gateway
privateA NAT gateway or NAT instance
internalNone
data "aws_region" "current" {}
data "aws_availability_zones" "available" {}

locals {
zone_names = zipmap(data.aws_availability_zones.available.zone_ids, data.aws_availability_zones.available.names)

massdriver_subnets = concat(
[for s in aws_subnet.public : { id = s.id, cidr = s.cidr_block, availability_zone_id = s.availability_zone_id, tier = "public" }],
[for s in aws_subnet.private : { id = s.id, cidr = s.cidr_block, availability_zone_id = s.availability_zone_id, tier = "private" }],
[for s in aws_subnet.database : { id = s.id, cidr = s.cidr_block, availability_zone_id = s.availability_zone_id, tier = "internal" }],
)
}

resource "massdriver_imported_resource" "network" {
name = "prod VPC (monolith)"
resource_type = "aws-vpc"
resource = jsonencode({
id = aws_vpc.main.id
region = data.aws_region.current.region # .name on AWS provider 5.x
cidr = aws_vpc.main.cidr_block
availability_zones = [
for z in distinct([for s in local.massdriver_subnets : s.availability_zone_id]) : { id = z, name = local.zone_names[z] }
]
subnets = local.massdriver_subnets
internet_egress = length(aws_nat_gateway.this) > 0
})

lifecycle {
ignore_changes = [resource_type]
}
}

Rename aws_subnet.public, aws_subnet.private, aws_subnet.database and aws_nat_gateway.this to the monolith's addresses. With terraform-aws-modules/vpc, build the lists from module.vpc.public_subnet_objects, private_subnet_objects and database_subnet_objects. Check the tiers: a subnet the monolith calls private with no NAT route is internal here, and postgres places databases in internal subnets.

Without changing the monolith: from the AWS CLI

Read the VPC as it is and import it with the CLI. Re-run it when the network changes.

VPC_ID=vpc-0abc1234def567890
REGION=us-east-1

aws ec2 describe-vpcs --region "$REGION" --vpc-ids "$VPC_ID" > vpc.json
aws ec2 describe-subnets --region "$REGION" --filters "Name=vpc-id,Values=$VPC_ID" > subnets.json
aws ec2 describe-route-tables --region "$REGION" --filters "Name=vpc-id,Values=$VPC_ID" > routes.json
aws ec2 describe-availability-zones --region "$REGION" > azs.json
vpc-payload.jq
# A subnet without its own route table uses the VPC's main one.
def route_table($tables; $subnet):
($tables | map(select(any(.Associations[]?; .SubnetId == $subnet))) | first)
// ($tables | map(select(any(.Associations[]?; .Main == true))) | first);

def tier($table):
($table.Routes | map(select(.DestinationCidrBlock == "0.0.0.0/0")) | first) as $default
| if $default == null then "internal"
elif ($default.GatewayId // "" | startswith("igw-")) then "public"
else "private" end;

$routes[0].RouteTables as $tables
| $subnets[0].Subnets as $all
| ($all | map({
id: .SubnetId,
cidr: .CidrBlock,
availability_zone_id: .AvailabilityZoneId,
tier: tier(route_table($tables; .SubnetId))
})) as $tiered
| {
id: $vpc[0].Vpcs[0].VpcId,
region: $region,
cidr: $vpc[0].Vpcs[0].CidrBlock,
availability_zones: ($azs[0].AvailabilityZones
| map(select(.ZoneId as $z | $all | any(.AvailabilityZoneId == $z)))
| map({id: .ZoneId, name: .ZoneName})),
subnets: $tiered,
internet_egress: ($tiered | any(.tier == "private"))
}
jq -n --arg region "$REGION" \
--slurpfile vpc vpc.json --slurpfile subnets subnets.json \
--slurpfile routes routes.json --slurpfile azs azs.json \
-f vpc-payload.jq > aws-vpc.json

mass resource create -n "prod VPC (monolith)" -t aws-vpc -f aws-vpc.json
mass resource grant create <resource-id> --condition md-environment=prod
mass resource update <resource-id> -f aws-vpc.json # after the network changes

Build on the network​

Create the project for the new work, make the imported network its default, and add bundles. On AWS:

mass project create orders --name "Orders"
mass environment create orders-prod --name "Prod"
mass environment default orders-prod <network-resource-id>
mass environment default orders-prod <aws-credential-id>

mass component add orders postgres --id db --name "Orders DB"
mass instance deploy orders-prod-db --params=postgres.yaml

postgres puts the database in the VPC's internal subnets, or its private subnets when there are no internal ones, with its own client security group. It does not touch the VPC's route tables, gateways or the monolith's security groups. Neither do mariadb or valkey. A VM bundle you write takes its subnet from the network resource in the same way.

To run containers and automations on the imported network without importing a cluster, add dns-zone and ecs-cluster:

mass component add orders dns-zone --id dns --name "DNS"
mass component add orders ecs-cluster --id cluster --name "Cluster"
mass component link orders-dns.zone orders-cluster.dns

Set the dns instance's Hosted zone to existing with the ID of a Route 53 zone the monolith already has; it adds a certificate for the domain and every name directly under it, with its validation records, and leaves the rest of the zone alone. DNSSEC applies only to a zone it creates. The cluster's load balancer serves HTTPS only with a zone connected, and automations need a public https:// host. ecs-cluster needs public subnets in at least two availability zones and at least one private subnet. Then continue at Run Automations with these three resources in place of the platform-dev ones.

To use the network for one instance only, set a remote reference instead of a default:

mass instance remote-reference set orders-prod-db network <network-resource-id>

Kubernetes​

The monolith keeps owning the clusters, node pools, ingress controllers and cert-manager. For each team, it creates a namespace and a ServiceAccount that is admin of that namespace and nothing else, and imports its credentials as the official massdriver-kubernetes-cluster type. Developers deploy Helm charts into that namespace.

This works the same on EKS, GKE, AKS and any other cluster. Only the API server address and its certificate authority come from a different resource:

locals {
cluster_server = aws_eks_cluster.main.endpoint
cluster_ca = aws_eks_cluster.main.certificate_authority[0].data
}
resource "kubernetes_namespace_v1" "team" {
metadata { name = "team-orders" }
}

resource "kubernetes_service_account_v1" "massdriver" {
metadata {
name = "massdriver"
namespace = kubernetes_namespace_v1.team.metadata[0].name
}
}

resource "kubernetes_role_binding_v1" "massdriver" {
metadata {
name = "massdriver-admin"
namespace = kubernetes_namespace_v1.team.metadata[0].name
}
role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "ClusterRole"
name = "admin"
}
subject {
kind = "ServiceAccount"
name = kubernetes_service_account_v1.massdriver.metadata[0].name
namespace = kubernetes_namespace_v1.team.metadata[0].name
}
}

resource "kubernetes_secret_v1" "massdriver" {
metadata {
name = "massdriver-token"
namespace = kubernetes_namespace_v1.team.metadata[0].name
annotations = {
"kubernetes.io/service-account.name" = kubernetes_service_account_v1.massdriver.metadata[0].name
}
}
type = "kubernetes.io/service-account-token"
wait_for_service_account_token = true
}

resource "massdriver_imported_resource" "team_orders_namespace" {
name = "prod cluster / team-orders"
resource_type = "massdriver-kubernetes-cluster"
resource = jsonencode({
cluster = {
server = local.cluster_server
certificate-authority-data = local.cluster_ca
}
user = { token = kubernetes_secret_v1.massdriver.data["token"] }
namespace = kubernetes_namespace_v1.team.metadata[0].name
})

lifecycle {
ignore_changes = [resource_type]
}
}

resource "massdriver_resource_grant" "team_orders_namespace" {
resource_id = massdriver_imported_resource.team_orders_namespace.id
recipient_conditions = jsonencode({ "md-project" = ["orders"] })
}

Repeat the namespace, ServiceAccount, RoleBinding, Secret and import for each team, or for_each over a map of teams. Nothing in the token reaches outside its namespace.

Set it as an environment default of the team's environments, so every Helm bundle there deploys into the namespace, or as a remote reference on one instance. Brownfield Kubernetes covers what to ask the cluster's owners for, the same import without Terraform, and deploying pr-sync into the namespace.

Landing zone​

The widest jumping-off point. The monolith already builds a complete environment: the account or subscription, its credential, the network, DNS, a container cluster, keys and logging. It imports all of them, and each becomes an environment default. A developer's environment then needs only their app on a runtime and the two or three data bundles it uses.

ImportedTypeFrom the monolith
Credentialaws-iam-roleAn IAM role that trusts Massdriver's identity tokens. Refer to Connect an AWS account.
Networkaws-vpcNetwork
DNSaws-dns-zonezone_id, domain, region and certificate_arn of a Route 53 zone with an ACM certificate for *.<domain>
Clusteraws-ecs-clusterAn ECS cluster and its shared load balancer: the HTTPS listener, the load balancer's security group, the VPC and private subnets, and the zone
resource "massdriver_imported_resource" "dns" {
name = "prod DNS (monolith)"
resource_type = "aws-dns-zone"
resource = jsonencode({
zone_id = aws_route53_zone.main.zone_id
domain = aws_route53_zone.main.name
region = data.aws_region.current.region
certificate_arn = aws_acm_certificate.wildcard.arn
name_servers = aws_route53_zone.main.name_servers
})
lifecycle { ignore_changes = [resource_type] }
}

resource "massdriver_imported_resource" "cluster" {
name = "prod ECS (monolith)"
resource_type = "aws-ecs-cluster"
resource = jsonencode({
name = aws_ecs_cluster.main.name
arn = aws_ecs_cluster.main.arn
region = data.aws_region.current.region
load_balancer = {
arn = aws_lb.main.arn
dns_name = aws_lb.main.dns_name
zone_id = aws_lb.main.zone_id
security_group_id = aws_security_group.lb.id
listener_arn = aws_lb_listener.https.arn
protocol = "HTTPS"
}
network = {
id = aws_vpc.main.id
private_subnet_ids = [for s in aws_subnet.private : s.id]
internet_egress = length(aws_nat_gateway.this) > 0
}
zone = {
zone_id = aws_route53_zone.main.zone_id
domain = aws_route53_zone.main.name
}
})
lifecycle { ignore_changes = [resource_type] }
}

The bundles that run on the cluster expect what the catalog's ecs-cluster builds, so the monolith's cluster needs it too:

  • Fargate capacity providers on the cluster
  • An HTTPS listener with a certificate for *.<domain>
  • A load balancer security group that allows outbound traffic to the tasks' container ports
  • Private subnets with a route out through NAT, so tasks can pull images and reach the Massdriver API

Developers then add ecs-service (or an app built on it) with postgres, s3-bucket, sqs-queue or valkey. A service with Subdomain orders answers at https://orders.<domain> behind the existing load balancer. The automation -ecs bundles run on the same imported cluster.

Set every imported resource as a default of the environment that uses it:

for id in <credential-id> <network-id> <dns-id> <cluster-id>; do
mass environment default orders-prod "$id"
done

Each stage the monolith builds (dev, staging, prod) imports its own landing zone and grants it to that stage's environments, such as md-environment=dev.

Move a piece out later​

When a piece of the monolith needs a change anyway, move it into a bundle. State in Massdriver is per instance, so the resources are imported into one instance's state, not written into the bundle's code. A bundle with import blocks would carry one environment's resource IDs into every instance made from it.

  1. Write or pick a bundle whose OpenTofu matches the monolith's code for that piece, and that produces the same resource type, such as aws-vpc. Publish it with mass bundle publish --development and add it as a component. Do not deploy the instance.

  2. From the bundle's step directory, point tofu at the instance's state with an http backend, and import each resource. Refer to State management. tofu import runs locally, so it needs:

    • AWS credentials for the account. The bundle's aws provider assumes the role with the deployment's identity token (assume_role_with_web_identity), which does not exist on your machine. Replace that provider block for the import with one that uses your own credentials, such as a profile.
    • A value for every variable without a default. mass bundle build writes them to _massdriver_variables.tf; put the instance's params and the credential object in a throwaway import.auto.tfvars.json.

    Keep the backend file, the replaced provider block and the variable values out of the bundle: none of it may be published.

    cat > backend_import.tf <<'HCL'
    terraform {
    backend "http" {}
    }
    HCL

    export TF_HTTP_USERNAME=<organization-id>
    export TF_HTTP_PASSWORD=<api-key>
    export TF_HTTP_ADDRESS="https://api.massdriver.cloud/state/orders-prod-network/src"
    export TF_HTTP_LOCK_ADDRESS=$TF_HTTP_ADDRESS TF_HTTP_UNLOCK_ADDRESS=$TF_HTTP_ADDRESS

    tofu init
    tofu import aws_vpc.main vpc-0abc1234def567890
    tofu import 'aws_subnet.private["use1-az1"]' subnet-0123456789abcdef0
    tofu state list
    rm backend_import.tf import.auto.tfvars.json

    Then put the bundle's own aws provider block back before anything is published.

    The /massdriver:import agent does this step for you, including the backend, the variables and the cleanup. Refer to Claude Code and opencode.

  3. Run mass instance deploy orders-prod-network --plan until the plan changes nothing in AWS. Change the bundle's code or params, not the cloud, until it does.

  4. Deploy it, or propose the deployment for a second person to approve. Then tofu state rm the same addresses from the monolith and delete their code there.

  5. Set the instance's resource as the environment default in place of the imported one, and delete the imported resource.

Bundles connected to the VPC keep working through the switch: the new resource has the same type and the same IDs.

Claude Code and opencode​

The Massdriver plugin for Claude Code and the plugin for opencode share the same agents and commands. Brownfield work is where they save the most time, because most of it is reading an existing cloud account and matching it to a schema.

# Claude Code
/plugin marketplace add massdriver-cloud/claude-plugins
/plugin install massdriver@massdriver
opencode.json
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["@massdriver/opencode"]
}

Both run the Massdriver MCP server in Docker and read MASSDRIVER_API_KEY and MASSDRIVER_ORGANIZATION_ID, or your mass profile, from the environment the editor starts in. /massdriver:import needs plugin 4.4.0 or later.

Step on this pageCommandWhat the agent does
Network/massdriver:import, Register resource onlyReads the resource type's schema, collects the values from your cloud account, and creates the imported resource. No IaC, no lifecycle.
Build on the network, when the catalog has no bundle for it/massdriver:developDesigns the bundle, deploys it to a throwaway environment, fixes failures and compliance findings, and tears it down
Move a piece out later into a new bundle/massdriver:import, New bundleWrites and publishes the bundle, adds the component, runs tofu import into the instance's state, and plans in Massdriver until the plan is clean
Move a piece out later into a catalog bundle, such as vpc/massdriver:import, Existing bundleUses an undeployed instance of the bundle, imports into its state, and plans until it is clean

When the plan is clean, the agent proposes the deployment instead of running it, and a person approves it. It publishes only development releases. The plugin's guard blocks mass commands and MCP calls against production, but not a tofu import the agent runs in a shell, so the agent asks you to confirm the target instance before it writes state.