[英]Lambda function works, but fails when invoked through the API gateway
I have a lambda function that puts items in a dynamodb table.我有一个 lambda function 将项目放在 dynamodb 表中。 It works perfectly fine with test events.它与测试事件完美配合。
import boto3
class DBupload:
def __init__(self):
client = boto3.resource('dynamodb')
self.table=client.Table('labels')
def Create_data(self,event):
response=self.table.put_item(
Item={
'instanceID' : event['id'],
'imageID' : event['imageid'],
'labels' : event['labels']
}
)
return{
'statusCode' : response['ResponseMetadata']['HTTPStatusCode'],
'body' : 'Record ' + event['id'] + ' added'
}
def lambda_handler(event, context):
if event:
instance = DBupload()
if event['tasktype'] == "create":
create_result = instance.Create_data(event['data'])
return create_result
else :
return {
'statusCode': '404',
'body': 'Not found'
}
I make a REST API gateway, create a resource, and a POST method, with proxy, and I enable IAM and API key, before deploying it.我制作了一个 REST API 网关,创建了一个资源和一个带有代理的 POST 方法,并在部署之前启用了 IAM 和 API 密钥。 I then go to usage plans, and add the stage to the usage plan, then deploy once more for good measure.然后我将 go 添加到使用计划中,并将该阶段添加到使用计划中,然后再次部署以进行良好测量。
When I send the exact same request now, through Postman, it throws an Internal Server Error.当我现在通过 Postman 发送完全相同的请求时,它会抛出一个内部服务器错误。 I can send it through the API Gateway testing function which bypasses auth, and it gives me the following error log我可以通过绕过身份验证的 API 网关测试 function 发送它,它给了我以下错误日志
Lambda execution failed with status 200 due to customer function error: 'tasktype'. Lambda 由于客户 function 错误:“任务类型”,执行失败,状态为 200。 Method completed with status: 502方法完成状态:502
So it looks like it is NOT an API error because the error log references the customer function in lambda. But the Lambda function works perfectly fine when I send my item in as a test-event through lambda. What gives?所以看起来它不是 API 错误,因为错误日志引用了 lambda 中的客户 function。但是当我通过 88355034.71388 将我的项目作为测试事件发送时,Lambda function 工作得很好
Here is the test event btw这是测试事件顺便说一句
{
"tasktype":"create",
"data":{
"id":"testinsanceid",
"imageid":"testimageid",
"labels":{
"labeltype1":"labelname1",
"labeltype2":"labelname2"
}
}
} }
Try尝试
if 'tasktype' in event and event['tasktype'] == "create":
This will ensure you're not taking a dependency on something that doesn't exist.这将确保您不会依赖不存在的东西。
Also print(event)
will allow you to ensure the event object is what you expect.此外print(event)
将允许您确保事件 object 是您所期望的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.