As a part of function configuration, environment variables are stored as key-value pairs. This topic describes how to configure environment variables in the Function Compute console, by using the FunCraft tool, and by using an SDK.

Configure environment variables in the console

  1. Log on to the Function Compute console.
  2. In the top navigation bar, select your region.
  3. In the left-side navigation pane, click Service/Function.
  4. Click the target service.
  5. Find the target function and click Configure in the Actions column.
    setting
  6. Add the key-value pairs of environment variables and click Submit.

    Click the Code tab.

    edit-environment-value
  7. Enter code in the code editor. The sample code is as follows. Click Invoke to debug the function.
    module.exports.handler = function(event, context, callback) {
      var bucket_name = process.env['BUCKET_NAME']
      var table_name = process.env['TABLE_NAME']
      console.log('BUCKET_NAME: ',bucket_name)
      console.log('TABLE_NAME: ',table_name)
      callback(null, "success")
    }           
    Click the Log tab. According to the printed log, you can find that the environment variables have been created. result

Configure environment variables by using FunCraft

You can configure environment variables by specifying the EnvironmentVariables property in the FunCraft specifications.

  1. Run the following sample code to configure a function and environment variables:
    ROSTemplateFormatVersion: '2015-09-01'
    Transform: 'Aliyun::Serverless-2018-04-03'
    Resources:
      FunDemo:
        Type: 'Aliyun::Serverless::Service'
        envdemo:
          Type: 'Aliyun::Serverless::Function'
          Properties:
            Handler: index.handler
            CodeUri: . /
            Runtime: python2.7
            EnvironmentVariables:
              OSSEndpoint: oss-cn-hangzhou.aliyuncs.com
              BucketName: fun-local-test           

    The description of the preceding sample code is as follows: Declare a service named FunDemo and then declare a function named envdemo for the FunDemo service. Set the function handler to index.handler and the function runtime to python2.7. In addition, set the CodeUri property to the current directory. During deployment, FunCraft packages the directory specified by the CodeUri property and then uploads the package. You can store dependencies in the directory specified by the CodeUri property.

    An environment variable with the key OSSEndpoint and the value oss-cn-hangzhou.aliyuncs.com and an environment variable with the key BucketName and the value fun-local-test are further configured for the function.

  2. Run the fun deploy command to deploy the function.
    After the deployment is complete, log on to function Compute console to view the created FunDemo service, the envdemo function, and environment variables with the keys OSSEndpoint and BucketName.

Configure environment variables by using an SDK

The following description uses the Python SDK as an example. The environmentVariables parameter specifies environment variables. The values of this parameter are stored in alphabetical order. The sample code for creating, updating, and obtaining environment variables is as follows:

  • Create an environment variable
    # coding: utf-8
    
    import fc2
    client = fc2.Client(
        endpoint='your endpoint',
        accessKeyID='your accessKeyID',
        accessKeySecret='your accessKeySecret')
    client.create_service('test')
    client.create_function(
        'test', 'test_env', 'python2.7',  'main.handler',
        codeDir='/path/to/code/', environmentVariables={'testKey': 'testValue'})
    res = client.get_function('test', 'test_env')
    print res.data                   
  • Update an environment variable
    client.update_function(
        'test', 'test_env', 'python2.7',  'main.handler',
        codeDir='/path/to/code/', environmentVariables={'newKey': 'newValue'})
    res = client.get_function('test', 'test_env')
    print res.data            
  • Obtain environment variables
    resp = client.get_function('test', 'test_env')
    env = func['environmentVariables']