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

Configure environment variables in the Function Compute console

  1. Log on to the Function Compute console.
  2. In the top navigation bar, select a region.
  3. In the left-side navigation pane, click Services and Functions. In the Services pane, click the service that you require.
  4. On the Functions tab, find the function that you require and click Modify Configurations in the Actions column.
    config_function
  5. In the Environment Variables section, click Key Value, add key-value pairs, and then click Submit.
    edit-environment-value
    After you click Submit, the Code tab appears.
  6. Enter code in the code editor. The following sample code is used. Then, 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 Logs tab. The printed log shows that the environment variables are created.result

Configure environment variables by using Funcraft

You can configure environment variables by specifying the EnvironmentVariables property defined in Funcraft.

  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 preceding sample code is run to declare the FunDemo service and the envdemo function for the FunDemo service. In addition, set the function handler to index.handler, the runtime of the function to Python 2.7, and the CodeUri property to the current directory. When you deploy the function, Funcraft packages the items in 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 configured for the function.

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

Configure environment variables by using SDKs

In this example, the SDK for Python is used. The environmentVariables parameter specifies environment variables. The values of this parameter are stored in alphabetical order. The following code provides an example on how to create, update, and obtain environment variables:

  • 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']