简体   繁体   中英

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

Update: Originally I used the correct callback(null, response) but I needed to also do sam init again for a new project, install a different node version with nvm.

I have the following lambda function that creates a new item in datocms using there api, I got it to work locally using aws sam cli.

I set it up to recieve either get or post requests, either way it would send the dummy content to dato. In the Get request if I view it in the browser I get the messsage test that I wrote, however if I sam build and sam deploy it says everything has deployed properly in my console, however when I go to visit the http get url in the lambda in aws console. I get the message internal server error.

Here is my function

// 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);
};

Here is my 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


This is my logs, you can see that it is console logging the siteClient variable and it is returning a function, so it seems like that lamda is getting run, but the client.items.create doesnt get run and nothing is returned from the function. If the data call was failing it should console log and I should see that, so I am not sure what is going on.

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

UPDATE:

So I was able to just do a new sam init, and this time what I did different is I made sure I had node 12 installed locally when I installed packages in the lambda, and removed the callback and just returned. The message is working now. The only error I am getting now is this

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

I remember watching a video on aws lambda something about a time setting but not sure what, and if it applies to this.

I was using return response instead of callback with the new sam init project so I think that had something to do with the disconnection

The main solution to my problem was running sam init again, installing node version 12

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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