查询端点时出现内部服务器错误

时间:2020-02-07 13:17:55

标签: amazon-web-services aws-lambda aws-api-gateway

我有一个非常简单的lambda函数,是在aws中创建的。请参见下面。

import json

print('Loading function')

def lambda_handler(event, context):
    #1. Parse out query string params
    userChestSize = event['userChestSize']

    print('userChestSize= ' + userChestSize)

    #2. Construct the body of the response object
    transactionResponse = {}
    transactionResponse['userChestSize'] = userChestSize
    transactionResponse['message'] = 'Hello from Lambda'

    #3. Construct http response object
    responseObject = {}
    responseObject['statusCode'] = 200
    responseObject['headers'] = {}
    responseObject['headers']['Content-Type'] = 'application/json'
    responseObject['body'] = json.dumps(transactionResponse)

    #4. Return the response object
    return responseObject

然后我用GET方法创建了一个简单的api。它为我生成了一个端点链接以测试我的lambda。所以当我使用链接https://abcdefgh.execute-api.us-east-2.amazonaws.com/TestStage?userChestSize=30

我知道

{“消息”:“内部服务器错误”}

Cloud Log出现以下错误

'userChestSize': KeyError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 7, in lambda_handler
    userChestSize = event['userChestSize']
KeyError: 'userChestSize'

我在做什么错?我按照基本说明创建了lambda和api网关。

1 个答案:

答案 0 :(得分:1)

date不存在。我建议记录整个event['userChestSize']对象,以便您可以查看事件中的实际情况。