Debugging and tracing Typescript AWS Lambda functions in a serverless environment using Visual Studio Code

After setting up an AWS Lambda using the serverless framework template, I encountered an issue when trying to debug/troubleshoot this lambda in my local environment. Despite successfully running a basic 'hello' function locally, I faced an error when attempting to set breakpoints for debugging.

[offline] Loading handler... (/Users/mickey/vault/github/backend-work/merlin-lambdas/be-ts-sponsored/.build/.webpack/service/src/functions/hello/handler)
[offline] _____ HANDLER RESOLVED _____
offline: Failure: Cannot find module '/Users/mickey/vault/github/backend-work/merlin-lambdas/be-ts-sponsored/.build/.webpack/service/src/functions/hello/handler'
Require stack:
- /Users/mickey/vault/github/backend-work/merlin-lambdas/be-ts-sponsored/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js

I am now seeking advice on how to effectively trace/debug this lambda, particularly since it is configured with webpack. Do I need to remove webpack for debugging purposes, or is there a way to work around it?

Your insights and assistance are highly appreciated.

Note: Relevant sections from my vcode launch.json and package.json files are provided below:

vcode launch.json:

{
  "type": "node",
  "request": "launch",
  "name": "DEBUG",
  "runtimeExecutable": "npm",
  "cwd": "${workspaceRoot}",
  "runtimeArgs": [
    "run-script",
    "debug"
  ],
  "console": "integratedTerminal",
  "restart": true,
  "internalConsoleOptions": "neverOpen",
  "port": 9229
}

package.json script samples:

"scripts": {
"start": "serverless offline -s dev",
"build": "yarn tsc",
"tsc": "tsc --project tsconfig.json",
"debug": "export SLS_DEBUG=* && node --inspect ./node_modules/.bin/serverless offline -s dev",
"test": "echo \"Error: no test specified\" && exit 1"
}
 

Answer №1

When running your function locally with the serverless framework, it can be challenging to trace breakpoints. Fortunately, there are alternative options available:

An effective approach is to isolate your code from AWS services that cannot be replicated locally using tools like jest mocks for DynamoDB, SQS, SNS, S3, etc.

Another method is to run your code similar to a regular Node application, utilizing tools like Jest for debugging and setting breakpoints independently of AWS services.

  • Consider using the SST framework instead of the serverless frameworks as it offers a workaround to run lambda functions locally within their AWS context (though not an official solution).

For more information on working locally and debugging with VS Code, you can refer to:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Here's a guide on using a button to toggle the display of password value in Angular, allowing users to easily hide

I have successfully implemented an Angular Directive to toggle the visibility of password fields in a form. However, I am facing an issue with updating the text displayed on the button based on the state of the input field. Is there a way for me to dynami ...

Why can't I omit <someUnion, oneInterface> in TypeScript?

Kindly review this simple example: interface A { a: number; } interface B { b: number; } interface C { c: number; } type ABC = A | B | C; type omitA = Omit<ABC, A>; https://i.sstatic.net/5Mun4.png Unfortunately, I am unable to exclude an i ...

Encountering an "Internal server error" message while deploying on AWS serverless, but everything works perfectly offline

Encountering an issue during deployment of a project using Serverless framework and Amazon Web Services, specifically with the API Gateway service. When running serverless offline on my local machine, everything functions correctly: Navigating to http://l ...

Files are nowhere to be found when setting up an angular project

After creating an Angular project, I noticed that some key files were missing in the initial setup, such as app.modules.ts and app-routing.modules.ts The project was generated using the command ng new name Here is a screenshot displaying all the files th ...

When running the 'nest build' command, a critical error occurs: 'FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory'

While my previous project works perfectly under the VS Code debugger, I am encountering issues when trying to build it from the command line. The specific error message I am receiving is: FATAL ERROR: Ineffective mark-compacts near heap limit Allocatio ...

