aws-toolkit-vscode: Debugger not working invalid (or missing) template file

Describe the bug

Lambda debug does not works, when press to “Start Debugging” button an error appear:

Invalid (or missing) template file (path must be workspace-relative, or absolute):

To Reproduce

  1. Open template.yaml
  2. Press on button “Add Debug Configuration”
  3. Configure Lambda payload and environmentVariables (optional)
  4. Press on “Start Debugging”

The .vscode/launch.json file look like this:

{
  "configurations": [
    {
      "type": "aws-sam",
      "request": "direct-invoke",
      "name": "MyFunction",
      "invokeTarget": {
        "target": "template",
        "templatePath": "template.yaml",
        "logicalId": "MyFunction"
      },
      "lambda": {
        "runtime": "nodejs12.x",
        "payload": {
          "path": "event.json"
        },
        "environmentVariables": {
          "MY_VARIABLE": "test"
        }
      }
    }
  ]
}

Expected behavior

The Lambda function start locally and VSCode debug is attached to it

Desktop (please complete the following information):

  • OS: Linux
  • Visual Studio Code Version: 1.51.1
  • AWS Toolkit Version: v1.15.0

Additional context

The template file path is correct position, I tested with a simple “stat/file/cat” in terminal and the path is correct, same behaviour using relative or absolute path.

$ file template.yaml
template.yaml: ASCII text
$ file /home/me/project/template.yaml # just an example or my absolute path
template.yaml: ASCII text

The template file is correct, I currently deploy it as SAM application without any issue.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 29 (14 by maintainers)

Commits related to this issue

Most upvoted comments

Any idea on how to debug this problem?

If the template.yaml file contents don’t look like a SAM file, Toolkit will skip it. What do the contents look like? Also, is the file part of the vscode workspace?

The file el is generated by CDK and I can use it just fine via command line sam local invoke .... But AWS Toolkit does not load it preventing me from using sam cli Debug Configuration. We are starting the sam local invoke debugger with a VS Code preLaunchTask and attaching to the debugger to workaround this problem but it would be nice for AWS Toolkit to support the same templates sam cli does

We should surface this somehow, will keep this open to track that.

ah-ah! experienced something similar to @dudutwizer:

2021-01-25 15:09:30 [ERROR]: SAM debug: invalid config: Runtime for Template Resource <my function name> in Template file <my absolute template path> is either undefined or unsupported.

here what I set in my template file:

Globals:
  Function:
    Timeout: 6
    Runtime: nodejs12.x

I’m sorry, I hadn’t noticed the error message before activating the verbose module logs and searching for them. I use the global field a lot, almost all common configurations and, for the runtime, I use it by default on new templates… that’s the reason why I had this error for basically all my projects 😄.

Just moved Runtime configuration from global to function’s properties and the error is gone, thank you everyone!


definitely a duplicate of #1211

@dudutwizer thanks! That particular issue is tracked in https://github.com/aws/aws-toolkit-vscode/issues/1211

And that confirms that this issue is related to “invalid” template.yaml.

So we need to surface this somehow. Alternatively we could add “invalid” template.yaml’s to the registry, and then surface the validation errors at appropriate times (e.g. when used in a launch config).

I had the same issue, and I solved it by moving the handler definition from the global to the resource level. instead of

Globals:
  Function:
    Timeout: 30
    Runtime: python3.8
    Handler: app.lambda_handler

to

Globals:
  Function:
    Timeout: 30
Resources:
  test:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: test/
      Events:
        test:
          Type: Api
          Properties:
            Path: /
            Method: post
      Runtime: python3.8
      Handler: app.lambda_handler

Hi @justinmk3,

just tested with 1.18.0 version (the latest one) and the error still exist. I did the test that you suggested and it returns true.

Also did a test with lstatSync and it works:

> require('fs').lstatSync('/my/absolute/path/template.yaml')

Stats {dev: 66305, mode: 33188, nlink: 1, uid: 1000, gid: 1000, …}

And a final test with readFileSync, it can correct read the entire file content:

> require('fs').readFileSync('/my/absolute/path/template.yaml').toString()

"AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
.....
.....
....."

Installed the version you linked and I correctly see the new version number:

version

unfortunately did not solve the problem, same error doing the same tests as before (VSCode restart and file saving included)

Can you confirm that you have /home/me/project/ open in vscode as a workspace? The Toolkit resolves relative paths against the current workspace. Can you share a screenshot of your VSCode File Explorer tree?

For reference: the contents of the template.yaml don’t matter for this error, only the workspace. If we can figure out what’s different about the state of your VSCode session, we take that case into consideration when resolving the full path to template.yaml.