How to detect if I'm running in AWS Lambda environment?
There is a process.env
property that you can check:
const isLambda = !!process.env.LAMBDA_TASK_ROOT;
if (isLambda) {
// You're on AWS Lambda
} else {
// Local or elsewhere
}
Credit to watson/is-lambda for the discovery.
Edit: Official AWS source (with more env vars) https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html
The best way to handle this is to track it yourself. In particular, you can set a global variable or environment variable after the lambda entry point. For example, in node.js:
exports.handler = function(event, context, callback) {
process.env['LAMBDA_ENV'] = 'true';
...
};
If you want to detect whether or not your code is running in local Lambda (aws-sam-cli) or real Lambda, there's the AWS_SAM_LOCAL
environment variable.
function isRunningLocalLambda() {
return process.env.AWS_SAM_LOCAL === 'true';
}