【问题标题】:Malformed Lambda proxy response: deploying python endpoints through serverless畸形的 Lambda 代理响应:通过无服务器部署 python 端点
【发布时间】:2018-03-16 14:46:41
【问题描述】:

我正在尝试通过 Lambda 函数(通过无服务器构建)发回 JSON 响应,但它出错了。

我正在创建响应:

response = {}
response["file_name"] = file_name
response["status"] = status
response["description"] = message
response["data"] = data 
return {
    "isBase64Encoded": False,
    "statusCode": 200,
    "headers": {
        "Content-Type": "application/json",
    },
    "body": json.dumps(response)
}

但 AWS 只是返回:

{
    "message": "Internal server error"
}

我正在打印该响应对象,它是:

{'isBase64Encoded': False, 'statusCode': 200, 'headers': {'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json'}, 'body': '{"file_name": "upload-4660557513950187006.csv", "status": "success", "description": "Success.", "data": ""}'}

当我通过 API Gateway 调用时,这是我得到的日志:

Thu Oct 05 16:21:15 UTC 2017 : Endpoint request body after transformations: {"resource":"/parse","path":"/parse","httpMethod":"POST","headers":null,"queryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{"path":"/parse","accountId":"317910044022","resourceId":"xik9xe","stage":"test-invoke-stage","requestId":"test-invoke-request","identity":{"cognitoIdentityPoolId":null,"accountId":"317910044022","cognitoIdentityId":null,"caller":"AIDAJGNM4CLIWDKHDDV2U","apiKey":"test-invoke-api-key","sourceIp":"test-invoke-source-ip","accessKey":"ASIAJHGYEQDHKLLRWL6A","cognitoAuthenticationType":null,"cognitoAuthenticationProvider":null,"userArn":"arn:aws:iam::317910044022:user/chase","userAgent":"Apache-HttpClient/4.5.x (Java/1.8.0_131)","user":"AIDAJGNM4CLIWDKHDDV2U"},"resourcePath":"/parse","httpMethod":"POST","apiId":"3gvwsa0cj2"},"body":"{\n\t\"file_name\": \"upload-4660557513950187006.csv\",\n\t\"vendor\": \"\"\n}","isBase64Encoded":false}
Thu Oct 05 16:21:15 UTC 2017 : Sending request to https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:317910044022:function:PyParsingService-dev-parse/invocations
Thu Oct 05 16:21:16 UTC 2017 : Received response. Integration latency: 416 ms
Thu Oct 05 16:21:16 UTC 2017 : Endpoint response body before transformations: "{\"isBase64Encoded\": false, \"statusCode\": 200, \"headers\": {\"Access-Control-Allow-Origin\": \"*\", \"Content-Type\": \"application/json\"}, \"body\": \"{\\\"file_name\\\": \\\"upload-4660557513950187006.csv\\\", \\\"status\\\": \\\"success\\\", \\\"description\\\": \\\"Success.\\\", \\\"data\\\": \\\"\\\"}\"}"
Thu Oct 05 16:21:16 UTC 2017 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, x-amzn-RequestId=36059915-a9e9-11e7-8444-438990db7407, Connection=keep-alive, Content-Length=317, Date=Thu, 05 Oct 2017 16:21:15 GMT, X-Amzn-Trace-Id=root=1-59d65bfb-7000702549081b6682b894a9;sampled=0, Content-Type=application/json}
Thu Oct 05 16:21:16 UTC 2017 : Execution failed due to configuration error: Malformed Lambda proxy response
Thu Oct 05 16:21:16 UTC 2017 : Method completed with status: 502

对 Python 不是很有经验 - 任何帮助将不胜感激。根据this,需要为 API 网关正确格式化响应。不知道为什么我的响应不符合 API Gateway 的要求。

【问题讨论】:

  • 这可能不是 JSON 的问题。您可以尝试使用空响应调用 Lambda 吗?只是为了确定是因为权限问题
  • 您的 Cloudwatch 日志告诉您什么?你的实际代码是这样的吗?如果是这样,那么您的 Python 代码由于缩进不正确而出现语法错误。
  • @dashmug 我已将日志包含在原始帖子中。我收到“格式错误的 Lambda 代理响应”错误。
  • @Chase 您使用的是 Lambda 代理集成类型吗?如果您通过 HTTP 请求(不使用 API Gateway 控制台)调用 CloudWatch,会出现错误吗?

标签: python amazon-web-services aws-lambda aws-api-gateway serverless-framework


【解决方案1】:

我发现我做错了什么。 Execution failed due to configuration error: Malformed Lambda proxy response 错误引用了 Lambda 试图通过 API Gateway 发回的响应。无服务器自动使用 Lambda 代理集成。当我查看响应时,我注意到在基本响应级别(例如,"{\"isBase64Encoded\": false, \"statusCode\": 200, ...})有额外的转义,我认为这会触发错误。果然,当我深入挖掘时,我意识到我在创建响应后正在转储 JSON (json.dumps()),这引发了错误。

感谢您的回复。是我的愚蠢错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-22
    • 2021-07-09
    • 2020-01-29
    • 2019-03-02
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2019-03-06
    相关资源
    最近更新 更多