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.48k stars 413 forks source link

Invalid SamLaunchRequestError for lambda runtime. #3161

Open whennemuth opened 1 year ago

whennemuth commented 1 year ago

Problem

I am seeing an error upon launching an "aws-sam" type direct-invoke of a lambda function based on on nodejs:

2023-02-11 01:51:36 [ERROR]: SamLaunchRequestError: Invalid launch configuration: Runtime for Template Resource LambdaFunction in Template file C:\whennemuth\workspaces\bu_workspace\bu-wp-assets-olap\template.yaml is either undefined or unsupported. [BadLaunchConfig]

What this error seems to be reporting is not true, the runtime, nodejs16.x is neither undefined or unsupported.

My launch configuration is as follows:

{
      "type": "aws-sam",
      "request": "direct-invoke",
      "name": "LAMBDA",
      "aws": {
        "credentials": "profile:infnprd",
        "region": "us-east-1"
      },
      "invokeTarget": {
        "target": "template",
        "templatePath": "${workspaceFolder}/template.yaml",
        "logicalId": "LambdaFunction"
      },
      "lambda": {
        "runtime": "nodejs16.x",
        "payload": {
          "path": "events/event.json"
        },
        "environmentVariables": {
          "S3_BUCKET": "bu-wp-assets-olap-dev-assets",
          "S3_REGION": "us-east-1",
          "HOST_NAME": "3.215.250.187",
          "SHIBBOLETH": "true"
        }
      }
    }

The lambda function in template.yaml is:

  LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub ${AWS::StackName}-s3-proxy
      Description: Receives https originated requests for s3 content and conditionally returns after applying authorization logic.
      Runtime: nodejs16.x
      Architectures: [ x86_64 ]
      CodeUri: ./
      Handler: src/Handler.GetAsset
      Timeout: 60
      Environment:
        Variables:
          S3_REGION: !Ref AWS::Region
          S3_BUCKET: !Ref Bucket
          SHIBBOLETH: !Ref Shibboleth
          HOST_NAME: 
            !If 
              - CustomHostName
              - !Ref HostName
              - !If 
                  - IncludeEIP
                  - !GetAtt EC2EIP.PublicIp
                  - !If 
                      - IncludeEC2
                      - "lookup"
                      - "local-ol"
      Policies:
        - S3CrudPolicy:
            BucketName: !Ref Bucket
        - Version: 2012-10-17
          Statement:
            - Effect: Allow
              Action: 
                - logs:*
              Resource: 
                - !GetAtt LambdaLogsGroup.Arn
            - Effect: Allow
              Action: 
                s3-object-lambda:WriteGetObjectResponse
              Resource:
                '*'

Steps to reproduce the issue

  1. Start a sam project in vscode

  2. Put the lambda content above in a file named template.yaml in the top level directory.

  3. Put the launch configuration above in the .vscode directory

  4. Create a file ./src/Handler.js:

    exports.GetAsset = async function(event, context) {
    console.log('hello');
    }
  5. Put a breakpoint on the line that prints "hello" and try to launch the "LAMBDA" configuration.

  6. This will work the first time, and you will see execution pause on the breakpoint.

  7. Terminate the debug session (do not let it run its course)

  8. Repeat step 5 and you should see the error.

  9. The only way to clear the error is to close vscode and reopen.

I'm not sure if this can be repeatable for you since I suspect it may have something to do with my specific system details (see below). However, I'd appreciate some clues as to how to get it to stop or an alternative approach that may bypass any suspected issues.

Expected behavior

No such error is expected - "nodejs16.x" should be accepted.

System details:

whennemuth commented 1 year ago

In order to take the template out of the question, I changed the "invokeTarget" section of the launch configuration shown above to be:

      "invokeTarget": {
        "target": "code",
        "projectRoot": "${workspaceFolder}/",
        "lambdaHandler": "src/Handler.GetAsset"
      }

Then when launching, I get a similar error:

Invalid launch configuration: Debug Configurations with an invoke target of "code" require a valid Lambda runtime value, expected one of [python3.7, python3.8, python3.9, dotnet6, java11, dotnetcore3.1, java8.al2, go1.x, nodejs12.x, java8, nodejs14.x, nodejs16.x, nodejs18.x]

The lambda runtime set in the launch configuration is still "nodejs16.x", which is in the list specified in the error. Can it be that the AWS toolkits sam launcher cannot properly parse the json? Or perhaps the toolkits internal list of valid runtimes is expecting a different format for node version than what it says is proper? For example, perhaps it's looking for something like "node16.x" or "nodejs16", etc.

JadenSimon commented 1 year ago

@whennemuth

Do you have any other extensions installed? It appears that the "lambda" section of your launch configuration is being removed prior to the Toolkit processing it. The error you're seeing is also (unfortunately) shown when no runtime exists.

whennemuth commented 1 year ago

Hi Jaden. Below is a list of extensions I have installed. One thing I had noted is that in order to reproduce this behavior, the initial debug session needs to be disconnected (shift + F5) manually without letting it run its course and ending naturally. Then, once run again, the error occurs. The only way to reset and get the error to go away is to restart vscode.

Since initially reporting this issue, I have restarted vscode, but not just the current window, but ALL windows I have had open. Now, the issue is not reproducible. It will come up again, as this seems to be a perennial behavior I see, though since my steps to reproduce are no longer reliable, I guess this issue is subject to closure. I'll let you decide about that - if the extensions list doesn't give you anything to go on, then I think there's no reason to keep this issue open.

Thanks, WH