Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if I'm running in AWS Lambda environment?

I want to detect if my code is getting executed in AWS Lambda environment. Is there a good, documented way to do it?

Currently I'm depending on existence of environmental variable LAMBDA_TASK_ROOT which was described in the Exploring The AWS Lambda Runtime Environment blog post which feels wrong.

like image 836
sumek Avatar asked Jul 15 '16 10:07

sumek


People also ask

How do you know if Lambda is running?

Lambda automatically monitors Lambda functions on your behalf and reports metrics through Amazon CloudWatch. To help you monitor your code when it runs, Lambda automatically tracks the number of requests, the invocation duration per request, and the number of requests that result in an error.

Is AWS Lambda always running?

Lambda has a hard timeout of 15 minutes. Therefore it cannot run continuously.

What is AWS Lambda running on?

AWS Lambda natively supports Java, Go, PowerShell, Node. js, C#, Python, and Ruby code, and provides a Runtime API which allows you to use any additional programming languages to author your functions. Please read our documentation on using Node. js, Python, Java, Ruby, C#, Go, and PowerShell.

What is a Lambda execution environment?

Lambda invokes your function in an execution environment, which provides a secure and isolated runtime environment. The execution environment manages the resources required to run your function.


1 Answers

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

like image 129
Joshua Robinson Avatar answered Sep 18 '22 09:09

Joshua Robinson