Search code examples
pythonamazon-web-servicesenvironmentaws-lambda

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?


Solution

  • EDIT 2: With the introduction of Lambda function custom runtimes, it may be better to check for the AWS_LAMBDA_FUNCTION_NAME environment variable, like so:

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

    EDIT: See the other answer, this is a better solution:

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

    Original answer:

    How about checking for the existence of the context object in the handler function? http://docs.aws.amazon.com/lambda/latest/dg/python-programming-model-handler-types.html