Environment variables can dynamically pass configuration information to the function code to avoid writing it to the function code. As a part of function configuration, environment variables are stored as key-value pairs. Different functions have different environmental variables and do not affect each other. This topic describes the basic information about environment variables, deployment methods in the console, and sample code.
Prerequisites
security
After you configure environment variables, the environment variables are configured to the operating system environment when a function is executed. You can read the configured environment variables from the system environment variables.
When you create or update environment variables, Function Compute encrypts and stores the environment variables by using Advanced Encryption Standard (AES) 256. When you execute the function, the environment variables are automatically decrypted in reverse. This ensures data security.
Scenario
- Deploy a function on different platforms or services
The configurations of the same function code may be different in the test environment and production environment. For example, you can use environment variables to select different Object Storage Service (OSS) buckets, databases, or tables. This way, you can deploy the same function to different platforms or environments without modifying the function code.
- Configure a key
You can use environment variables to configure security-sensitive authentication information, such as a username and a password for connecting to a database and your Alibaba Cloud AccessKey pair.
- 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 prevent confusion, you cannot use the environment variables that Function Compute reserves. Reserved variables include FC_*, accessKeyID, accessKeySecret, securityToken, and topic.
Configure environment variables in the Function Compute console
Configure environment variables by using Serverless Devs
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']
Sample code
Assume that the environment variable {"key":"val"}
is configured. The following code provides an example on how to read the environment
variable and print the value of the environment variable:
var value = process.env.key console.log(value)
import os value = os.environ.get('key') print(value)
System.out.println("value: "+ System.getenv("key"));
$value = getenv('key');
Console.WriteLine(Environment.GetEnvironmentVariable("key"));