Our production deploy had grown to 22 minutes. Almost none of that time was spent on code that had changed.
We build and run our own products on AWS: a few dozen Lambda functions behind API Gateway, with DynamoDB, spread across a handful of CloudFormation stacks. Everything we run is defined as infrastructure as code. AWS SAM describes the serverless parts: Lambda functions, API Gateway and DynamoDB. Plain CloudFormation describes the rest: networking, IAM and monitoring. Every change goes through a pull request and a pipeline, preprod first and then production, so nothing is ever changed by hand in the console.
That discipline is the easy part to recommend. What took longer to learn is how a well-meant infrastructure-as-code setup slowly becomes slow, fragile and expensive to change. Here are the three lessons, in the order we learned them.
1. Only deploy what changed
A Lambda function publishes a new version whenever its code or its configuration changes. CloudFormation then updates its alias, and if you use canary deployments, each new version spends minutes taking a share of live traffic before it is promoted.
That is exactly what you want for a function you changed. It is pure waste for one you didn’t, and we found three ways in which unchanged functions were being republished on every release:
- A shared file copied into every function. A small module describing our events was packaged with every function, whether it used those events or not. A one-line note in that file changed the package of every function, so every function looked new.
- Build details in function settings. We stored the commit, the build time and the pipeline run in environment variables, which is a common way to make “which build is running?” easy to answer. But those values change on every build, so they gave every function a new configuration, and a new version, on every deploy, even when not a line of its code had moved.
- Canaries waiting on each other. One function referred to another function’s live alias. CloudFormation therefore waited for the first function’s canary to finish before it could even start the second, and three five-minute canaries ran one after another instead of side by side.
The fixes were plain:
- Each function carries only what it uses. A shared module is split, or copied per function with only the parts that function needs, so a change reaches only the functions it affects.
- The build’s identity lives outside the function. A small deploy record maps each function version to the build that produced it. The question “which build is running?” is still answered, and the function’s settings no longer change unless someone changed them on purpose.
- References that don’t create waits. Pointing at a function’s alias by name (
!Sub '${Function}:live') instead of referencing the alias resource keeps the call working, and CloudFormation no longer treats it as a resource dependency, so it stops ordering the two deploys. - Canaries only where customers are. The rule is one sentence: any function on a customer-facing path, or whose failure would hurt more than the people who run it, keeps its canary and its automatic rollback. A function only staff use deploys straight away, and the choice is written down per function, never left to a default.
Together these took our production deploy from 22 minutes to around five. A change to a single function now takes under two.
2. Measure before you optimise
The obvious next step looked like build caching. AWS SAM can cache built functions so unchanged ones are not rebuilt, and caching is the reflex answer to “the pipeline is slow”.
So we measured a typical 15-minute preprod deploy before building anything else:
| Step | Time |
|---|---|
| CloudFormation updating the stack | about 10 minutes |
| After-deploy steps (smaller stacks, migrations, smoke tests) | about 4 minutes |
| Packaging | 23 seconds |
| Building | 11 seconds |
The build was about 1% of the deploy. The cache worked perfectly and saved almost nothing, so we took it out again: plumbing that saves nothing is still plumbing someone has to understand. The rule we kept: cache only a step that measures slow. And the real cost, CloudFormation updating functions, is what the first lesson already attacks.
The same habit applies to tests. A test suite rarely needs a faster language. It needs to stop repeating expensive setup in every test, to run in parallel, and to run only the tests a change can reach on each push, with the full suite before each release. Measure where the seconds go, then fix the biggest one.
3. Split stacks along the domain, and keep data apart from code
CloudFormation refuses a stack with more than 500 resources, and a serverless stack reaches that faster than you’d think: every function brings a role, a version, an alias, permissions for each route, and a deployment group if it has a canary. When a stack nears the limit, the tempting fix is to move “whatever fits” into a second stack. We did that once: a side stack collected the scheduled jobs simply because no API route reached them. It worked, and it had no end state.
The shape we are moving to follows the business, not the template:
- One stack per bounded context. Each part of the product with its own data and its own language (a phone line, the inbox a business reads, its settings) gets its own stack, its own tests and its own deploy. Channels such as phone, WhatsApp and web chat are adapters into the same conversations, and each gets a small stack of its own, so a change to WhatsApp can never touch phone calls.
- Data in its own tier. Tables, event buses and secrets change a few times a year; functions change daily. They live in separate stacks. Every stateful resource is set to be retained if its stack is ever deleted or replaced, with point-in-time recovery switched on, so a code deploy cannot replace or delete data, even by mistake.
- One owner per table. Only the context that owns a table writes it. Others read through its API or its events, never by reaching into its tables, and a test over the IAM grants keeps it that way.
- Values travel through what the owner publishes. API paths, events and named parameters, not cross-stack exports that lock two stacks together.
- A budget per stack, never raised. A test warns at 400 resources and fails at 450. A stack over budget is split along a context line, and a new area of the product starts in its own stack from its first pull request.
The payoff is the same as in the first lesson, at a larger scale: a change to one part of the product deploys only that part, in parallel with nothing else waiting, and the data underneath is never at risk.

What I’d tell a team starting today
- Make “what changed” true. Before speeding anything up, check that a deploy only publishes what actually changed. Build stamps and shared files are the usual culprits.
- Put canaries where customers are. They are a safety net, not a ritual. Make the choice per function, on purpose.
- Measure each step of the pipeline first. The slow part is rarely the one you would guess.
- Draw your stacks from your domain. One context, one stack, with data kept apart, and a budget that stops a stack from quietly growing until it hits a wall.
None of this needs a different tool. CDK and Terraform have the same failure modes if the boundaries are wrong. The tool was never the problem; the boundaries were.
If your own deploys have crept up to tens of minutes, or your stacks are close to their limits, this is the kind of review we do for clients.
