aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.47k stars 1.16k forks source link

Error: 'str' object has no attribute 'decode'Bug: TITLE #6582

Closed daskabe closed 2 months ago

daskabe commented 6 months ago

Description:

unable to debug lambda locally.

Steps to reproduce:

The starter repo i started with can be found here https://github.com/mir4ef/aws-lambda-typescript-starter.

I cloned it, changed runtime to node18.x using latest container. Bellow is effort to get local debug to work.

$  sam build --use-container --build-image public.ecr.aws/sam/build-nodejs18.x:1.107.0-20240110201056
...
$  sam local invoke -d 9999 HelloWorldFunction 
 da@da-mbp  ~/workspace/app/backend/app-aws-services   master ±  sam build --use-container --build-image public.ecr.aws/sam/build-nodejs18.x:1.107.0-20240110201056
Starting Build inside a container                                                                                                                                                                                                                                                                         
Building codeuri: /Users/da/workspace/app/backend/app-aws-services/dist runtime: nodejs18.x metadata: {} architecture: x86_64 functions: HelloWorldFunction, MyFunctrion, MyFunc                                                                                                         

Fetching public.ecr.aws/sam/build-nodejs18.x:1.107.0-20240110201056 Docker container image......
Mounting /Users/da/workspace/app/backend/app-aws-services/dist as /tmp/samcli/source:ro,delegated, inside runtime container                                                                                                                                                              

Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Validate SAM template: sam validate
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch
[*] Deploy: sam deploy --guided
 Running NodejsNpmBuilder:NpmPack
 Running NodejsNpmBuilder:CopyNpmrcAndLockfile
 Running NodejsNpmBuilder:CopySource
 Running NodejsNpmBuilder:NpmInstall
 Running NodejsNpmBuilder:CleanUpNpmrc
 Running NodejsNpmBuilder:LockfileCleanUp
 da@da-mbp  ~/workspace/app/backend/app-aws-services   master ±  sam local invoke -d 9999 HelloWorldFunction                                         

Invoking app/function.lambdaHandler (nodejs18.x)                                                                                                                                                                                                                                                          
Local image is up-to-date                                                                                                                                                                                                                                                                                 
Using local image: public.ecr.aws/lambda/nodejs:18-rapid-x86_64.                                                                                                                                                                                                                                          

Mounting /Users/da/workspace/app/backend/app-aws-services/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated, inside runtime container                                                                                                                                          

Error: 'str' object has no attribute 'decode'
Traceback:
  File "click/core.py", line 1078, in main
  File "click/core.py", line 1688, in invoke
  File "click/core.py", line 1688, in invoke
  File "click/core.py", line 1434, in invoke
  File "click/core.py", line 783, in invoke
  File "samcli/cli/cli_config_file.py", line 347, in wrapper
  File "click/decorators.py", line 92, in new_func
  File "click/core.py", line 783, in invoke
  File "samcli/lib/telemetry/metric.py", line 184, in wrapped
  File "samcli/lib/telemetry/metric.py", line 149, in wrapped
  File "samcli/lib/utils/version_checker.py", line 42, in wrapped
  File "samcli/cli/main.py", line 95, in wrapper
  File "samcli/commands/local/invoke/cli.py", line 103, in cli
  File "samcli/commands/local/invoke/cli.py", line 201, in do_cli
  File "samcli/commands/local/lib/local_lambda.py", line 144, in invoke
  File "samcli/lib/telemetry/metric.py", line 324, in wrapped_func
  File "samcli/local/lambdafn/runtime.py", line 193, in invoke
  File "samcli/local/lambdafn/runtime.py", line 148, in run
  File "samcli/local/docker/manager.py", line 118, in run
  File "samcli/local/docker/container.py", line 362, in start

An unexpected error was encountered while executing "sam local invoke".
Search for an existing issue:
https://github.com/aws/aws-sam-cli/issues?q=is%3Aissue+is%3Aopen+Bug%3A%20sam%20local%20invoke%20-%20AttributeError
Or create a bug report:
https://github.com/aws/aws-sam-cli/issues/new?template=Bug_report.md&title=Bug%3A%20sam%20local%20invoke%20-%20AttributeError
 ✘ da@da-mbp  ~/workspace/app/backend/app-aws-services   master ±  

Observed result:

Error: 'str' object has no attribute 'decode'

Note :

just adding the port param causes the error

✘ $ sam local invoke HelloWorldFunction  
Invoking app/function.lambdaHandler (nodejs18.x)                                                                                                                                                                                                                                                          
Local image is up-to-date                                                                                                                                                                                                                                                                                 
Using local image: public.ecr.aws/lambda/nodejs:18-rapid-x86_64.                                                                                                                                                                                                                                          

