简体   繁体   English

AWS Lambda和API Gateway Endpoint响应中的调试错误

[英]Debugging error in AWS Lambda and API Gateway Endpoint response

I have a lambda function that is wired up to work with API Gateway. 我有一个lambda函数,它连接到API网关。 When I run it locally it works, when I simulate it with serverless offline it works. 当我在本地运行它时,它工作,当我用serverless offline模拟它时,它工作。

But against the published dev endpoint, it returns an error 500 and a json object with an error message: 但是对于已发布的dev端点,它会返回错误500和带有错误消息的json对象:

{
  "message": "Internal server error"
}

In the CloudWatch logs, there are no errors. 在CloudWatch日志中,没有错误。 In the callstack in the API gateway endpoint testing page, I see my correct response body in the Lambda portion of the process, and then when it gets to the API gateway response step, something goes wrong. 在API网关端点测试页面的callstack中,我在进程的Lambda部分看到了正确的响应主体,然后当它到达API网关响应步骤时出现问题。

How should I debug API Gateway when there aren't any errors in logs? 当日志中没有任何错误时,我应该如何调试API网关?

If you're writing it in node, it could be that your node version is different than 4.3.2 ( the one that AWS Lambda uses ) and that's why testing offline and locally works but not in the AWS Lambda. 如果您在节点中编写它,可能是您的节点版本不同于4.3.2( AWS Lambda使用的那个),这就是为什么离线和本地测试工作但不在AWS Lambda中的原因。

This happened to me before, when I was using let . 在我使用let之前,这发生在我身上。

You could use nvm to install node v 4.3.2 to test it locally or you could look here to see what features are you allowed to use in your code. 您可以使用nvm来安装node v 4.3.2以在本地测试它,或者您可以在这里查看您可以在代码中使用哪些功能。

To get API Gateway feeding cloudWatch : 要获得API网关提供cloudWatch

  1. create a new role with AmazonAPIGatewayPushToCloudWatchLogs in IAM IAM中使用AmazonAPIGatewayPushToCloudWatchLogs创建一个新角色
  2. copy the role ARN arn:aws:iam::<account id>:role/<role name> 复制角色ARN arn:aws:iam::<account id>:role/<role name>
  3. paste it in field : API Gateway > Settings > CloudWatch log role ARN 将其粘贴到字段中: API网关 > 设置 > CloudWatch日志角色ARN
  4. Go to APIs > <API name> > Stages > Settings > CloudWatch Settings and : 转到API > <API name> >> 阶段 > 设置 > CloudWatch设置和:
    • check Enable CloudWatch Logs 选中启用CloudWatch Logs
    • choose Log Level (INFO to see everything) 选择日志级别 (INFO以查看所有内容)
    • check Log full requests/responses data (dump the entire initial request and response into the log) 检查记录完整的请求/响应数据 (将整个初始请求和响应转储到日志中)
    • check Enable CloudWatch Metrics (to see Graph in CloudWatch) 选中启用CloudWatch指标 (查看CloudWatch中的图表)

Now your logs can be viewed in : 现在可以在以下位置查看您的日志:

  • CloudWatch > Logs CloudWatch > 日志

under a name like API-Gateway-Execution-Logs_<api id>/<api stage> API-Gateway-Execution-Logs_<api id>/<api stage>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM