Environment Variables
This section describes how to configure function environment variables on the FunctionGraph console.
Scenarios
Environment variables allow you to pass dynamic parameters to a function without modifying code.
Scenarios:
- Environment distinguishing: Configure different environment variables for the same function logic. For example, use environment variables to configure testing and development databases.
- Configuration encryption: Configure encrypted environment variables to dynamically obtain authentication information (account, password) required to access other services.
- Dynamic configuration: Configure environment variables for parameters that need to be dynamically adjusted, including query period and timeout, in function logic.
Notes and Constraints
When you define environment variables, FunctionGraph displays all your input information in plain text. For security purposes, do not include sensitive information.
Configuring Environment Variables
- Log in to the FunctionGraph console. In the navigation pane, choose Functions > Function List.
- Click the name of a function.
- Choose Configuration > Environment Variables and click Edit Environment Variable.
Figure 1 Adding environment variables
- On the displayed dialog box, click Add and configure the environment variable information.
- Configure Static Encryption for the environment variables. (This step is optional and available only in the LA-Sao Paulo1 region.)
To use static encryption with KMS, add the permission policy to the agency of the function. The following KMS static encryption modes are supported:
- functiongraph/default (default): The function automatically creates a default key in DEW.
- Customer master key (CMK): Select the CMK to encrypt the code. For details about how to create a CMK, see Creating a Custom Key.
If you select Customer master key (CMK), do not delete the CMK used for function encryption in DEW. Otherwise, the function execution will fail because encrypted data cannot be decrypted.
- After the configuration is complete, click OK to save the environment variables.
After the environment variables are configured, you can use the environment variables in functions. For details, see Application Example of Environment Variables.
Preset Environment Variables
Table 2 lists the preset environment variables.
Environment Variable |
Description |
Obtaining Method |
---|---|---|
RUNTIME_PROJECT_ID |
Project ID |
Obtain the value from a Context interface or a system environment variable. |
RUNTIME_FUNC_NAME |
Function name |
Obtain the value from a Context interface or a system environment variable. |
RUNTIME_FUNC_VERSION |
Function version |
Obtain the value from a Context interface or a system environment variable. |
RUNTIME_HANDLER |
Handler |
Obtain the value from a system environment variable. |
RUNTIME_TIMEOUT |
Execution timeout allowed for a function |
Obtain the value from a system environment variable. |
RUNTIME_USERDATA |
Value passed through an environment variable |
Obtain the value from a Context interface or a system environment variable. |
RUNTIME_CPU |
CPU usage of a function. The value is in proportion to MemorySize. |
Obtain the value from a Context interface or a system environment variable. |
RUNTIME_MEMORY |
Memory size configured for a function, in MB |
Obtain the value from a Context interface or a system environment variable. |
RUNTIME_MAX_RESP_BODY_SIZE |
Maximum size of the return value (default: 6,291,456 bytes) |
Obtain the value from a system environment variable. |
RUNTIME_INITIALIZER_HANDLER |
Initializer |
Obtain the value from a system environment variable. |
RUNTIME_INITIALIZER_TIMEOUT |
Initialization timeout of a function |
Obtain the value from a system environment variable. |
RUNTIME_ROOT |
Path of the runtime package (default: /home/snuser/runtime) |
Obtain the value from a system environment variable. |
RUNTIME_CODE_ROOT |
Path of the code in the container (default: /opt/function/code) |
Obtain the value from a system environment variable. |
RUNTIME_LOG_DIR |
Directory for storing system logs in the container (default: /home/snuser/log) |
Obtain the value from a system environment variable. |
Application Example of Environment Variables
You can use environment variables to configure which directory to install files in, where to store outputs, and how to store connection and logging settings. These settings are decoupled from the application logic, so you do not need to update your function code when you change the settings.
- Using environment variable obs_output_bucket, you can flexibly set the OBS bucket used for storing output images.
Figure 4 Environment variables
- Use environment variables in function code.
In the following code snippet, obs_output_bucket is the bucket used for storing processed images.
- Non-HTTP functions use context.getUserData ('xxx') to obtain environment variables.
- HTTP functions use system methods to obtain environment variables. For example, Python functions use os.Environ['xx'], and Node.js functions use process.env.xx. Node.js functions use process.env.RUNTIME_USERDATA to obtain encrypted environment variables.
- Python
def handler(event, context): srcBucket, srcObjName = getObsObjInfo4OBSTrigger(event) obs_address = context.getUserData('obs_address') outputBucket = context.getUserData('obs_output_bucket') if obs_address is None: obs_address = '{obs_address_ip}' if outputBucket is None: outputBucket = 'casebucket-out' ak = context.getSecurityAccessKey() sk = context.getSecuritySecretKey() st = context.getSecurityToken() # download file uploaded by user from obs # TODO: Replace with actual implementation GetObject(obs_address, srcBucket, srcObjName, ak, sk, st) outFile = watermark_image(srcObjName) # Upload converted files to a new OBS bucket. # TODO: Replace with actual implementation PostObject(obs_address, outputBucket, outFile, ak, sk, st) return 'OK'
- Node.js
exports.handler = async (event, context) => { let bucket = context.getUserData('obs_output_bucket'); console.log(bucket); const output = { 'statusCode': 200, 'headers': { 'Content-Type': 'application/json' }, 'isBase64Encoded': false, 'body': JSON.stringify(event), } return output; }
Helpful Links
- Manage the function lifecycle using APIs. For details, see Function Lifecycle Management APIs.
- For FAQs about function environment variables, see Function Configuration FAQs.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot