Skip to content

Lesson 10 of 14 · 15m

Splitting and Wiring Stacks

Split state by blast radius, then wire stacks with `terraform_remote_state` outputs or SSM lookups — the coupling decision is a design call.

Production isn't one root module. A single mega-state means every plan refreshes every resource — minutes of API calls on big estates — every apply can theoretically touch everything, and every open PR contends for the same lock. The split is by blast radius and rate of change: network changes rarely and hurts worst, data is where the irreversible things live, compute churns weekly. Separate roots, separate backends, separate locks.

Then the wiring problem appears: the app stack needs vpc_id and subnet_ids that the network stack created. Stacks have to talk, and how they talk is a design decision with failure modes on both sides.

terraform_remote_state: tight coupling

data "terraform_remote_state" "network" {
  backend = "s3"
  config = {
    bucket = "my-terraform-state"
    key    = "prod/network.tfstate"
    region = "us-east-1"
  }
}

resource "aws_instance" "app" {
  subnet_id = data.terraform_remote_state.network.outputs.private_subnet_ids[0]
}

The app stack reads the network stack's published output values directly from its state file. It works — and it's brittle in three specific ways:

  • Outputs are the API surface. Rename private_subnet_ids and downstream stacks fail at their next plan with a missing-attribute error — no compiler warns you, because the contract is runtime JSON, not types.
  • Read access to the whole file. terraform_remote_state needs s3:GetObject on the remote state — which contains everything in that stack, secrets included. You can't grant read on "just the outputs."
  • Refresh coupling. A broken remote state blocks every consumer's plan — the network stack's lock outage is your stack's plan outage.

Use it for same-team stacks that change together and share an access boundary anyway.

Looser coupling: look the value up in the cloud

The alternative is to publish facts where lookup doesn't require reading someone's state file:

# network stack publishes
resource "aws_ssm_parameter" "private_subnet_ids" {
  name  = "/prod/network/private_subnet_ids"
  type  = "StringList"
  value = join(",", aws_subnet.private[*].id)
}

# app stack consumes
data "aws_ssm_parameter" "private_subnet_ids" {
  name = "/prod/network/private_subnet_ids"
}

Now the contract is a parameter name in AWS — the network stack can refactor its guts freely as long as the parameter keeps existing. Access control is on the parameter, not a file full of unrelated attributes. Route53 records, tagged resources found via data "aws_vpc" { filter ... }, and Secrets Manager names are the same pattern: share facts through the cloud, not through each other's files.

The cost is an extra resource to manage per fact, and lookups that fail at plan time if the producer hasn't run yet — ordering dependencies become explicit instead of implicit in a shared file.

Data sources vs resources, one more time

Both patterns lean on data blocks, which is the general skill: data sources read reality without owning it. data "aws_vpc" for a VPC another team made, data "aws_caller_identity" for the account you're in, data "aws_region" for where you landed. If Terraform didn't create it and shouldn't destroy it, it enters your config as data, never resource — declaring a resource for something that exists means apply tries to create a duplicate or import adopts it into management it didn't ask for.

Field notes

  • Keep stack boundaries on change-rate lines: network, data, compute, edge. A stack that changes weekly shouldn't share state with one that changes yearly.
  • When two stacks both need a fact, the producer publishes once — don't have two stacks "both manage" the same SSM parameter.
  • If a terraform_remote_state consumer's plan fails after a producer refactor, check output names first — renames are the breakage that doesn't announce itself.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.