代码之家  ›  专栏  ›  技术社区  ›  Catfish

无服务器502坏网关

  •  0
  • Catfish  · 技术社区  · 7 年前

    我可以按照文档创建一个简单的无服务器函数,但是当我添加 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,
        },
      };
    };
    

    无服务器的

    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"}%```  
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   A.Khan    7 年前

    你需要穿线 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

    推荐文章