如何在 Visual Studio 代码上使用 Python 设置 AWS SAM 本地调试?
How to setup AWS SAM local debugging with Python on Visual Studio Code?
我试图在 Visaul Studio Code 中使用 lambda (python) 的调试功能。我按照 AWS Docs 上的说明进行操作,但无法在调试模式下触发 python 应用程序。
请看看你是否知道这个问题,如果我设置不正确,谢谢。
观察
- 开始申请
应用程序似乎没有在指定的调试端口上启动?
- 请求通话
无法到达端点,python 未输入应用程序
如果通过端口 3000 访问,应用程序可以成功完成
已执行设置
- 初始化项目,按照提示安装ptvsd
- 在 python 代码上启用 ptvsd
- 添加启动配置
项目结构
Python 来源
这基本上只是 python
的官方 helloworld 示例
import json
# import requests
import ptvsd
# Enable ptvsd on 0.0.0.0 address and on port 5890 that we'll connect later with our IDE
ptvsd.enable_attach(address=('localhost', 5890), redirect_output=True)
ptvsd.wait_for_attach()
def lambda_handler(event, context):
"""Sample pure Lambda function
Parameters
----------
event: dict, required
API Gateway Lambda Proxy Input Format
Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
context: object, required
Lambda Context runtime methods and attributes
Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html
Returns
------
API Gateway Lambda Proxy Output Format: dict
Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
"""
# try:
# ip = requests.get("http://checkip.amazonaws.com/")
# except requests.RequestException as e:
# # Send some context about this error to Lambda Logs
# print(e)
# raise e
return {
"statusCode": 200,
"body": json.dumps({
"message": "hello world",
# "location": ip.text.replace("\n", "")
}),
}
启动配置
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "SAM CLI Python Hello World",
"type": "python",
"request": "attach",
"port": 5890,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}/hello_world/build",
"remoteRoot": "/var/task"
}
]
}
]
}
我似乎是在按照文档的指导在“python-debugging/hello_world/build”处编辑 python 文件(文档中有一个步骤要求您复制 python文件到“python-debugging/hello_world/build”)。
但是当您 运行 “sam local start-api”时,它实际上 运行 是位于 CloudFormation 模板指定位置的 python 文件(tempalted.yaml),位于“python-debugging/hello_world”(检查“CodeUri”属性)。
当我将所有库移动到与 python 文件相同的文件夹时,它起作用了。
所以我想你必须确定你正在 运行 哪个 python(或 lambda)脚本,并确保库与 python 脚本在一起(如果你没有使用层)。
文件夹结构
在 Visual studio 代码中进入调试模式
第 1 步:调用并启动本地 API 网关
- 服务器
第 2 步:发送测试请求
- 客户
第 3 步:收到请求,触发 lambda,待激活 Visual Studio 代码中的调试模式
- 服务器
第 4 步:触发 Lambda 函数,在 Visual Studio 代码
中进入调试模式
在 IDE 中,打开“运行”透视图,select 此文件的启动配置(“SAM CLI Python Hello World”)。开始调试。
第 5 步:单步执行函数,return 响应
- 服务器
- 客户
我试图在 Visaul Studio Code 中使用 lambda (python) 的调试功能。我按照 AWS Docs 上的说明进行操作,但无法在调试模式下触发 python 应用程序。
请看看你是否知道这个问题,如果我设置不正确,谢谢。
观察
- 开始申请
应用程序似乎没有在指定的调试端口上启动?
- 请求通话
无法到达端点,python 未输入应用程序
如果通过端口 3000 访问,应用程序可以成功完成
已执行设置
- 初始化项目,按照提示安装ptvsd
- 在 python 代码上启用 ptvsd
- 添加启动配置
项目结构
Python 来源
这基本上只是 python
的官方 helloworld 示例import json
# import requests
import ptvsd
# Enable ptvsd on 0.0.0.0 address and on port 5890 that we'll connect later with our IDE
ptvsd.enable_attach(address=('localhost', 5890), redirect_output=True)
ptvsd.wait_for_attach()
def lambda_handler(event, context):
"""Sample pure Lambda function
Parameters
----------
event: dict, required
API Gateway Lambda Proxy Input Format
Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
context: object, required
Lambda Context runtime methods and attributes
Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html
Returns
------
API Gateway Lambda Proxy Output Format: dict
Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
"""
# try:
# ip = requests.get("http://checkip.amazonaws.com/")
# except requests.RequestException as e:
# # Send some context about this error to Lambda Logs
# print(e)
# raise e
return {
"statusCode": 200,
"body": json.dumps({
"message": "hello world",
# "location": ip.text.replace("\n", "")
}),
}
启动配置
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "SAM CLI Python Hello World",
"type": "python",
"request": "attach",
"port": 5890,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}/hello_world/build",
"remoteRoot": "/var/task"
}
]
}
]
}
我似乎是在按照文档的指导在“python-debugging/hello_world/build”处编辑 python 文件(文档中有一个步骤要求您复制 python文件到“python-debugging/hello_world/build”)。
但是当您 运行 “sam local start-api”时,它实际上 运行 是位于 CloudFormation 模板指定位置的 python 文件(tempalted.yaml),位于“python-debugging/hello_world”(检查“CodeUri”属性)。
当我将所有库移动到与 python 文件相同的文件夹时,它起作用了。
所以我想你必须确定你正在 运行 哪个 python(或 lambda)脚本,并确保库与 python 脚本在一起(如果你没有使用层)。
文件夹结构
在 Visual studio 代码中进入调试模式
第 1 步:调用并启动本地 API 网关
- 服务器
第 2 步:发送测试请求
- 客户
第 3 步:收到请求,触发 lambda,待激活 Visual Studio 代码中的调试模式
- 服务器
第 4 步:触发 Lambda 函数,在 Visual Studio 代码
中进入调试模式在 IDE 中,打开“运行”透视图,select 此文件的启动配置(“SAM CLI Python Hello World”)。开始调试。
第 5 步:单步执行函数,return 响应
- 服务器
- 客户