Serverless deployed lambda callbacks extremely slow

All my lambdas with callbacks deployed by serverless when invoked have durations around 5960ms. (Lambda timeout set to 6 seconds… peculiar)
The same code written in the AWS lambda inline editor, has the normal duration 100ms etc.

This is only if module.exports.handler has a callback and is not async.
So I’m just converting my handler functions to async which I should have already done.

Has anyone else experienced this? Not too worried about getting this resolved, just curious.

Chur

Would have to see the handler code, but what appears to be the most common cause is failure to call the callback at the end of the function.

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({ region: "us-east-1",napiVersion: "2012-08-10"});

exports.handler = (event, context, callback) => {
  const userToMarkInactive = event.userToMarkInactive;
  const params = {
    ExpressionAttributeNames: {
      "#AT": "active"
    },
    ExpressionAttributeValues: {
      ":t": {
        BOOL: false
      }
    },
    Key: {
      Username: {
        S: "" + userToMarkInactive
      }
    },
    ReturnValues: "ALL_NEW",
    TableName: process.env.UsersTable,
    UpdateExpression: "SET #AT = :t"
  };
  dynamodb.updateItem(params, function(err, data) {
    if (err) {
      console.log(err);
      callback(err);
    } else {
      console.log(data);
      callback(null, data);
    }
  });
};

Hmm… I don’t see any issues.
To confirm I went ahead and whipped up a simple service with your code and it’s returning as expected:

First invocation:
REPORT RequestId: 4b88f946-3c65-4ba6-aaca-8a27bff70320 Duration: 128.67 ms Billed Duration: 200 ms Memory Size: 1024 MB Max Memory Used: 77 MB Init Duration: 205.88 ms

Subsequent invocations:

REPORT RequestId: 95f041dd-6a19-40c3-9e65-d1153c8b422a  Duration: 27.90 ms      Billed Duration: 100 ms Memory Size: 1024 MB    Max Memory Used: 77 MB
REPORT RequestId: 4d607472-2e11-40ec-8ce7-bf6423f95411  Duration: 27.52 ms      Billed Duration: 100 ms Memory Size: 1024 MB    Max Memory Used: 77 MB
REPORT RequestId: d0348c35-c8f3-4fad-a071-409c4e030b9b  Duration: 27.25 ms      Billed Duration: 100 ms Memory Size: 1024 MB    Max Memory Used: 77 MB

Invocation command:
sls invoke -s brett -f hello -d '{"userToMarkInactive":"joe"}' -l

Here’s my serverless.yml:

service: simple

plugins:
  - serverless-pseudo-parameters

provider:
  name: aws
  runtime: nodejs8.10
  stage: brett
  region: ${opt:region, 'us-west-2'}

  environment:
    UsersTable: ${self:provider.stage}-users

  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:*
      Resource:
        - arn:aws:dynamodb:#{AWS::Region}:#{AWS::AccountId}:table/${self:provider.stage}-users

functions:
  hello:
    handler: handler.handler

resources:
  Resources:
    UserTable:
      Type: AWS::DynamoDB::Table
      DeletionPolicy: Delete
      Properties:
        AttributeDefinitions:
          - AttributeName: Username
            AttributeType: S
        BillingMode: PAY_PER_REQUEST
        KeySchema:
          - AttributeName: Username
            KeyType: HASH
        TableName: ${self:provider.stage}-users

and the handler function:

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({ region: "us-west-2", napiVersion: "2012-08-10"});

exports.handler = (event, context, callback) => {
  const userToMarkInactive = event.userToMarkInactive;
  const params = {
    ExpressionAttributeNames: {
      "#AT": "active"
    },
    ExpressionAttributeValues: {
      ":t": {
        BOOL: false
      }
    },
    Key: {
      Username: {
        S: "" + userToMarkInactive
      }
    },
    ReturnValues: "ALL_NEW",
    TableName: process.env.UsersTable,
    UpdateExpression: "SET #AT = :t"
  };
  dynamodb.updateItem(params, function(err, data) {
    if (err) {
      console.log(err);
      callback(err);
    } else {
      console.log(data);
      callback(null, data);
    }
  })
}

Built and deployed with Serverless 1.53.0