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_idsand 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_stateneedss3:GetObjecton 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_stateconsumer's plan fails after a producer refactor, check output names first — renames are the breakage that doesn't announce itself.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
AWS Emulator Puts AWS on Your Laptop. Keep Your IAM Tests Off It.
AWS released AWS Emulator on November 11, a free local server that answers AWS API calls on 127.0.0.1 with per-run keys and fresh certificates.
What is Infrastructure as a Service? The Layer That Makes Everything Else Possible
Infrastructure as a Service gives you virtualized servers, storage, and networking on demand without owning any hardware. You rent compute and own everything that runs on it. Here is what IaaS actually is, what it demands from you.
What is Platform as a Service? The Sweet Spot Between "Too Easy" and "Too Much Work"
Platform as a Service sits between raw servers and fully managed software. You push code, the platform runs it. No Nginx config, no OS patches, no 3 AM server alerts. Here is what PaaS actually gives you, what it costs you in control, and how to decide if it is the right layer for your application.
The World now is Serverless and Container based
A new report from Datadog reveals that serverless computing could be entering the mainstream. This has already been the case in my world for the past few years. Results show that more than half of all organizations are using serverless on one of the three major public cloud providers: Amazon,