Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I know whether it is running inside lambda?

I am deploying nodejs code to AWS lambda and I'd like to know how I can check whether it is running in lambda. Because I need to do something different in code between lambda and local.

like image 838
Joey Yi Zhao Avatar asked Jun 25 '26 02:06

Joey Yi Zhao


2 Answers

AWS Lambda sets various runtime environment variables that you can leverage. You can use the following in Node.js, for example:

const isLambda = !!process.env.LAMBDA_TASK_ROOT;
console.log("Running on Lambda:", isLambda);

Note that the double bang !! converts a truthy/falsey object to a boolean (true/false).

like image 56
jarmod Avatar answered Jun 27 '26 17:06

jarmod


I'd advise using a Lambda environment variable rather than attempting to check against any runtimes of the Lambda executing.

By doing this you can ensure that any infrastructure changes on the AWS side of Lambda will not affect your code.

It also allows you test it locally if you are trying to reproduce a scenario without the need to hardcode logic.

like image 40
Chris Williams Avatar answered Jun 27 '26 15:06

Chris Williams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!