Sls lambda to invoke *local* version of lambda, not aws hosted

I’ve been struggling with this - how to tell serverless that a call

lambda.invoke(params)

refers to a local version of the lambda instead of the amazon hosted one, when testing functions locally?

My code below for illustration - it’s simple enough, just that it calls the aws hosted lambda instead of the local one!

var AWS = require('aws-sdk');
AWS.config.region = 'us-east-1';
var lambda = new AWS.Lambda();

var params = {
  FunctionName: 'SomeName',
  InvocationType: 'Event',
  Payload: JSON.stringify(event)
};

lambda.invoke(params);

Thanks for any hints!

I just use a local environment variable export NODE_ENV=local or add it to your .bash_profile for a more permanent solution. Then check it in your code:

if (process.env.NODE_ENV === 'local')...

It would be nice though if there was a built-in env variable in serverless to check against when running invoke local.

I think this is what localstack is aimed at dealing with.
You’d change the endpoint your lambda object points to (to point locally) and then run your SDK commands as normal.

I haven’t tested this, so can’t comment on how effective it is. I would generally think that this is an anti-pattern, because it’s going to greatly complicate your application, and you can never be 100% sure it matches the “real world” AWS API.

I’m also looking for this solution. Does sls offer no way of doing a local lambda.invoke?
@mnort9 I’m not sure I understand you solution. Is there an api for local testing, without using localstack?

As of v1.14, serverless provides a IS_LOCAL env variable when running sls invoke local