Watching TypeScript CDK Builds

Viewed 147

I need to be able to watch for my TypeScript lambda function changes within my CDK app. I'm using SAM to locally invoke the API and do not want to deploy to the cloud each time changes happen. So using something such as SAM Accelerate, for example, is not an option.

Currently, I must run cdk build and sam local start-api manually each time I change a single line in my function code, and it painfully takes a long time to start.

Any solutions or workarounds for this?

1 Answers

You need a Typescript watch feature with a hook to run arbitrary post-compile commands.* Typescript's tsc --watch can't do it (open issue), but the tsc-watch package can:

tsc-watch --onSuccess "./start-api.sh"

tsc-watch will call start-api.sh after each each successful compile, synthing a sam-friendly template version and starting the local testing api:

# start-api.sh
STACK_NAME=MyStack
npx cdk synth $STACK_NAME -a 'ts-node ./bin/app.ts' --no-staging --no-validation --quiet --output cdk.local
sam local start-api --template cdk.local/$STACK_NAME.template.json

* cdk watch (an alias of cdk deploy --watch) won't work in your case, because you don't want to deploy on each change.

Related