laurent
laurent

Reputation: 90736

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?

Upvotes: 31

Views: 10880

Answers (5)

EDIT 2: Thanks @MarkB for the update regarding the new feature of custom runtimes.

The Approach: There are certain environment variables whose value is set when code runs in AWS. Checking for the existence of such variables would indicate that the code is running in AWS.

However, due to a new feature my previous take on it with AWS_EXECUTION_ENV environment variable does not work in all cases. From the docs here https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html The AWS_EXECUTION_ENV environment variable is not defined for custom runtimes, which means that checking for its existence when using custom runtimes would not be an indicator of whether the code is running on AWS. One can check for the existence of one of the other AWS_* specific environment variables (see link above). Which one is right for you might depend on your use case. But Mark's suggestion looks good!

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

This works for me The following would work as long as you are using a standard AWS runtime environment

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 ...

Upvotes: 39

Mark B
Mark B

Reputation: 200466

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

Upvotes: 17

Jonathan
Jonathan

Reputation: 11355

This is what I use

import os

try:
  region = os.environ['AWS_REGION']
except:
  # Not in Lambda environment
  region = "us-east-1"

Upvotes: -1

pointtonull
pointtonull

Reputation: 9

Because of this bug it is possible to tell if you are running inside an AWS Lambda Function.

import multiprocessing

def on_lambda():
    try:
        multiprocessing.Pool()
        on_lambda = False
    except:
        on_lambda = True
    return on_lambda

I used this to implement context sensible metric reporting successfully. Lets hope they don't fix the bug any soon!

Upvotes: -2

Jason
Jason

Reputation: 10842

For unit testing I use the structure:

+ my_function/
+- __init__.py - empty files
+- code/
   +- __init__.py
   +- lambda_function.py
+- unittest/
   +- __init__.py
   +- tests.py - from ..code.lambda_function import *

When running unit tests with python -m my_function.unittest.tests, in lambda_function.py the __name__ == 'my_function.code.lambda_function'.

When running in the Lambda running, __name__ == 'lambda_function'. Note that you'll get the same value if you run with python -m my_function.code.lambda_function so you'll always need a wrapper.

Upvotes: 1

Related Questions