aws / aws/aws-sam-cli

Bug: Unable to Debug Python with sam local start-api --debug-port - Python debugger libraries not being copied

Open
#6,546 14 comments 0 reactions 0 assignees View on GitHub
type/question
Dominant language
Python
Stars
6.7k
Forks
1.2k
Avg merge
1d 10h
Merged PRs (30d)
52

Description

### Description:
Team,
The SAM cli - `sam local start-api --debug-port 5858` doesn't allow developers to debug their code (pause at breakpoints), even if `--debug-function HelloWorldFunction` is specified.

If you inspect the container that gets launched, note 2 things:
1. We have the args between python3.11 and bootstrap.py. THe below image is what the args are when using vscode - launch.json - direct invoke. It also seems that the files needed for debugging e.g. `debugpy`.
Screenshot 2024-01-10 at 4 54 28 PM

2. The libraries needed for python debugging are also not copied e.g. debugpy etc. Below is what is copied with aws-sam direct invoke which is the correct files needed to debug:
Screenshot 2024-01-11 at 10 40 58 AM

On the other hand, in the same project, that same command works great with NodeJS.

The expected behavior is to have the SAM lambda container wait on port --debug-port to allow IDE / VSCode to attach to it for debugging. Constantly having to launch direct-invoke tanks the developer experience and sam local start-api is a perfect tool for auto-reload and debugging.

#### Same with CDK:
Note, the same behavior happens with a CDK project where the command is run against the CDK synth template :`sam local start-api -d 5858 -t ./cdk.out/template.yaml`

### Steps to reproduce:

- Create a new sam project for python `sam init`
- Select helloworldexample, choose python with zip packaging
- sam build , sam local start-api --debug-port 5858

