tencent cloud

Feedback

Environment Variables

Last updated: 2024-12-02 19:46:29
    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.

    Adding an environment variable

    Adding an environment variable in the console

    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.

    Adding an environment variable locally

    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
    

    Viewing an environment variable

    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.

    Using an environment variable

    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.
    In a Python runtime environment, the way to read the environment variables is as follows:
    import os
    value = os.environ.get('key')
    print(value)
    In a Node.js runtime environment, the way to read the environment variables is as follows:
    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_SESSIONTOKENTENCENTCLOUD_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"));
    In a Go runtime environment, the way to read the environment variables is as follows:
    import "os"
    var value string
    value = os.Getenv("key")
    In a PHP runtime environment, the way to read the environment variables is as follows:
    $value = getenv('key');

    Overview

    Variable value extraction: Values that may change in the business can be extracted into environment variables, eliminating the need to modify the code according to business changes.
    External storage of encrypted information: Keys related to authentication and encryption can be extracted from the code into environment variables, avoiding security risks caused by the presence of relevant keys hard-coded in the code.
    Environment differentiation: The configuration and database information for different development stages can be extracted into the environment variables, so that in different stages of development and release, you only need to modify the environment variable values and execute the development environment database and release environment database separately.

    Use Limits

    The following Use Limits apply to the environment variables of functions:
    The key must begin with a letter ([a-zA-Z]) and can only contain alphanumeric characters and underscores ([a-zA-Z0-9_]).
    The keys of reserved environment variables cannot be modified, including:
    Keys beginning with SCF_, such as SCF_RUNTIME.
    Keys beginning with QCLOUD_, such as QCLOUD_APPID.
    Keys beginning with TENCENTCLOUD_, such as TENCENTCLOUD_SECRETID.

    Built-in Environment Variables

    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
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support