Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAM typescript lambda: Unable to debug Hello World Example with Powertools #4899

Closed
samshteinman opened this issue May 1, 2024 · 4 comments
Labels
bug We can reproduce the issue and confirmed it is a bug. language:node debugging NodeJS / javascript / typescript lambdas using SAM local language:typescript sam

Comments

@samshteinman
Copy link

Description:

After calling sam init, I cannot run and debug the application.

Breakpoints are not hit and an error is shown

2024-04-29 22:29:18.368 [info] 2024-04-30T02:29:18.365Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'app'\nRequire stack:\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'app'","Require stack:","- /var/runtime/index.mjs","    at _loadUserApp (file:///var/runtime/index.mjs:1087:17)","    at async Object.load (file:///var/runtime/index.mjs:1119:21)","    at async start (file:///var/runtime/index.mjs:1282:23)","    at async file:///var/runtime/index.mjs:1288:1"]}
2024-04-29 22:29:18.376 [info] Waiting for the debugger to disconnect...
2024-04-29 22:29:18.433 [info] 30 Apr 2024 02:29:18,430 [ERROR] (rapid) Init failed InvokeID= error=Runtime exited with error: exit status 129
30 Apr 2024 02:29:18,430 [ERROR] (rapid) Invoke failed error=Runtime exited with error: exit status 129 InvokeID=bf09f315-6705-41df-8995-075ad78661e0
30 Apr 2024 02:29:18,431 [ERROR] (rapid) Invoke DONE failed: Sandbox.Failure

I'm not sure why it's looking for index.mjs
The auto-generated aws-toolkit-tsconfig.json contains:

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "inlineSourceMap": true,
        "outDir": "D:\\folderA\\sam-app\\hello-world",
        "rootDir": ".",
        "sourceRoot": "D:/folderA/sam-app/hello-world",
        "typeRoots": [
            "node_modules/@types"
        ],
        "types": [
            "node"
        ]
    }
}

Steps to reproduce:

sam init

Which template source would you like to use?
1 - AWS Quick Start Templates
2 - Custom Template Location
Choice: 1

Choose an AWS Quick Start application template
1 - Hello World Example
2 - Data processing
3 - Hello World Example with Powertools for AWS Lambda
4 - Multi-step workflow
5 - Scheduled task
6 - Standalone function
7 - Serverless API
8 - Infrastructure event management
9 - Lambda Response Streaming
10 - Serverless Connector Hello World Example
11 - Multi-step workflow with Connectors
12 - GraphQLApi Hello World Example
13 - Full Stack
14 - Lambda EFS example
15 - DynamoDB Example
16 - Machine Learning
Template: 3

Which runtime would you like to use?
1 - dotnet8
2 - dotnet6
3 - java17
4 - java11
5 - java8.al2
6 - nodejs20.x
7 - nodejs18.x
8 - nodejs16.x
9 - python3.9
10 - python3.8
11 - python3.12
12 - python3.11
13 - python3.10
Runtime: 6

Based on your selections, the only Package type available is Zip.
We will proceed to selecting the Package type as Zip.

Based on your selections, the only dependency manager available is npm.
We will proceed copying the template using npm.

Would you like to enable X-Ray tracing on the function(s) in your application? [y/N]: N

Would you like to enable monitoring using CloudWatch Application Insights?
For more info, please view https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html [y/N]:

Would you like to set Structured Logging in JSON format on your Lambda functions? [y/N]:

Project name [sam-app]:

-----------------------
Generating application:
-----------------------
Name: sam-app
Runtime: nodejs20.x
Architectures: x86_64
Dependency Manager: npm
Application Template: hello-world-powertools-typescript
Output Directory: .
Configuration file: sam-app\samconfig.toml

Next steps can be found in the README file at sam-app\README.md

Create a launch configuration

{
  "configurations": [
    {
      "type": "aws-sam",
      "request": "direct-invoke",
      "name": "hello-world:app.lambdaHandler (nodejs20.x)",
      "invokeTarget": {
        "target": "code",
        "projectRoot": "${workspaceFolder}/hello-world",
        "lambdaHandler": "app.lambdaHandler"
      },
      "lambda": {
        "runtime": "nodejs20.x",
        "payload": {},
        "environmentVariables": {}
      }
    }
  ]
}

Observed result:

Code is not executed
Breakpoints are not hit in app.ts
An error is shown in the AWS Toolkit OUTPUT console

Expected result:

Breakpoints are hit, code is executed

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

{
  "version": "1.115.0",
  "system": {
    "python": "3.11.8",
    "os": "Windows-10-10.0.19045-SP0"
  },
  "additional_dependencies": {
    "docker_engine": "25.0.3",
    "aws_cdk": "Not available",
    "terraform": "1.6.6"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}

System details (run the AWS: About Toolkit command)

  • OS: Windows_NT x64 10.0.19045
  • Visual Studio Code extension host: 1.88.1
  • AWS Toolkit version: 3.1.0
  • node: 18.18.2
  • electron: 28.2.8
@samshteinman samshteinman added the bug We can reproduce the issue and confirmed it is a bug. label May 1, 2024
@samshteinman
Copy link
Author

Linked issue: aws/aws-sam-cli#6999

@justinmk3 justinmk3 added language:node debugging NodeJS / javascript / typescript lambdas using SAM local language:typescript sam labels May 2, 2024
@justinmk3
Copy link
Contributor

I'm not sure why it's looking for index.mjs

AWS Toolkit currently assumes that any node 20.x project will have a .mjs entrypoint.

Since your example appears to be a typescript project, it may be necessary to build it first. Also using invokeTarget.target = template may help.

@samshteinman
Copy link
Author

Thanks for your response!

AWS Toolkit currently assumes that any node 20.x project will have a .mjs entrypoint.

I re-created the project and chose node 16, same issue. The template.yaml and the launch configuration both specify nodejs 16

There is no index file specified anywhere, I have an app.ts , the "main" in my package.json specifies "app.js"

Since your example appears to be a typescript project, it may be necessary to build it first.

The project is automatically being built, at least according to the AWS Toolkit output

Also using invokeTarget.target = template may help.

Switching over to template, I see that the function runs successfully, but no breakpoints are being hit.

@justinmk3 justinmk3 changed the title Unable to debug AWS Quick Start application - Hello World Example with Powertools for AWS Lambda SAM typescript lambda: Unable to debug Hello World Example with Powertools May 3, 2024
@samshteinman
Copy link
Author

samshteinman commented May 6, 2024

Pulled down the source code and starting to investigate...

One thing I'm noticing is that when sam build is triggered by the toolkit, the generated InputTemplate app___vsctk___template.yaml does not contain the metadata for ESBuild, is it supposed to? (The default template.yaml in the base-dir contains this information)

Either way, the output code in the temp directory, is all still Typescript, it's never transpiled to Javascript.

If I set breakpoints, and add the metadata manually to the app___vsctk___template.yaml, the esbuild step is triggered by sam build, and the code runs successfully, the error I originally talked about is gone... BUT still no breakpoints being hit.

Skipping the toolkit entirely, if I just call sam local invoke -d 5858 and attach, I still can't get the breakpoints to hit. So looks like issue is outside the toolkit?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug We can reproduce the issue and confirmed it is a bug. language:node debugging NodeJS / javascript / typescript lambdas using SAM local language:typescript sam
Projects
None yet
Development

No branches or pull requests

2 participants