Skip to main content

Environment Variables

When creating or editing a cloud function, you can add, delete, or modify environment variables in the runtime environment by updating the configuration.

After configuring environment variables, they will be injected into the operating system environment during function runtime and remain globally effective throughout function execution. The function code can access system environment variables to retrieve the configured values and utilize them within the code.

Viewing Environment Variables

After configuring environment variables for the cloud function, you can view the specific configured environment variables by checking the function configuration in the cloud function console. The environment variables are displayed in the key=value format.

Using Environment Variables

The configured environment variables are injected into the runtime environment during function execution. You can access system environment variables through code to retrieve the specific values and utilize them in your code. Note that environment variables cannot be accessed locally.

Suppose the environment variable key configured for the cloud function is "key". The following sample code demonstrates how to read and print the value of this environment variable in respective runtime environments.

  • In the Node.js runtime environment, environment variables can be accessed using:
var value = process.env.key
console.log(value)
  • In the Python runtime environment, environment variables can be accessed using:
import os
value = os.environ.get('key')
print(value)

Use Cases

  • Extracting variable values: Place values that may change during business operations into environment variables to avoid modifying the code when business changes occur.
  • Externalizing encrypted information: Extract authentication and encryption-related keys from the code into environment variables to avoid security risks caused by hard-coding these keys in the code.
  • Environment differentiation: Configuration and database information required for different development stages can be extracted into environment variables. For distinct phases like development and release, you only need to maintain the environment variable values to access the development environment database and production environment database respectively.

Usage Limitations

For environment variables in cloud functions, the following usage limitations apply:

  • The key must start with a letter [a-zA-Z] and can only contain alphanumeric characters and underscores ([a-zA-Z0-9_]).
  • Reserved environment variable keys cannot be configured. The reserved keys include:
    • Keys starting with SCF_, such as SCF_RUNTIME.
    • Keys starting with QCLOUD_, such as QCLOUD_APPID.
    • Keys starting with TENCENTCLOUD_, such as TENCENTCLOUD_SECRETID.

Built-in Environment Variables

Environment Variable KeySpecific Value or Value Source
TENCENTCLOUD_SESSIONTOKEN{temporary SESSION TOKEN}
TENCENTCLOUD_SECRETID{temporary SECRET ID}
TENCENTCLOUD_SECRETKEY{temporary SECRET KEY}
_SCF_SERVER_PORT28902
TENCENTCLOUD_RUNENVSCF
USER_CODE_ROOT/var/user/
TRIGGER_SRCtimer (when using a timer trigger)
PYTHONDONTWRITEBYTECODEx
PYTHONPATH/var/user:/opt
CLASSPATH/var/runtime/java x:/var/runtime/java x/lib/*:/opt (x is 8 or 11)
NODE_PATH/var/user:/var/user/node_modules:/var/lang/node x/lib/node_modules:/opt:/opt/node_modules (x is 16, 14, 12, 10, 8, or 6)
PHP_INI_SCAN_DIR/var/user/php_extension:/opt/php_extension
_/var/lang/python3/bin/python x (x is 37, 3, or 2)
PWD/var/user
LOGNAMEqcloud
LANGen_US.UTF8
LC_ALLen_US.UTF8
USERqcloud (event functions have this built-in variable in the Node.js 16.13 environment, while
HOME/home/qcloud
PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SHELL/bin/bash
SHLVL3
LD_LIBRARY_PATH/var/runtime/java x:/var/user:/opt (x is 8 or 11)
HOSTNAME{host id}
SCF_RUNTIMEFunction runtime environment
SCF_FUNCTIONNAMEFunction name
SCF_FUNCTIONVERSIONFunction version
TENCENTCLOUD_REGIONRegion
TENCENTCLOUD_APPIDAccount APPID
TENCENTCLOUD_UINAccount UIN
TENCENTCLOUD_TZTime zone, currently set to UTC
Tip

Code to obtain temporary key information must be placed in the main function to trigger. For details, see: Obtain Runtime Role Temporary Key Information