Angular manages Fullcalendar, causing ViewRender to activate

I'm having trouble triggering an event when the view changes in Fullcalendar. Specifically, I need to detect when the month shifts to the previous or next one. I tried using ViewRender, but it didn't work for me. I've come across several res ...

Continually receiving the error message: "tsc.exe" has exited with an error code of 1

After I include a tsconfig.json file in my Visual Studio 2015 web solution, I encounter the error mentioned above. Furthermore, this prevents the compiler from generating js files again even when the "compileOnSave": true option is set. When I click on t ...

Adding a value to an array in TypeScript

When trying to add values to an array in my code, I encountered an error stating that "number" is not a valid type for the array. someArray: Array <{ m: number, d: Date}> = []; this.someArray.push(500,new Date(2020,1,15)); ...

Transforming the express app object into a data type instead of a regular variable

Currently, I am setting up my own TypeScript Express project and aiming to abstract the code for better organization. In my app.ts file, the code looks like this: import express from 'express' const app = express(); const port = 3000; require( ...

Tips on preventing Realtime database onWrite trigger function callback from iterating through data that has been altered

I am currently developing a 1 vs 1 game matching system using a real-time database. The system works by creating a record in the users table when a user signs in. Once there are two players with a status of placeholder, a cloud function generates a gameInf ...

Encountering an error due to an invalid type union when creating a new instance of a

My TypeScript code includes a Logger class that has an optional options parameter in its constructor. The options parameter includes a class generic C which represents custom levels. These custom levels are used within the class for methods like log(level: ...

Utilizing Typescript/React to Invoke Microsoft Graph Function and Validate M365 Group Owners

As a newcomer to React and TypeScript, I am eager to utilize the Microsoft Graph API in my React/TypeScript application to verify if the logged-in user is an owner of an M365 group. This is the code snippet I currently have: import { callMsGraph } from ...

The entire DOM in Angular2+ flickers upon loading a component using ngFor

I am facing an issue where, after a user clicks on an item to add it to a list and then renders the list using ngFor, there is a flickering effect on the screen/DOM. The strange thing is that this flicker only happens after adding the first item; subsequen ...

I am having trouble getting my react project to start using npm

Struggling to get started with React and encountering an issue with npm. Here's the code snippet: Error : user:~/reactApp$ sudo npm start > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2b0a7a3a1b6a3b2b2b5afee91fe9 ...

Transferring data from two child components back to the parent component

Within my web application, I have a parent component (A) and two children components (B, C). The parent component is responsible for maintaining the basic layout of the page, which remains consistent across all layouts. However, I need to dynamically swap ...

Redirect user to new page upon successful login using next-auth middleware

I recently implemented the next-auth middleware to protect all pages on my website. I followed the official documentation on next-auth (next-auth) and verified that it successfully redirects users who are not logged in. However, I encountered an issue whe ...

Retrieve class property in Angular by its name

How can I retrieve an array from a class like the one below without using a switch statement, dictionary, or other collection to look up the name passed into the method? export class ProcessOptions { Arm = [{ name: 'Expedited Review ("ER") ...

Troubleshooting a Pulumi script in Azure using Typescript and encountering difficulties with function writing

My background is in using terraform, but now I am trying out Pulumi/typescript for the first time. In my codebase, I have two files - index.ts and blob.ts. The create function in blob.ts is responsible for creating a storage account, resource group, blob ...

Utilizing the Embed tag to invoke URL requests repeatedly within an Angular application

I am currently developing a custom PDF viewer to display the PDF content fetched from an API response. The main issue I'm encountering is that the API call is being triggered multiple times, approximately 50 - 60 times or even more in some cases. Stra ...

To tally and showcase the existing strings within an array

I am working with an API named teachers that has the following structure: Teachers: [ { "id": "01", "teacherName": "Binky Alderwick", "studentIds": [ "010", "024", "031" ], "totalStudents":"20" }, { "id": " ...