简体   繁体   English

Lambda function 有效,但通过 API 网关调用时失败

[英]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.

相关问题 Dynamodb.put 在运行 lambda 测试配置时工作正常,但在通过 API 网关调用 lambda 时却不行 - Dynamodb.put works fine when running a lambda test configuration, but not when invoking the lambda through API gateway 目标仅在通过 AWS CLI 调用 Lambda 时有效 - Destination only works when Lambda is invoked through AWS CLI 当 lambda 失败时,AWS API 网关 401 未授权 - AWS API Gateway 401 Unauthorized when lambda fails 为什么在通过 API 网关调用时,Java 中的 AWS Lambda 代码返回“内部服务器错误”? - why does this AWS Lambda code in Java return "internal server error" when invoked via an API gateway? Serverless API Gateway/Lambda 下载失败 PDF - Serverless API Gateway/Lambda fails to download PDF 如何处理从 api 网关调用的 lambda 重试? - How to handle lambda retries invoked from api gateway? 通过API网关触发Lambda的特定功能 - Triggering specific functions of a Lambda through API Gateway 使用标头调用时无法访问 lambda function - Not able to access lambda function when invoked with headers 用CDK中的API网关直接流量到Lambda function - Direct traffic to Lambda function with API Gateway in CDK 如何通过API Gateway将Cognito User Pool信息传递给Lambda - How to pass Cognito User Pool information to Lambda through API Gateway
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM