如何在Visual Studio代码上使用Python设置AWS SAM本地调试?

2024-05-20 20:20:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Visaul Studio代码中使用lambda(python)的调试函数。我在遵循AWS文档上的说明,但是我无法在调试模式下触发python应用程序

请看看你是否知道这个问题,如果我有任何设置错误,谢谢

参考: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-debugging.html

观察

  • 启动应用程序

应用程序似乎未在指定的调试端口上启动

Start_server_with_debug_mode

  • 请求呼叫

无法访问终结点,并且未输入python应用程序

Not_ok_with_port_5890

如果通过端口3000访问,应用程序可以成功完成

OK_with_port_3000

执行的设置

  1. 按照说明初始化项目并安装ptvsd
  2. 在python代码上启用ptvsd
  3. 添加启动配置

项目结构

Project_Structure

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"
            }
        ]
    }
    ]
}

Tags: lambdahttpsimportcomawsjson应用程序docs
1条回答
网友
1楼 · 发布于 2024-05-20 20:20:40

我似乎是在“python调试/hello_-world/build”中按照文档的指导方针编辑python文件(文档中有一个步骤要求您将python文件复制到“python调试/hello_-world/build”)

但是,当您运行“sam local start api”时,它实际上会在CloudFormation模板(tempalted.yaml)指定的位置运行python文件,该模板位于“python调试/hello_world”(检查“CodeUri”属性)

当我将所有库文件移动到与python文件相同的文件夹中时,它就工作了

因此,我假设您必须确保正在运行哪个python(或lambda)脚本,并确保库与python脚本一起运行(如果您不使用层)

文件夹结构

Folder_Structure

在Visual studio代码中进入调试模式

步骤1:调用并启动本地API网关

  • 服务器

Start_API_Gateway

步骤2:发送测试请求

  • 客户

Test_Request

步骤3:收到请求,lambda已触发,在Visual Studio代码中挂起激活调试模式

  • 服务器

Lambda_Triggered

步骤4:触发Lambda函数,在Visual Studio代码中进入调试模式

在IDE中,打开“运行”透视图,选择此文件的启动配置(“SAM CLI Python Hello World”)。启动调试

Entering_Debug_Mode

步骤5:逐步完成函数,返回响应

  • 服务器

Lambda_Completed

  • 客户

enter image description here

相关问题 更多 >