Environment variables can decouple function code from configurations to improve the flexibility and portability of the code. You can use environment variables in Function Compute to dynamically pass configuration information to function code. This prevents configuration information from being hard-coded in the function code. As a part of function configuration, environment variables are stored as key-value pairs. Different functions can contain different environment variables that are mutually independent.

Security

After you configure environment variables in the Function Compute instance, the Function Compute instance will configure the environment variables to the operating system environment when the function is run. You can read configured environment variables from the system environment variables and then use the values of the environment variables in the code.

When you create or update environment variables, the Function Compute instance encrypts and stores the environment variables by using Advanced Encryption Standard (AES) 256. When you run the function, the environment variables are automatically decrypted in reverse. In this way, data security is enhanced.

Scenarios

  • Deploy a function on different platforms or services

    When you deploy the same function to different platforms or environments, for example, test environment and production environments, you can configure environment variables for different implementations instead of modifying the function code. For example, you can select different Object Storage Service (OSS) buckets, databases, or tables by using environment variables.

  • Configure a key

    When you connect to a database, you can use environment variables to set a user name and password, your Alibaba Cloud AccessKey pair, and other security-sensitive authentication information.

  • Configure system variables

    You can configure the PATH and HOME variables to system directories.

Limits

  • Character set rules
    • A key must start with a letter and can contain letters and digits.
    • A value must contain printable ASCII characters and cannot contain other characters such as Chinese characters.
  • Size limits

    The total size of environment variables cannot exceed 4 KB.

  • Reserved variables

    To avoid confusion, you cannot use the environment variables that are reserved by the Function Compute system. Reserved variables include FC_ *, accessKeyID, accessKeySecret, securityToken, and topic.