Recently when working on a TypeScript project using AWS CDK inside a Turborepo monorepo, I needed a way to test an API Gateway setup locally without redeploying every time I tweaked a Lambda function code.
While tools like LocalStack can simulate multiple AWS services, I wanted to keep things simple and stick with the AWS SAM CLI. With a little bit of setup, I got a workflow that lets me edit Lambda code, save, and instantly test again — no redeploy required.
The Project Setup
My repo was organized with a lambdas folder containing multiple Lambda functions. These functions were referenced in the CDK stack as handlers for the API Gateway routes.
To enable hot-reload testing, I made sure I had the AWS SAM CLI installed, then added two scripts in my package.json and installed nodemon as a development dependency:
{
"synth": "cdk synth",
"startapi": "sam local start-api -t cdk.out/stack.template.json --skip-pull-image --warm-containers EAGER",
"watch": "nodemon --watch ../../lambdas -e ts --exec 'pnpm run synth'"
}
Here’s what they do:
-
startapi— Runssam local start-api, using the synthesized CloudFormation template from CDK (e.g: cdk.out/stack.template.json).--skip-pull-image— avoids pulling the latest Lambda image every time.--warm-containers EAGER— containers for all functions are loaded at startup and persist between invocations.
-
watch— Uses nodemon to watch for changes in the lambdas folder.- On every save, it runs npm run synth:local to regenerate the CDK template.
- SAM picks up the updated template and reloads automatically.
How It Works
With this setup running:
- Start SAM in one terminal:
pnpm startapi
- Run the watcher in another terminal:
pnpm watch
- Make a change to a Lambda function, save, and hit your local API endpoint again — your changes are now live.