gpt4 book ai didi

python-3.x - 由于客户函数错误,Lambda 执行失败,状态为 200

转载 作者:行者123 更新时间:2023-12-04 13:52:43 31 4
gpt4 key购买 nike

我正在创建一个简单的 Lambda 函数来在 AWS Gateway 中提供 POST 服务:

import json
import boto3
from datetime import datetime


def lambda_handler(event,context):
wf = event['WF']

if wf == 'start1':
body = 'Suceeded'
return {
"isBase64Encoded": False,
'statusCode': 200,
'body': json.dumps(body),
'headers': {
'Content-Type': 'application/json'
}
}
else:
body = 'Failed'
return {
"isBase64Encoded": False,
'statusCode': 400,
'body': json.dumps(body),
'headers': {
'Content-Type': 'application/json'
}
}
如果我发送带有此正文的 POST 成功:
{
"WF": "start1",
"Context": "OK"
}
返回:
Test Event Name
test

Response
{
"isBase64Encoded": false,
"statusCode": 200,
"body": "\"Suceeded\"",
"headers": {
"Content-Type": "application/json"
}
}

Function Logs
START RequestId: 91663a9d-d8b6-4aad-b8b6-eb2f15ff0354 Version: $LATEST
END RequestId: 91663a9d-d8b6-4aad-b8b6-eb2f15ff0354
REPORT RequestId: 91663a9d-d8b6-4aad-b8b6-eb2f15ff0354 Duration: 1.74 ms Billed Duration: 2 ms Memory Size: 128 MB Max Memory Used: 66 MB Init Duration: 300.73 ms

Request ID
91663a9d-d8b6-4aad-b8b6-eb2f15ff0354
但是如果我在 AWS 的 API Gateway 服务中运行测试,我会得到:
Fri Jun 04 13:45:22 UTC 2021 : Sending request to https://XXXXXXX
Fri Jun 04 13:45:22 UTC 2021 : Received response. Status: 200, Integration latency: 15 ms
Fri Jun 04 13:45:22 UTC 2021 : Endpoint response headers: {Date=Fri, 04 Jun 2021 13:45:22 GMT, Content-Type=application/json, Content-Length=159, Connection=keep-alive, x-amzn-RequestId=d479fdac-6737-42fa-96a3-9e991056b48d, X-Amz-Function-Error=Unhandled, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-60ba2e72-ff62f051febbd21d6cc9d114;sampled=0}
Fri Jun 04 13:45:22 UTC 2021 : Endpoint response body before transformations: {"errorMessage": "'WF'", "errorType": "KeyError", "stackTrace": [" File \"/var/task/lambda_function.py\", line 7, in lambda_handler\n wf = event['WF']\n"]}
Fri Jun 04 13:45:22 UTC 2021 : Lambda execution failed with status 200 due to customer function error: 'WF'. Lambda request id: d479fdac-6737-42fa-96a3-9e991056b48d
Fri Jun 04 13:45:22 UTC 2021 : Method completed with status: 502
为什么第 7 行是错误的?
有没有另一种方法来解析 POST 的正文?

最佳答案

看起来您的 API 网关调用没有填充 event Lambda 函数的参数。当您在 lambda 函数的页面上创建测试事件时,您可以创建自己的测试事件。 API Gateway 不会使用此测试事件,而是会发送一个空事件(可能为 null)。
IE。


import json
import boto3
from datetime import datetime


def lambda_handler(event,context): # Event is null here
wf = event['WF'] # Attempting to read the attribute 'WF' of null results in an error

关于python-3.x - 由于客户函数错误,Lambda 执行失败,状态为 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67838536/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com