All Products
Search
Document Center

Environment variables

Last Updated: Oct 22, 2018

Environment variables separate settings from the function code to enhance the flexibility and portability of code. Instead of hard-coding the settings, you can use the environment variables for Function Compute to dynamically pass settings to your function code. Environment variables are key-value pairs that you create and modify as part of your function configuration. You can create the same environment variables for different functions, but these environment variables are independent of each other.

Your data has a high level of security. When you create or update the environment variables, Function Compute encrypts your environment variables using Advanced Encryption Standard 256 (AES256). The environment variables are decrypted automatically by Function Compute when the corresponding function is invoked.

Scenarios

  • Deploy a function across platforms: The configurations for the same code may differ in the testing and production environments. You can configure a function to reference different OSS buckets, databases, or tables depending on the environment variables. You do not need to change the code when a function is applied to different platforms.

  • Configure AccessKeys: You can use environment variables to configure a database username and password, your Alibaba Cloud AccessKey, or other sensitive authentication information.

  • Configure system variables: To use system libraries more flexibly, you can configure PATH and HOME directories using environment variables.

Restrictions

  • Naming rules for the key and value:
    • Key: It must start with letters [a-zA-Z]. Can only contain [a-zA-Z0-9_].
    • Value: It must be printable ASCII characters. Cannot contain other characters such as Chinese characters.
  • Size limit: The total size of the environment variable set cannot exceed 4 KB.
  • System-reserved variables: You cannot use the variables reserved by Function Compute. System-reserved variables include FC_*, accessKeyID, accessKeySecret, securityToken and topic.

Configure environment variables in the console

  1. Log on to the Function Compute console.

  2. Select a region.

    Select a region

  3. Select a service.

  4. Edit variables.

    • For new functions:

      1. In the left-side navigation pane, click the Create Function icon.

      2. Complete the Function Template and Configure Triggers sections. On the Configure Function Settings page, specify the Key and Value under Environment Variables.

        New functions

    • For existing functions:

      1. Find the target function.

      2. In the Function Properties area, click Edit. In the dialog box that appears, specify Key and Value in the Environment Variables area.

        Existing functions

      3. Click OK.

Test environment variables

In the console, write and test the following code.

  1. module.exports.handler = function(event, context, callback) {
  2. var bucket_name = process.env['BUCKET_NAME']
  3. var table_name = process.env['TABLE_NAME']
  4. console.log('BUCKET_NAME: ',bucket_name)
  5. console.log('TABLE_NAME: ',table_name)
  6. callback(null, "success")
  7. }

The following log indicates that the environment variables have been created.Console test 3

Configure environment variables using SDK

Create environment variables

In the Python SDK, the parameter for environment variables is environmentVariables. The values of the parameter are stored in a dictionary. The following is an example function code in Python that is added with environment variables.

  1. # coding: utf-8
  2. import fc2
  3. client = fc2. Client(
  4. endpoint='your endpoint',
  5. accessKeyID='your accessKeyID',
  6. accessKeySecret='your accessKeySecret')
  7. client.create_service('test')
  8. client.create_function(
  9. 'test', 'test_env', 'python2.7', 'main.handler',
  10. codeDir='/path/to/code/', environmentVariables={'testKey': 'testValue'})
  11. res = client.get_function('test', 'test_env')
  12. print res.data

Update environment variables

Run the update_function command to update environment variables.

  1. client.update_function(
  2. 'test', 'test_env', 'python2.7', 'main.handler',
  3. codeDir='/path/to/code/', environmentVariables={'newKey': 'newValue'})
  4. res = client.get_function('test', 'test_env')
  5. print res.data