question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose a `IS_LOCAL` environment variable if running via `sls invoke local`

See original GitHub issue

This is a Feature Proposal

Description

When testing code locally it is sometimes important to know whether the code is ran locally or in the AWS Lambda environment. The three main reasons for us are:

  • Typically we enable long stack traces for Bluebird in our code. However, if our Lambda function is ran via sls invoke local this isn’t possible and crashes with a Error: cannot enable long stack traces after promises have been created (which is caused by Serverless also using Bluebird promises). When testing code locally I have to comment out that section in our code! 😱
  • Our errors are typically reported to Sentry, a third party service for error tracking. We never want to collect errors raised in a local developer environment.
  • We often use a local Redis server when running code locally, as our real services are hidden inside of a VPC.

A possible workaround would be to create a dedicated “local” stage and have specific configuration for that. Certainly an option, but sometimes I’d prefer a way of quickly checking in code if my function runs locally or on AWS. Hence my suggestion: Introduce a new IS_LOCAL or SERVERLESS_INVOKE_LOCAL environment variable that is only set when the code is invoked via sls invoke local. This would be similar to the Serverless Offline Plugin which sets IS_OFFLINE.

This would enable code like this:

if (process.env.IS_LOCAL) {
  console.log("Running locally!");
}

Additional Data

  • Serverless Framework Version you’re using: 1.13.2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
HyperBraincommented, May 18, 2017

@DavidWells Already achieved 😸 . My PR just sets the variable in the common environment setter of the invoke local command. That will affect any AWS invoke local invocation - so to say all runtimes. There are no side effects nor downsides.

0reactions
HyperBraincommented, May 2, 2018

I have also explicitly set BLUEBIRD_LONG_STACK_TRACES=1 in my env vars.

I think you have to enable long stack traces only if not running locally, because serverless invoke local already does that. So, you should put the variable assignment into a condition !IS_LOCAL.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invoke Local - AWS Lambda - Serverless Framework
The invoke local command sets reasonable environment variables for the invoked function. All AWS specific variables are set to values that are quite...
Read more >
Serverless Detect Running Locally - aws lambda
IS_LOCAL will detect if you are running locally. ... If you're using AWS Lambda, it has some built-in environment variables.
Read more >
Black Belt Techniques in Serverless Framework App ...
We've been writing Python applications using Serverless Framework ... For one, you won't be incurring costs if your environment is local.
Read more >
Invoking Lambda functions locally - AWS Documentation
You can invoke your AWS Lambda function locally by using the sam local invoke AWS SAM CLI command and providing the function's logical...
Read more >
Serverless Plugin Deploy Environment - Morioh
This plugin exposes per-stage deployment variables and deployment environment, and allows users to run commands with the environment of a given stage. The ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found