Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Python app is running within AWS lambda function?

I've got a Python application that connects to a database and I would like the db credentials to be different when it's running in local env (for testing) or within a lambda function (for production).

Is there any way, from the Python app, to detect that it is running inside the lambda function?

like image 633
laurent Avatar asked Mar 29 '16 14:03

laurent


People also ask

How do you know if a Lambda function 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.


1 Answers

This works for me

os.environ.get("AWS_EXECUTION_ENV") is not None

EDIT: I find the existence of the context object insufficient for such a check because you might be mocking it when not running within an AWS lambda function. Then again, you may be mocking the AWS_EXECUTION_ENV as well ...

like image 91
Nikolay Manolov Avatar answered Sep 29 '22 09:09

Nikolay Manolov