Demonstrates sharing infrastructure across multiple Next.js branch deployments using NextjsRegionalContainers.
SharedInfra stack (deploy once)
├── VPC + Gateway Endpoints (S3, DynamoDB)
├── ECS Cluster
├── ALB + HTTP Listener (default 404)
├── S3 Cache Bucket
├── S3 Static Assets Bucket
├── DynamoDB Revalidation Table
└── SSM Parameters (resource IDs)
branch-<name> stack (deploy per branch)
├── NextjsRegionalContainers (Fargate service only)
└── ALB Listener Rule (host-header routing → target group)
Each branch gets its own Fargate service and ALB listener rule. The ALB routes
traffic based on the Host header (<branch>.app.example.com).
For NextjsGlobalContainers, CloudFront forwards the Host header to the ALB
origin, so the ALB handles all branch routing — no CloudFront changes needed per
branch.
- AWS CLI configured with credentials
- Docker running (for Next.js container build)
- Create
.envwithAWS_PROFILE="your-profile"
# Install dependencies
pnpm i
# 1. Deploy shared infrastructure (once)
pnpm run deploy:shared
# 2. Deploy a branch (defaults to "main")
pnpm run deploy:branch
# Deploy a specific branch
pnpm run deploy:branch -- -c branchName=feature/my-feature# Destroy branch stack first
pnpm run destroy:branch
# Then destroy shared infrastructure
pnpm run destroy:sharedPoint *.app.example.com to the ALB DNS name (CNAME or Route 53 alias) so
host-header routing works. Each branch is reachable at
<sanitized-branch>.app.example.com.
SharedInfraStackcreates all long-lived resources and writes their IDs to SSM Parameter Store under/cdk-nextjs/bring-your-own/*.BranchStackreads those SSM parameters at synth time, imports the resources viafromLookup/fromBucketName/fromTableName, and passes them toNextjsRegionalContainers.removeAutoCreatedListener()prevents the Fargate service from creating a duplicate listener on the shared ALB.- A host-header listener rule routes
<branch>.app.example.comto the branch's target group with a deterministic priority derived from the branch name.