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 Key | Specific Value or Value Source |
|---|---|
| TENCENTCLOUD_SESSIONTOKEN | {temporary SESSION TOKEN} |
| TENCENTCLOUD_SECRETID | {temporary SECRET ID} |
| TENCENTCLOUD_SECRETKEY | {temporary SECRET KEY} |
| _SCF_SERVER_PORT | 28902 |
| TENCENTCLOUD_RUNENV | SCF |
| USER_CODE_ROOT | /var/user/ |
| TRIGGER_SRC | timer (when using a timer trigger) |
| PYTHONDONTWRITEBYTECODE | x |
| 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 |
| LOGNAME | qcloud |
| LANG | en_US.UTF8 |
| LC_ALL | en_US.UTF8 |
| USER | qcloud (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 |
| SHLVL | 3 |
| LD_LIBRARY_PATH | /var/runtime/java x:/var/user:/opt (x is 8 or 11) |
| HOSTNAME | {host id} |
| SCF_RUNTIME | Function runtime environment |
| SCF_FUNCTIONNAME | Function name |
| SCF_FUNCTIONVERSION | Function version |
| TENCENTCLOUD_REGION | Region |
| TENCENTCLOUD_APPID | Account APPID |
| TENCENTCLOUD_UIN | Account UIN |
| TENCENTCLOUD_TZ | Time zone, currently set to UTC |
Code to obtain temporary key information must be placed in the main function to trigger. For details, see: Obtain Runtime Role Temporary Key Information