Expose a `IS_LOCAL` environment variable if running via `sls invoke local`
See original GitHub issueThis 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 aError: 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:
- Created 6 years ago
- Reactions:2
- Comments:5 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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.
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
.