Thread: https://twitter.com/heitor_lessa/status/1137361365818060800
- Local prototyping with hot-reloading and step through debugging: npm run watch
- Bundle, minify and valid structure for SAM package/deploy: npm run build
- Webpack is instructed to dynamically look up for
index.tswithinsrcfolder -- (src/function1/index.ts, src/function2/index.ts) - Webpack builds a bundle per function to
build/function1/index.js,build/function2/index.js... - SAM CodeUri is set to the build folder
build/function1 - VS Code Debugging uses one entry per function pointing to the local bundle (
"localRoot": "${workspaceRoot}/build/ingest") - VS Code uses both Source Map and the
outFilesdirectives to map breakpoints back to the originalindex.tsfile
With this structure, it allows an organic growth in the number of functions within a project without bothering with the tooling boilerplate. It auto discovers, minifies, bundles and transpiles every function that has index.ts -- <new_function>/index.ts
Current structure
.
├── build
│ ├── get
│ │ ├── index.js
│ │ └── index.js.map
│ └── ingest
│ ├── index.js
│ └── index.js.map
├── jest.config.js
├── local-env-vars.json
├── package-lock.json
├── package.json
├── src
│ ├── get
│ │ ├── event.json
│ │ ├── index.ts
│ │ └── lib
│ │ ├── document_client.js
│ │ └── document_client.ts
│ └── ingest
│ ├── event.json
│ ├── index.ts
│ └── lib
│ ├── document_client.js
│ └── document_client.ts
├── template.yaml
├── tests
│ ├── get.test.ts
│ └── ingest.test.ts
├── tsconfig.json
└── webpack.config.js
-
Open up VS Code at
aws-serverless-airline-booking/src/backend/loyalty: cd aws-serverless-airline-booking/src/backend/loyalty && code . -
That should ensure
${workspaceRoot}is correctly set. IfSAM TS Webpackisn't shown in the Debugger drop down menu, create a new one by copying/pastinglaunch.jsoncontents. -
Add a breakpoint within
src/ingest/index.ts -
Run
sam local invoke IngestFunc -e src/ingest/event.json -d 5858 -
Switch to
index.tswithin VS Code and hit the debugger - Profit!
Reasons why Step through debugging wasn't working:
Missing libraryTarget: commonjs2 instruction for Webpack
Default bundle output wasn't compatible with what Lambda NodeJS Runtime expects, and therefore the error: {"errorMessage":"Handler 'handler' missing on module 'index'"}
Missing outFiles VSCode Debug option
outFiles instructs VSCode that code being debugged has been transpilled to a separate location. That alongside with sourceMaps: true makes it possible for VSCode to know where to circle back for the correct line in the breakpoint, and what file to find the source code now transpiled.