Cloud Functions Get Project ID, Region, Function Name, Memory, Timeout etc. (Python)

Jun 10, 2019
import osdef test(request):    REGION = os.environ.get('FUNCTION_REGION')      # us-central1    PROJECT_ID = os.environ.get('GCP_PROJECT')    FUNCTION_NAME = os.environ.get('FUNCTION_NAME') # test    IDENTITY = os.environ.get('FUNCTION_IDENTITY')  # [email protected]    HOST = request.headers.get('Host')              # us-central1-PROJECT_ID.cloudfunctions.net    AUTHORIZATION = request.headers.get('Authorization')    # Bearer eyJh...    USER_AGENT = request.headers.get('User-Agent')  # Google-Cloud-Scheduler, Google-Cloud-Tasks    FUNCTION_MEMORY_MB = os.environ.get('FUNCTION_MEMORY_MB')       # 256    FUNCTION_TIMEOUT_SEC = os.environ.get('FUNCTION_TIMEOUT_SEC')   # 60    return 'OK'

Headers

Content-Length: 0
Content-Type: text/plain
Host: us-central1-PROJECT_ID.cloudfunctions.net
User-Agent: Google-Cloud-Scheduler
Accept-Encoding: gzip,deflate,br
Authorization: Bearer eyJhb...
Forwarded: for="35.187...";proto=https
Function-Execution-Id: o1i60x7f1ebu
X-Appengine-Country: ZZ
X-Appengine-Default-Version-Hostname: b54...-tp.appspot.com
X-Appengine-Https: on
X-Appengine-Request-Log-Id: 5cfde...
X-Appengine-User-Ip: 35.187.132.55
X-Cloud-Trace-Context: abbd...;o=1
X-Forwarded-For: 35.187...
X-Forwarded-Proto: https

Environment Variables

{
    'VIRTUAL_ENV': '/env',
    'FUNCTION_TRIGGER_TYPE': 'HTTP_TRIGGER',
    'X_GOOGLE_FUNCTION_VERSION': '4',
    'PORT': '8080',
    'X_GOOGLE_FUNCTION_REGION': 'us-central1',
    'X_GOOGLE_GCLOUD_PROJECT': 'PROJECT_ID',
    'X_GOOGLE_CONTAINER_LOGGING_ENABLED': 'false',
    'X_GOOGLE_FUNCTION_IDENTITY': '[email protected]',
    'X_GOOGLE_GCP_PROJECT': 'PROJECT_ID',
    'X_GOOGLE_FUNCTION_TRIGGER_TYPE': 'HTTP_TRIGGER',
    'NODE_ENV': 'production',
    'X_GOOGLE_FUNCTION_MEMORY_MB': '256',
    'X_GOOGLE_SUPERVISOR_HOSTNAME': '169.254...',
    'GCLOUD_PROJECT': 'PROJECT_ID',
    'ENTRY_POINT': 'test',
    'PWD': '/user_code',
    'GCP_PROJECT': 'PROJECT_ID',
    'X_GOOGLE_SUPERVISOR_INTERNAL_PORT': '8081',
    'WORKER_PORT': '8091',
    'X_GOOGLE_FUNCTION_NAME': 'test',
    'SUPERVISOR_INTERNAL_PORT': '8081',
    'HOME': '/tmp',
    'DEBIAN_FRONTEND': 'noninteractive',
    'X_GOOGLE_ENTRY_POINT': 'test',
    'FUNCTION_MEMORY_MB': '256',
    'SUPERVISOR_HOSTNAME': '169.254...',
    'X_GOOGLE_LOAD_ON_START': 'false',
    'FUNCTION_IDENTITY': '[email protected]',
    'FUNCTION_TIMEOUT_SEC': '60',
    'FUNCTION_NAME': 'test',
    'X_GOOGLE_WORKER_PORT': '8091',
    'X_GOOGLE_FUNCTION_TIMEOUT_SEC': '60',
    'FUNCTION_REGION': 'us-central1',
    'CODE_LOCATION': '/user_code',
    'X_GOOGLE_CODE_LOCATION': '/user_code',
    'PATH': '/env/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
    'LC_CTYPE': 'C.UTF-8'
}

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.