【问题标题】:Serverless 502 Bad Gateway无服务器 502 错误网关
【发布时间】:2019-06-07 09:52:24
【问题描述】:

我能够按照文档创建一个简单的无服务器函数,但是当我添加 http 侦听器时,我在尝试访问我的端点时不断收到 502 Bad Gateway

我该如何调试?

'use strict';

module.exports.hello = async (event, context) => {
  return {
    statusCode: 200,
    body: {
      message: 'Go Serverless v1.0! Your function executed successfully!',
      input: event,
    },
  };
};

serverless.yaml

service: playing-with-serverless # NOTE: update this with your service name

provider:
  name: aws
  runtime: nodejs8.10

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: hello
          method: get

我已经部署了我的函数

 $ sls deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (423 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..............
Serverless: Stack update finished...
Service Information
service: playing-with-serverless
stage: dev
region: us-east-1
stack: playing-with-serverless-dev
api keys:
  None
endpoints:
  GET - https://1r5mt9wa63.execute-api.us-east-1.amazonaws.com/dev/hello
functions:
  hello: playing-with-serverless-dev-hello
layers:
  None
Serverless: Removing old service artifacts from S3...

卷曲

$ curl --request GET \
  --url https://1r5mt9wa63.execute-api.us-east-1.amazonaws.com/dev/hello
{"message": "Internal server error"}%```  

【问题讨论】:

    标签: node.js amazon-web-services aws-lambda serverless


    【解决方案1】:

    您需要在响应对象中将body 字符串化:

    return {
      statusCode: 200,
      body: JSON.stringify({
        message: 'Go Serverless v1.0! Your function executed successfully!',
        input: event
      })
    };
    

    See docs 特别是Output Format of a Lambda Function for Proxy Integration

    Set up an Integration Response in API Gateway

    【讨论】:

    • 我在文档的那个部分中没有看到任何关于必须对正文进行字符串化的内容。你能引用确切的短语吗?你的建议确实解决了我的问题。我的另一个问题是网上的许多例子似乎有第三个参数callback。无服务器生成的代码示例没有这个分支。无服务器是否抽象了对这个 callback 参数的需求,只允许你返回一些东西?
    • 集成响应的文档是here。您不需要在 Lambda 中使用回调,因为您使用的是 Nodejs 8.1。您可以返回响应或抛出异常。但在 API 集成的情况下,您不会返回带有正确状态码的响应。如果使用低于 8 的 Nodejs 版本,则需要使用回调。
    【解决方案2】:

    您的服务器端代码可能有错误。

    尝试在 AWS 控制台上测试您的请求。如果有错误,它将准确指出您的 Javascript 代码中的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-20
      • 2016-07-29
      • 2019-12-01
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多