Mounting /Users/da/workspace/app/backend/testDeleteMe/aws-lambda-typescript-starter/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated, inside runtime container                                                                                                                      
START RequestId: 9004258e-7a62-4bbc-a41f-f85c0bcf6eeb Version: $LATEST
END RequestId: 000a6707-d6cd-485d-a538-ad6dfd5c156b
REPORT RequestId: 000a6707-d6cd-485d-a538-ad6dfd5c156b  Init Duration: 0.79 ms  Duration: 927.32 ms     Billed Duration: 928 ms Memory Size: 128 MB     Max Memory Used: 128 MB
{"statusCode": 200, "body": "{\"message\":\"hello world\",\"version\":\"$LATEST\"}"}.   <<<<< SUCCESS

✘ $ sam local invoke -d 9999 HelloWorldFunction       <<<<< with port
Invoking app/function.lambdaHandler (nodejs18.x)                                                                                                                                                                                                                                                          
Local image is up-to-date                                                                                                                                                                                                                                                                                 
Using local image: public.ecr.aws/lambda/nodejs:18-rapid-x86_64.                                                                                                                                                                                                                                          

Mounting /Users/da/workspace/app/backend/testDeleteMe/aws-lambda-typescript-starter/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated, inside runtime container                                                                                                                      

Error: 'str' object has no attribute 'decode'
Traceback:
  File "click/core.py", line 1078, in main
  File "click/core.py", line 1688, in invoke
  File "click/core.py", line 1688, in invoke
  File "click/core.py", line 1434, in invoke
  File "click/core.py", line 783, in invoke
  File "samcli/cli/cli_config_file.py", line 347, in wrapper
  File "click/decorators.py", line 92, in new_func
  File "click/core.py", line 783, in invoke
  File "samcli/lib/telemetry/metric.py", line 184, in wrapped
  File "samcli/lib/telemetry/metric.py", line 149, in wrapped
  File "samcli/lib/utils/version_checker.py", line 42, in wrapped
  File "samcli/cli/main.py", line 95, in wrapper
  File "samcli/commands/local/invoke/cli.py", line 103, in cli
  File "samcli/commands/local/invoke/cli.py", line 201, in do_cli
  File "samcli/commands/local/lib/local_lambda.py", line 144, in invoke
  File "samcli/lib/telemetry/metric.py", line 324, in wrapped_func
  File "samcli/local/lambdafn/runtime.py", line 193, in invoke
  File "samcli/local/lambdafn/runtime.py", line 148, in run
  File "samcli/local/docker/manager.py", line 118, in run
  File "samcli/local/docker/container.py", line 362, in start

An unexpected error was encountered while executing "sam local invoke".
Search for an existing issue:
https://github.com/aws/aws-sam-cli/issues?q=is%3Aissue+is%3Aopen+Bug%3A%20sam%20local%20invoke%20-%20AttributeError
Or create a bug report:
https://github.com/aws/aws-sam-cli/issues/new?template=Bug_report.md&title=Bug%3A%20sam%20local%20invoke%20-%20AttributeError

Expected result:

HelloWorldFunction should have started for debuting ws; something like this. Debugger listening on ws://0.0.0.0:9999/a47891d0-d0d3-419e-8123-caf8baf4fbbc

and then i should have been able to connect to it from VSCode debugger

.vscode/launch.json looks like this:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Attach to SAM CLI",
      "type": "node",
      "request": "attach",
      "address": "localhost",
      "port": 9999,
      "localRoot": "${workspaceFolder}/backend/app-aws-services/src/",
      "remoteRoot": "/var/task",
      "protocol": "inspector",
      "stopOnEntry": false
    }
  ]
}

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

  1. OS: mac
  2. sam --version: "1.107.0",
  3. AWS region: us-east-2
{
  "version": "1.107.0",
  "system": {
    "python": "3.8.13",
    "os": "macOS-13.4-arm64-arm-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "24.0.7",
    "aws_cdk": "Not available",
    "terraform": "Not available"
  },
  "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"
  ]
}

Add --debug flag to command you are running

hnnasit commented 6 months ago

Hi @daskabe, can you check if this issue helps in attaching a debugger manually? You can also check out the AWS toolkits extension docs which abstracts away all the setup. Let us know if you still have questions.

mildaniel commented 2 months ago

Closing as this is inactive

github-actions[bot] commented 2 months ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.