<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Scott Burton on Medium]]></title>
        <description><![CDATA[Stories by Scott Burton on Medium]]></description>
        <link>https://medium.com/@scottburton_60429?source=rss-99f9f98c08b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Z_Btghv_SBEeIych.jpg</url>
            <title>Stories by Scott Burton on Medium</title>
            <link>https://medium.com/@scottburton_60429?source=rss-99f9f98c08b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 05:38:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@scottburton_60429/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How We Structure Our CDK Stacks (and Why)]]></title>
            <link>https://medium.com/@scottburton_60429/how-we-structure-our-cdk-stacks-and-why-ef718eb1a0db?source=rss-99f9f98c08b------2</link>
            <guid isPermaLink="false">https://medium.com/p/ef718eb1a0db</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Scott Burton]]></dc:creator>
            <pubDate>Mon, 01 Jun 2026 15:29:11 GMT</pubDate>
            <atom:updated>2026-06-01T15:29:11.012Z</atom:updated>
            <content:encoded><![CDATA[<h4><em>A few tips (that we learned the hard way) to keep you building in CloudFormation</em></h4><h3>Just Another Day At The Frustration Factory</h3><p>After trying everything else, we figured out how to sidestep some of CloudFormation’s worst traps.</p><p>Every CDK app we build, we want boring in the same three ways: predictable to deploy, safe to change, and structurally incapable of accidentally wiping a database. None of that is the default. The default is a single stack that grows until the day it betrays you.</p><p>Here’s the layout we keep arriving at instead, and the specific mistakes that taught us to arrive at it.</p><h3>Split by blast radius, not by tidiness</h3><p>The instinct is to organize stacks by category — networking here, compute there, storage off to the side. Tidy. Also wrong. The question that matters isn’t <em>what is this thing</em>, it’s <em>what happens when CloudFormation decides to replace it</em>.</p><p>So we split on exactly that axis. Two top-level stacks.</p><p>The <strong>stateful stack</strong> holds what you never want recreated on a whim: the VPC, RDS, Redis, the S3 exports bucket, Secrets Manager entries, the ECR repositories. Every removal policy in here is RETAIN or SNAPSHOT. It changes rarely, by design.</p><p>The <strong>stateless stack</strong> holds the compute and ingress tier: the ECS cluster, the web and runtime services, the load balancer, the cert, SES. This is the stuff we redeploy all day. It’s built to be thrown away and rebuilt without anyone holding their breath.</p><p>This split makes replacing the <em>customer-facing</em> tier painless. A failed deploy of the disposable tier cannot orphan or replace your database. Physically can’t — they’re different stacks with different lifecycles, so a bad afternoon in the compute tier stays in the compute tier. Deploy order is one line:</p><pre>stateless.addDependency(stateful);</pre><p>The data tier always exists before the compute tier goes looking for it.</p><h3>The export trap</h3><p>Once the stacks are split, you have to share things across them — a VPC ID, a secret ARN. The obvious tool is a CloudFormation export. Reach for it and you quietly undo the entire reason you split.</p><p>CDK makes this even more tempting; public class properties become exports the moment you inject them as props. You may not even know you’re creating exports.</p><p>Exports create <em>import locks</em>. The moment Stack B imports an exported value from Stack A, you can no longer change or delete that value in A while B still references it. An innocent change can lead to hours of frustration and require a non-intuitive fix that’s <a href="https://blog.infra.kiwi/aws-cdk-cross-stack-references-54e52e2f8053">documented nowhere but a few helpful Medium posts</a>.</p><p>We split stateful from stateless precisely so the stateful tier could evolve freely — and an export welds the two back together at the exact seam we were keeping loose.</p><p>So we don’t export. The stateful stack <strong>publishes</strong> its outputs to SSM Parameter Store under a /myapp/* path contract. The stateless stack <strong>reads</strong> them at deploy time and reconstructs the shared resources with the from* lookups: Vpc.fromVpcAttributes, Secret.fromSecretCompleteArn, ecr.Repository.fromRepositoryName. Zero template-level coupling between the two stacks.</p><p>The paths live in one place, ssm.ts, as a typed contract both sides import — so a typo can&#39;t quietly mint two parameter names that almost match:</p><pre>// ssm.ts - StatefulStack publishes these; StatelessStack reads them.<br>// No CloudFormation Export/Import, so stateful resources stay changeable.<br>export const SECRET_NAMES = [<br>  SECRET_ONE,<br>] as const;<br>export type SecretName = (typeof SECRET_NAMES)[number];<br><br>// ECR repo names are fixed, not SSM-published. The Application stack references<br>// them via ecr.Repository.fromRepositoryName, which builds the ARN from the<br>// literal name + account/region - no export, no cross-stack lock.<br>export const ECR_REPO_NAMES = {<br>  backend: &quot;backend&quot;,<br>  web: &quot;frontend&quot;,<br>} as const;<br><br>const PREFIX = &quot;/myapp&quot;;<br><br>export const SsmPaths = {<br>  vpcId: `${PREFIX}/network/vpc-id`,<br>  publicSubnetIds: `${PREFIX}/network/public-subnet-ids`,<br>  privateSubnetIds: `${PREFIX}/network/private-subnet-ids`,<br>  secretArn: (name: SecretName) =&gt; `${PREFIX}/secrets/${name}-arn`,<br>} as const;</pre><p>And we don’t trust ourselves to remember — we verify. Synth output gets checked in CI for <em>zero</em> cross-stack exports. This matters because if anyone passes a CDK object directly across the stack boundary, CDK doesn’t warn you. It silently generates an export, and now you own the import lock you spent all this effort avoiding. The check catches it. The fix is always “add an SSM parameter,” never “add an export.”</p><h3>Nested stacks, because 500 comes at you fast</h3><p>A single CloudFormation stack caps at 500 resources. A real VPC plus RDS plus an ECS service behind an ALB eats that budget faster than anyone estimates, and the worst possible time to learn where the ceiling is, is mid-deploy, in the error message, with traffic waiting. (<a href="https://medium.com/@abhishekp1996/splitting-your-aws-stack-using-nested-stacks-3294692c6ee9">Nested stacks are the usual way out.</a>)</p><p>So each of our two top-level stacks is really a thin parent over <strong>nested stacks</strong>, and each nested stack gets its own 500-resource budget:</p><ul><li>Stateful → Network, Data, Secrets, Registry</li><li>Stateless → Application (ECS + ALB), Email</li></ul><p>Nesting buys headroom. The better dividend is clean seams. Network owns the VPC and the shared &quot;client&quot; security group; Data takes that VPC and stands up RDS and Redis. The composition reads top-to-bottom in the parent.</p><p>Two placement calls are worth defending, because both look wrong until you’ve been bitten:</p><p><strong>ECR lives in stateful, not in its own registry stack.</strong> There’s a chicken-and-egg with container images: the repo has to exist before you can push, and the service shouldn’t start before the image exists. The temptation is a dedicated registry stack to “own” the ordering. We solve it with stack <em>separation</em> instead — repos in stateful, the services that pull them in stateless. Deploy stateful → push images → deploy stateless, and the ordering falls out for free. The RETAIN policy means a stateless rollback can&#39;t take the images down with it.</p><p><strong>ALB and ECS stay co-located</strong> in the Application nested stack. Registering the service with the load balancer mutates the service&#39;s security group to let the ALB in. Split those across two nested stacks and congratulations, you&#39;ve built a circular dependency. They belong together; don&#39;t let symmetry argue you out of it.</p><p>Here’s the whole layout in app.ts, ordering and all:</p><pre>import * as cdk from &quot;aws-cdk-lib&quot;;<br>import { StatefulStack } from &quot;./stacks/stateful-stack.js&quot;;<br>import { StatelessStack } from &quot;./stacks/stateless-stack.js&quot;;<br><br>const app = new cdk.App();<br><br>// Environment-agnostic on purpose: synth is a no-deploy target with no AWS<br>// permissions, so we avoid concrete-account AZ lookups. The account resolves<br>// from the deploy credentials; region defaults to us-west-2.<br>const env = { region: process.env[&quot;CDK_DEFAULT_REGION&quot;] ?? &quot;us-west-2&quot; };<br>const stateful = new StatefulStack(app, &quot;StatefulStack&quot;, { env });<br>const stateless = new StatelessStack(app, &quot;StatelessStack&quot;, {<br>  env,<br>});<br><br>// Deploy ordering. Stateful owns the ECR repos, the secret placeholders, and<br>// the data tier; stateless reads its SSM parameters and pulls the images.<br>stateless.addDependency(stateful);</pre><h3>The small stuff that keeps it alive</h3><p>A handful of unglamorous helpers do most of the work of keeping this maintainable:</p><p><strong>AZ pinning.</strong> We keep synth offline and account-agnostic on purpose. A concrete account number triggers a live DescribeAvailabilityZones lookup that wants credentials we&#39;d rather not hand the build. So availabilityZonesFor(region) returns region-derived AZ names, both stacks override the availabilityZones getter to use it, and the VPC and the stateless tier&#39;s fromVpcAttributes agree without a single API call:</p><pre>// Region-prefixed AZ names. Pinning them keeps synth offline and ensures the<br>// stateful VPC and the stateless fromVpcAttributes agree on AZs.<br>export function availabilityZonesFor(region: string): string[] {<br>  return [`${region}a`, `${region}b`];<br>}</pre><p><strong>Fn.split with </strong><strong>assumedLength.</strong> Subnet IDs come out of SSM as one deploy-time token of unknown length. fromVpcAttributes insists the subnet count be a multiple of the AZ count, so we hand Fn.split the AZ count as its third argument and get back a fixed-length token array it&#39;ll accept.</p><pre>publicSubnetIds: cdk.Fn.split(&quot;,&quot;, read(SsmPaths.publicSubnetIds), azCount)</pre><p><strong>A staged deploy script.</strong> CDK can’t pause mid-deploy to seed container images or wait for a human to paste secret values into the console. So deploy.sh stages it: deploy stateful → build-and-push images → fill the CHANGE_ME secret placeholders by hand → deploy.sh --finish for the stateless tier.</p><p>None of this is exotic. The theme never changes: split by lifecycle, decouple through data instead of template references, keep synth offline so the build never needs credentials. Boring, predictable, hard to break — the only three things you actually want from infrastructure code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ef718eb1a0db" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>