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

kkbh8khc  于 2023-01-27  发布在  Python
关注(0)|答案(3)|浏览(160)

我正在创建一个简单的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的主体?

sshcrbum

sshcrbum1#

已通过在POST -集成请求中禁用Lambda代理集成找到解决方案。

slmsl1lt

slmsl1lt2#

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

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
wj8zmpe1

wj8zmpe13#

在我的例子中,我在通过API网关调用Lambda函数时收到了这个错误,当直接调用期望简单JSON主体的lambda函数时也完全没有问题。
我的预期载荷:{"Email": "foo3@foo.com"}
原来配置API网关中的Lambda Proxy Integration in the POST在传递到Lambda之前更改了负载结构,我得到了缺少Key属性Email的错误。我注意到cloudwatch中的Endpoint request body after transformations:行,所以我深入研究了它更改请求正文的原因。

调试方式:

相关问题