### Observed result:
```
bash-3.2$ sam local start-api -d 5858 --debug
2024-01-11 10:32:06,754 | Config file location: /Users/xxxx/Documents/Development/sam/sam-python/samconfig.toml
2024-01-11 10:32:06,760 | Loading configuration values from [default.['local', 'start-api'].parameters] (env.command_name.section) in config file at
'/Users/xxxx/Documents/Development/sam/sam-python/samconfig.toml'...
2024-01-11 10:32:06,763 | Configuration values successfully loaded.
2024-01-11 10:32:06,765 | Configuration values are: {'stack_name': 'sam-python'}
2024-01-11 10:32:06,793 | Using SAM Template at /Users/xxxx/Documents/Development/sam/sam-python/.aws-sam/build/template.yaml
2024-01-11 10:32:06,891 | Using config file: samconfig.toml, config environment: default
2024-01-11 10:32:06,893 | Expand command line arguments to:
2024-01-11 10:32:06,894 | --template_file=/Users/xxxx/Documents/Development/sam/sam-python/.aws-sam/build/template.yaml --debug_port=(5858,) --host=127.0.0.1
--port=3000 --static_dir=public --layer_cache_basedir=/Users/xxxx/.aws-sam/layers-pkg --container_host=localhost --container_host_interface=127.0.0.1
2024-01-11 10:32:07,034 | local start-api command is called
2024-01-11 10:32:07,058 | No Parameters detected in the template
2024-01-11 10:32:07,115 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloWorldFunction is HelloWorldFunction
2024-01-11 10:32:07,116 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloWorldFunction1 is HelloWorldFunction1
2024-01-11 10:32:07,118 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloWorldFunction2 is HelloWorldFunction2
2024-01-11 10:32:07,120 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloWorldFunction3 is HelloWorldFunction3
2024-01-11 10:32:07,122 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloWorldFunction4 is HelloWorldFunction4
2024-01-11 10:32:07,124 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the
resource id
2024-01-11 10:32:07,130 | 0 stacks found in the template
2024-01-11 10:32:07,132 | No Parameters detected in the template
2024-01-11 10:32:07,189 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloWorldFunction is HelloWorldFunction
2024-01-11 10:32:07,190 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloWorldFunction1 is HelloWorldFunction1
2024-01-11 10:32:07,192 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloWorldFunction2 is HelloWorldFunction2
2024-01-11 10:32:07,194 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloWorldFunction3 is HelloWorldFunction3
2024-01-11 10:32:07,196 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloWorldFunction4 is HelloWorldFunction4
2024-01-11 10:32:07,198 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the
resource id
2024-01-11 10:32:07,202 | 6 resources found in the stack
2024-01-11 10:32:07,205 | Found Serverless function with name='HelloWorldFunction' and CodeUri='HelloWorldFunction'
2024-01-11 10:32:07,207 | --base-dir is not presented, adjusting uri HelloWorldFunction relative to
/Users/xxxx/Documents/Development/sam/sam-python/.aws-sam/build/template.yaml
2024-01-11 10:32:07,209 | Found Serverless function with name='HelloWorldFunction1' and CodeUri='HelloWorldFunction1'
2024-01-11 10:32:07,212 | --base-dir is not presented, adjusting uri HelloWorldFunction1 relative to
/Users/xxxx/Documents/Development/sam/sam-python/.aws-sam/build/template.yaml
2024-01-11 10:32:07,215 | Found Serverless function with name='HelloWorldFunction2' and CodeUri='HelloWorldFunction2'
2024-01-11 10:32:07,218 | --base-dir is not presented, adjusting uri HelloWorldFunction2 relative to
/Users/xxxx/Documents/Development/sam/sam-python/.aws-sam/build/template.yaml
2024-01-11 10:32:07,220 | Found Serverless function with name='HelloWorldFunction3' and CodeUri='HelloWorldFunction3'
2024-01-11 10:32:07,221 | --base-dir is not presented, adjusting uri HelloWorldFunction3 relative to
/Users/xxxx/Documents/Development/sam/sam-python/.aws-sam/build/template.yaml
2024-01-11 10:32:07,225 | Found Serverless function with name='HelloWorldFunction4' and CodeUri='HelloWorldFunction4'
2024-01-11 10:32:07,228 | --base-dir is not presented, adjusting uri HelloWorldFunction4 relative to
/Users/xxxx/Documents/Development/sam/sam-python/.aws-sam/build/template.yaml
2024-01-11 10:32:07,244 | Found '1' API Events in Serverless function with name 'HelloWorldFunction'
2024-01-11 10:32:07,250 | Found '1' API Events in Serverless function with name 'HelloWorldFunction1'
2024-01-11 10:32:07,252 | Found '1' API Events in Serverless function with name 'HelloWorldFunction2'
2024-01-11 10:32:07,255 | Found '1' API Events in Serverless function with name 'HelloWorldFunction3'
2024-01-11 10:32:07,261 | Found '1' API Events in Serverless function with name 'HelloWorldFunction4'
2024-01-11 10:32:07,265 | Detected Inline Swagger definition
2024-01-11 10:32:07,268 | Parsing Swagger document using 2.0 specification
2024-01-11 10:32:07,270 | Found '0' authorizers in resource 'ServerlessRestApi'
2024-01-11 10:32:07,272 | Lambda function integration not found in Swagger document at path='/nodejs-function-1' method='get'
2024-01-11 10:32:07,276 | Lambda function integration not found in Swagger document at path='/nodejs-function-2' method='get'
2024-01-11 10:32:07,280 | Lambda function integration not found in Swagger document at path='/python-function-2' method='get'
2024-01-11 10:32:07,282 | Lambda function integration not found in Swagger document at path='/python-function-1' method='get'
2024-01-11 10:32:07,284 | Lambda function integration not found in Swagger document at path='/hello' method='get'
2024-01-11 10:32:07,287 | Found '0' APIs in resource 'ServerlessRestApi'
2024-01-11 10:32:07,290 | Authorizer not found or disabled, returning early
2024-01-11 10:32:07,294 | Removed duplicates from '0' Explicit APIs and '5' Implicit APIs to produce '5' APIs
2024-01-11 10:32:07,298 | 5 APIs found in the template
2024-01-11 10:32:07,327 | Mounting HelloWorldFunction4 at http://127.0.0.1:3000/python-function-2 [GET]
2024-01-11 10:32:07,332 | Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
2024-01-11 10:32:07,335 | Mounting HelloWorldFunction3 at http://127.0.0.1:3000/python-function-1 [GET]
2024-01-11 10:32:07,337 | Mounting HelloWorldFunction1 at http://127.0.0.1:3000/nodejs-function-1 [GET]
2024-01-11 10:32:07,340 | Mounting HelloWorldFunction2 at http://127.0.0.1:3000/nodejs-function-2 [GET]
2024-01-11 10:32:07,343 | You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your
functions, changes will be reflected instantly/automatically. If you used sam build before running local commands, you will need to re-run sam build for the
changes to be picked up. You only need to restart SAM CLI if you update your AWS SAM template
2024-01-11 10:32:07,352 | Localhost server is starting up. Multi-threading = False
2024-01-11 10:32:07 WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:3000
2024-01-11 10:32:07 Press CTRL+C to quit
2024-01-11 10:32:10,422 | Constructed Event 1.0 to invoke Lambda. Event: {'version': '1.0', 'httpMethod': 'GET', 'body': None, 'resource': '/hello',
'requestContext': {'resourceId': '123456', 'apiId': '1234567890', 'resourcePath': '/hello', 'httpMethod': 'GET', 'requestId':
'50e46488-415d-4925-a99f-487df64acb5c', 'accountId': '123456789012', 'stage': 'Prod', 'identity': {'apiKey': None, 'userArn': None, 'cognitoAuthenticationType':
None, 'caller': None, 'userAgent': 'Custom User Agent String', 'user': None, 'cognitoIdentityPoolId': None, 'cognitoAuthenticationProvider': None, 'sourceIp':
'127.0.0.1', 'accountId': None}, 'extendedRequestId': None, 'path': '/hello', 'protocol': 'HTTP/1.1', 'domainName': 'localhost:3000', 'requestTimeEpoch':
1704987127, 'requestTime': '11/Jan/2024:15:32:07 +0000'}, 'queryStringParameters': None, 'multiValueQueryStringParameters': None, 'headers': {'Host':
'localhost:3000', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0', 'Accept':
'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8', 'Accept-Language': 'en-US,en;q=0.5', 'Accept-Encoding': 'gzip, deflate,
br', 'Connection': 'keep-alive', 'Upgrade-Insecure-Requests': '1', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'cross-site',
'X-Forwarded-Proto': 'http', 'X-Forwarded-Port': '3000'}, 'multiValueHeaders': {'Host': ['localhost:3000'], 'User-Agent': ['Mozilla/5.0 (Macintosh; Intel Mac OS X
10.15; rv:121.0) Gecko/20100101 Firefox/121.0'], 'Accept': ['text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8'],
'Accept-Language': ['en-US,en;q=0.5'], 'Accept-Encoding': ['gzip, deflate, br'], 'Connection': ['keep-alive'], 'Upgrade-Insecure-Requests': ['1'],
'Sec-Fetch-Dest': ['document'], 'Sec-Fetch-Mode': ['navigate'], 'Sec-Fetch-Site': ['cross-site'], 'X-Forwarded-Proto': ['http'], 'X-Forwarded-Port': ['3000']},
'pathParameters': None, 'stageVariables': None, 'path': '/hello', 'isBase64Encoded': False}
2024-01-11 10:32:10,433 | Found one Lambda function with name 'HelloWorldFunction'
2024-01-11 10:32:10,435 | Invoking app.lambda_handler (python3.11)
2024-01-11 10:32:10,436 | No environment variables found for function 'HelloWorldFunction'
2024-01-11 10:32:10,437 | Loading AWS credentials from session with profile 'None'
2024-01-11 10:32:10,461 | Resolving code path. Cwd=/Users/xxxx/Documents/Development/sam/sam-python/.aws-sam/build,
CodeUri=/Users/xxxx/Documents/Development/sam/sam-python/.aws-sam/build/HelloWorldFunction
2024-01-11 10:32:10,462 | Resolved absolute path to code is /Users/xxxx/Documents/Development/sam/sam-python/.aws-sam/build/HelloWorldFunction
2024-01-11 10:32:10,464 | Code /Users/xxxx/Documents/Development/sam/sam-python/.aws-sam/build/HelloWorldFunction is not a zip/jar file
2024-01-11 10:32:11,536 | Local image is up-to-date
2024-01-11 10:32:11,553 | Checking free port on 127.0.0.1:8483
2024-01-11 10:32:11,564 | Using local image: public.ecr.aws/lambda/python:3.11-rapid-x86_64.

2024-01-11 10:32:11,566 | Mounting /Users/xxxx/Documents/Development/sam/sam-python/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated, inside runtime
container
2024-01-11 10:32:11,991 | Setting up SIGTERM interrupt handler
{'body': None, 'headers': {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US,en;q=0.5', 'Connection': 'keep-alive', 'Host': 'localhost:3000', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'cross-site', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0', 'X-Forwarded-Port': '3000', 'X-Forwarded-Proto': 'http'}, 'httpMethod': 'GET', 'isBase64Encoded': False, 'multiValueHeaders': {'Accept': ['text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8'], 'Accept-Encoding': ['gzip, deflate, br'], 'Accept-Language': ['en-US,en;q=0.5'], 'Connection': ['keep-alive'], 'Host': ['localhost:3000'], 'Sec-Fetch-Dest': ['document'], 'Sec-Fetch-Mode': ['navigate'], 'Sec-Fetch-Site': ['cross-site'], 'Upgrade-Insecure-Requests': ['1'], 'User-Agent': ['Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0'], 'X-Forwarded-Port': ['3000'], 'X-Forwarded-Proto': ['http']}, 'multiValueQueryStringParameters': None, 'path': '/hello', 'pathParameters': None, 'queryStringParameters': None, 'requestContext': {'accountId': '123456789012', 'apiId': '1234567890', 'domainName': 'localhost:3000', 'extendedRequestId': None, 'httpMethod': 'GET', 'identity': {'accountId': None, 'apiKey': None, 'caller': None, 'cognitoAuthenticationProvider': None, 'cognitoAuthenticationType': None, 'cognitoIdentityPoolId': None, 'sourceIp': '127.0.0.1', 'user': None, 'userAgent': 'Custom User Agent String', 'userArn': None}, 'path': '/hello', 'protocol': 'HTTP/1.1', 'requestId': '50e46488-415d-4925-a99f-487df64acb5c', 'requestTime': '11/Jan/2024:15:32:07 +0000', 'requestTimeEpoch': 1704987127, 'resourceId': '123456', 'resourcePath': '/hello', 'stage': 'Prod'}, 'resource': '/hello', 'stageVariables': None, 'version': '1.0'}
END RequestId: c0ef6397-385f-453b-b8f1-038713569e75
REPORT RequestId: c0ef6397-385f-453b-b8f1-038713569e75 Init Duration: 0.08 ms Duration: 124.43 ms Billed Duration: 125 ms Memory Size: 128 MB Max Memory Used: 128 MB

2024-01-11 10:32:12,343 | Cleaning all decompressed code dirs
2024-01-11 10:32:12,346 | No Content-Type given. Defaulting to 'application/json'.
2024-01-11 10:32:12 127.0.0.1 - - [11/Jan/2024 10:32:12] "GET /hello HTTP/1.1" 200 -
```

### Expected result:
Expected that the sam lambda container will wait using debugpy for the IDE attach to it and then allow a developer to debug.

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

1. OS: Mac OS Ventura 13.6.3
2. `sam --version`: 1.106.0
4. AWS region: ca-central-1

```
{
"version": "1.106.0",
"system": {
"python": "3.8.13",
"os": "macOS-13.6.3-x86_64-i386-64bit"
},
"additional_dependencies": {
"docker_engine": "24.0.7",
"aws_cdk": "2.110.0 (build c6471f2)",
"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"
]
}
```

Feel free if you need more info.
Thanks!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.