使用 aws sam cli 构建和部署 lambda 函数后,我在日志中看到客户端网络套接字已断开连接

After building and deploying lambda function with aws sam cli, I get Client network socket disconnected in logs

更新:最初我使用了正确的回调(null,响应)但我还需要为新项目再次执行 sam init,使用 nvm 安装不同的节点版本。

我有以下 lambda 函数,它使用那里 api 在 datocms 中创建一个新项目,我使用 aws sam cli 让它在本地工作。

我将其设置为接收 get 或 post 请求,无论哪种方式,它都会将虚拟内容发送给 dato。在 Get 请求中,如果我在浏览器中查看它,我会得到我编写的消息测试,但是如果我 sam buildsam deploy 它说一切都已正确部署在我的控制台中,但是当我去访问时aws 控制台中 lambda 中的 http get url。我收到消息内部服务器错误。

这是我的函数

// const axios = require('axios')
// const url = 'http://checkip.amazonaws.com/';
const SiteClient = require('datocms-client').SiteClient;
const client = new SiteClient('codegoeshere');

let response;

/**
 *
 * Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
 * @param {Object} event - API Gateway Lambda Proxy Input Format
 *
 * Context doc: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html 
 * @param {Object} context
 *
 * Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
 * @returns {Object} object - API Gateway Lambda Proxy Output Format
 * 
 */
exports.lambdaHandler = async (event, context, callback) => {
    //console.log(JSON.parse(event.body).data.object.amount);
    console.log(SiteClient);
    client.items.create({
        title: 'Post Request From Stripe',
        itemType: '223937'
      })
      .then((item) => {
        console.log(item);
      })
      .catch((error) => {
        console.error(error);
      });

    try {
        // const ret = await axios(url);
        response = {
            'statusCode': 200,
            "body" : {"message":"test"}
        }
    } catch (err) {
        console.log(err);
        return err;
    }

    callback(null, response);
};

这是我的 template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  test

  Sample SAM Template for test

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello-world/
      Handler: app.lambdaHandler
      Runtime: nodejs12.x
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get
        HelloWorld1:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: post

Outputs:
  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
  # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
  HelloWorldApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn


这是我的日志,你可以看到它正在控制台记录 siteClient 变量并且它正在返回一个函数,所以看起来 lamda 正在获取 运行,但 client.items.create 没有get 运行 并且函数没有返回任何内容。如果数据调用失败,它应该控制台日志,我应该看到,所以我不确定发生了什么。

START RequestId: 060bef82-7b0d-46d0-82c9-515e41ff8945 Version: $LATEST
2020-04-24T15:08:27.414Z    060bef82-7b0d-46d0-82c9-515e41ff8945    INFO    [Function: Client]
END RequestId: 060bef82-7b0d-46d0-82c9-515e41ff8945
REPORT RequestId: 060bef82-7b0d-46d0-82c9-515e41ff8945  Duration: 3001.87 ms    Billed Duration: 3000 ms    Memory Size: 128 MB Max Memory Used: 104 MB Init Duration: 572.60 ms    
2020-04-24T15:08:30.396Z 060bef82-7b0d-46d0-82c9-515e41ff8945 Task timed out after 3.00 seconds

更新:

所以我可以做一个新的 sam init,这次我做的不同是当我在 lambda 中安装包时确保我在本地安装了节点 12,并删除了回调并返回。该消息现在有效。我现在遇到的唯一错误是

request to https://site-api.datocms.com/docs/site-api-hyperschema.json failed, reason: Client network socket disconnected before secure TLS connection was established

我记得在 aws lambda 上看过一个关于时间设置的视频,但不确定是什么,以及它是否适用于此。

我在新的 sam init 项目中使用 return response 而不是回调,所以我认为这与断开连接有关

我的问题的主要解决方案是 运行 sam init 再次安装节点版本 12