Amazon web services 查询终结点时出现内部服务器错误
我在aws中创建了一个非常简单的lambda函数。请看下面Amazon web services 查询终结点时出现内部服务器错误,amazon-web-services,aws-lambda,aws-api-gateway,Amazon Web Services,Aws Lambda,Aws Api Gateway,我在aws中创建了一个非常简单的lambda函数。请看下面 import json print('Loading function') def lambda_handler(event, context): #1. Parse out query string params userChestSize = event['userChestSize'] print('userChestSize= ' + userChestSize) #2. Construc
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
我明白了
{“消息”:“内部服务器错误”}
云日志有以下错误
'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网关。
事件['userChestSize']
不存在。我建议记录整个事件
对象,以便查看事件中的实际内容 “内部服务器错误”通常意味着您需要检查服务器日志以查看实际错误。请在CloudWatch日志中检查函数的输出。@MarkB我刚刚用CloudWatch日志更新了这个问题,你是说打印[事件]?对不起,如果我的问题是愚蠢的是的,做一个打印(事件)看看里面有什么it@Dinero是的,只需进行一些基本调试,即可查看事件中的内容。Python告诉您事件中没有名为userChestSize
的键。可能是event['params']['userChestSize']
或其他什么,只要打印出事件对象就可以告诉您需要做什么。我在哪里可以得到所有可能的事件类型的列表,如params、queryString等我的意思是它可能是event['anything']您可以在文档中看到一些示例,但是记录事件将向您展示它是什么,这是查看需要更改代码以处理什么的最简单方法。