aws / aws-toolkit-vscode

Amazon Q, CodeCatalyst, Local Lambda debug, SAM/CFN syntax, ECS Terminal, AWS resources
https://marketplace.visualstudio.com/items?itemName=AmazonWebServices.amazon-q-vscode
Apache License 2.0
1.43k stars 361 forks source link

SAM: cannot invoke "AWS::Lambda::Function" lambda #4281

Closed azgorov closed 6 months ago

azgorov commented 6 months ago

System details (run the AWS: About Toolkit command)

Question

Is debug config works with AWS CDK synth generated template?

Steps to reproduce:

  1. Generate init app with cdk.
  2. Add NodejsFunction
  3. cdk synth --no-staging > template.yml
  4. sam local invoke xxDE507B92 -e events/xx.json -this command works without any issues.

if I try normal node attachment to sam debugging works with specific localRoot

  1. sam local invoke xxDE507B92 -d 5858 -e events/xx.json
  2. config:
    {
            "name": "Attach to SAM Local",
            "type": "node",
            "request": "attach",
            "address": "localhost",
            "port": 5858,
            "localRoot": "${workspaceRoot}/cdk.out/asset.494325ce081b1d56eca9757238468014df0442a53c07341f158a22fdd2199d83/",
            "remoteRoot": "/var/task",
            "protocol": "inspector"
        }

if I try "AWS: Add SAM Debug Configuration" with synth template I got something like :

{
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "xx:xxDE507B92 (nodejs18.x)",
            "invokeTarget": {
                "target": "template",
                "templatePath": "${workspaceFolder}/template.yml",
                "logicalId": "xxDE507B92"
            },
            "lambda": {
                "payload": {},
                "environmentVariables": {}
            }
        },

Which looks fine but trying to run it I've got an exception:

2024-01-15 15:43:03 [ERROR]: SamLaunchRequestError: Invalid launch configuration: Template Resource xxDE507B92 in Template file /folder_xx/template.yml needs to be of type AWS::Serverless::Function or AWS::Lambda::Function [BadLaunchConfig]

Template.yml contains following resource:

  xxDE507B92:
    Type: AWS::Lambda::Function
    Properties:
      Architectures:
        - arm64
      Code:
        S3Bucket:
          Fn::Sub: cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}
        S3Key: 494325ce081b1d56eca9757238468014df0442a53c07341f158a22fdd2199d83.zip
      Environment:
        Variables:
          AWS_NODEJS_CONNECTION_REUSE_ENABLED: "1"
      Handler: index.handler
      Role:
        Fn::GetAtt:
          - xxServiceRoleEA518023
          - Arn
      Runtime: nodejs18.x
    DependsOn:
      - xxServiceRoleEA518023
    Metadata:
      aws:cdk:path: XxStack/xx/Resource
      aws:asset:path: /folder_xx/cdk.out/asset.494325ce081b1d56eca9757238468014df0442a53c07341f158a22fdd2199d83
      aws:asset:is-bundled: true
      aws:asset:property: Code
justinmk3 commented 6 months ago

Thanks for the report! The Toolkit error message is indeed confusing. https://github.com/aws/aws-toolkit-vscode/pull/4282 changes the validation to allow AWS::Lambda::Function resources.

If sam cli supports AWS::Lambda::Function , then you should get a better result with that change.

Can you try this vsix and confirm the result?

  1. Download and extract buildArtifacts.zip
  2. In VSCode, run Extensions: Install from VSIX to install the *.vsix file. (You can run commands with ctrl-shift-p or cmd-shift-p)
  3. Reload VSCode and verify that the Toolkit has the new version (not the older version).
azgorov commented 6 months ago

Thank you Justin.

New build fixed error message. Now SAM starts but it does not stop at breakpoint. I am not sure now if it is a problem is in my simple code or something in sam settings.

I have created demo project with 1 typescript lambda that shows debug issue: https://github.com/azgorov/demos

Best Kalin

justinmk3 commented 6 months ago

Thank you for testing! I can confirm the same results. I tried your demo repo with these steps:

  1. setup
    git clone https://github.com/azgorov/demos
    npm install
    cdk synth
    rm pnpm-lock.yaml
  2. update aws:asset:path: in template.yml
  3. confirmed these settings in tsconfig.json
    "inlineSourceMap": true,
    "inlineSources": true,
    "noEmit": false,
  4. set breakpoint in lib/lambda/xx.ts
  5. AWS Toolkit invoked the function, with the correct sam cli --debug command:
    
    sam local invoke --debug xxDE507B92 --template /tmp/aws-toolkit-vscode/vsctk26c62125/output/template.yaml -d 5858
  6. output correctly indicates that debugger was attached:
    Debugger attached.
  7. ... but it invokes the function without stopping at the breakpoint.
    2024-01-15T17:00:52.331Z 2b158033-9bcc-4784-bfcc-5173d6514127    INFO    {} 1
    END RequestId: 2b158033-9bcc-4784-bfcc-5173d6514127
    REPORT RequestId: 2b158033-9bcc-4784-bfcc-5173d6514127   Init Duration: 0.21 ms  Duration: 644.31 ms Billed Duration: 645 ms Memory Size: 128 MB Max Memory Used: 128 MB 
    {"statusCode": 200, "body": "{\"message\":\"Hello World\"}"}
justinmk3 commented 6 months ago

Workaround

Using a target: code launch config in your demo project, works:

        {
            "name": "node18",
            "type": "aws-sam",
            "request": "direct-invoke",
            "invokeTarget": {
                "target": "code",
                "lambdaHandler": "lib/lambda/xx.handler",
                "projectRoot": "${workspaceFolder}/"
            },
            "lambda": {
                "runtime": "nodejs18.x"
            },
            "sam": {
                "containerBuild": false,
                "skipNewImageCheck": false
            }
        },

With that launch config, I was able to hit a breakpoint in xx.ts.

justinmk3 commented 6 months ago

Status

Resolution

Because #4282 fixes the AWS::Lambda::Function error message, and #3210 tracks the more general topic of SAM typescript debugging, I am closing this issue.