AWS lambda 函数在参数中具有“事件”和“上下文”。“事件”是一个 json 对象。
我尝试将 API(通过 AWS API 网关的管理器)连接到我的 lambda 函数,将事件的 json 作为 http POST 的正文发送。这很失败,我只是有一些迹象表明可能有一个空事件发送到 lambda 函数。
我应该如何通过 API 发送“事件”?
这是我的 lambda 函数的代码:
from __future__ import print_function
import boto3
import json
import time
print('Loading function')
def lambda_handler(event, context):
print("Received event: ")
print(type(event))
print(""+json.dumps(event, indent=2))
id = event['Id']
dynamo = boto3.resource('dynamodb').Table('Table1')
dynamo.put_item(
Item = {
'Button' : int(id),
'Time' : int(time.time()),
})
return {
'statusCode' : '400',
'body' : null,
'headers' : { 'Content-Type': 'application/json', },
}
对 lambda 函数运行测试会得到以下日志:
START RequestId: x Version: $LATEST
Received event:
<type 'dict'>
{
"Id": "1"
}
END RequestId: x
和答案
{
"body": null,
"headers": {
"Content-Type": "application/json"
},
"statusCode": "400"
}
但是通过 API Gateway 测试功能运行它会给出
Tue May 16 15:54:27 UTC 2017 : Endpoint response body before transformations:
{"stackTrace": [["/var/task/lambda_function.py", 12, "lambda_handler",
"id = event['Id']"]], "errorType": "KeyError", "errorMessage": "'Id'"}
Tue May 16 15:54:27 UTC 2017 : Endpoint response headers:
{x-amzn-Remapped-Content-Length=0, x-amzn-RequestId=x,
Connection=keep-alive, Content-Length=153,
X-Amz-Function-Error=Unhandled, Date=Tue, 16 May 2017 15:54:27 GMT,
X-Amzn-Trace-Id=root=x;sampled=0, Content-Type=application/json}
Tue May 16 15:54:27 UTC 2017 : Execution failed due to configuration
error: Malformed Lambda proxy response
Tue May 16 15:54:27 UTC 2017 : Method completed with status: 502