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.
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.
Lambda has a hard timeout of 15 minutes. Therefore it cannot run continuously.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With