Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Typescript sourcemaps in AWS Lambda and AWS Cloudwatch

I am using AWS Lambdas with Typescript built by ESBuild

ESBuild aggregates all the necessary code in a single index.js build file which makes the cold boot pretty fast and makes sure no unecessary code is sent. It can also generate sourcemaps.

Do you know how I can make Cloudwatch be aware of sourcemaps in order to get stacktraces related to my actual source code and not the build ?

like image 772
maxime Avatar asked Jun 17 '26 12:06

maxime


1 Answers

I found the answer here: NodeJS supports sourcemaps, and Lambda supports node options.

Generate your sourcemaps on Typescript and just add this Environment Variable to the lambda:

NODE_OPTIONS=--enable-source-maps

now when a stacktrace is logged, it points the original file and line

{
  "errorType": "Error",
  "errorMessage": "Missing process.env.TABLE_EVENT",
  "trace": [
    "Error: Missing process.env.TABLE_EVENT",
    "    at /var/task/index.js:12496:11",
    "        -> /backend/service/userService/lib/dynamodb.js:6:9",
    "    at /var/task/index.js:4:5",
    "    at /var/task/index.js:16979:18",
    "        -> /backend/service/userService/lib/handlers/publishReminders.js:4:26",
    "    at /var/task/index.js:4:5",
    "    at /var/task/index.js:17121:33",
    "        -> /backend/service/userService/lib/handlers/index.js:1:43",
    "    at /var/task/index.js:4:5",
    "    at Object.<anonymous> (/var/task/index.js:17136:16)",
    "        -> /backend/service/userService/lib/index.js:16:26",
    "    at Module._compile (internal/modules/cjs/loader.js:999:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)",
    "    at Module.load (internal/modules/cjs/loader.js:863:32)"
  ]
}
like image 151
maxime Avatar answered Jun 20 '26 11:06

maxime