When creating or editing a function, you can add, delete, or modify environment variables for the function runtime environment by modifying the environment variables in the configuration.
The configured environment variables will be configured into the OS environment when the function is executed. The function code can read the system environment variables to obtain the specific values and use them in the code.
1.Log in to the Serverless console and click Function Service on the left sidebar
2. When creating or editing a function, you can add environment variables in "Environment Variable".
Environment variables usually appear as key-value
pairs. Enter the required environment variable key in the first input box and the required value in the second one. Note that the value of key
or value
can contain 2–64 bytes of letters, digits, and underscores and must begin with a letter.
For local development, you can configure the Environment
environment variable directly under the function in serverless.yml
and run the scf deploy
command to deploy it to the cloud as shown below:
component: scf # Component name, which is required. It is `scf` in this example
name: scfdemo # Component instance name, which is required
# Component parameter configuration
inputs:
name: scfdemo # Function name, which is `${name}-${stage}-${app}` by default
namespace: default
# 1. Default format. Create a specifically named COS bucket and upload it
src: ./src
type: event # Function. Valid values: event - event-triggered (default), web - HTTP-triggered
handler: index.main_handler # Entry (valid if the function is event-triggered)
runtime: Nodejs10.15 # Runtime environment, which is Nodejs10.15 by default
region: ap-guangzhou # Function region
description: This is a function in ${app} application.
memorySize: 128 # Memory size in MB
timeout: 20 # Function execution timeout period in seconds
initTimeout: 3 # Initialization timeout period in seconds
environment: # Environment variable
variables: # Environment variable object
TEST1: value1
TEST2: value2
After configuring environment variables for the function, you can query the specific configured environment variables by viewing the function configuration, which are displayed in the form of key=value
.
The configured environment variables will be configured into the runtime environment when the function is executed. The code can read the system environment variables to get the specific values and use them in the code. It should be noted that environment variables cannot be read locally.
Assume that the key of the configured environment variable for a function is key
. The following is the sample codes for reading and printing the value of this environment variable in different runtime environments.
import os
value = os.environ.get('key')
print(value)
var value = process.env.key
console.log(value)
In a Java runtime environment, the way to read the environment variables varies by temporary authorized fields and other fields:
For temporary authorized fields (including TENCENTCLOUD_SESSIONTOKEN
, TENCENTCLOUD_SECRETID
, and TENCENTCLOUD_SECRETKEY
), the way to read the environment variables is as follows:
System.out.println("value: "+ System.getProperty("key"));
For other fields, the way to read the environment variables is as follows:
System.out.println("value: "+ System.getenv("key"));
import "os"
var value string
value = os.Getenv("key")
$value = getenv('key');
The following Use Limits apply to the environment variables of functions:
The Key
and Value
of built-in environment variables in the current runtime environment are as shown in the table below:
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 (if a timer trigger is used) |
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 |
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 |
SCF_FUNCTIONNAME | Function name |
SCF_FUNCTIONVERSION | Function version |
TENCENTCLOUD_REGION | Region |
TENCENTCLOUD_APPID | Account APPID |
TENCENTCLOUD_UIN | Account UIN |
TENCENTCLOUD_TZ | Time zone, which is UTC currently |
Was this page helpful?