Environment Variables

Last update:2024-09-03 15:10:11

Environment variables are variables that are available to your function during runtime. They provide a mechanism to control the behavior of your function flexibly without modifying the code itself. By using environment variables, developers can separate sensitive information and configuration parameters from their code, enhancing security and maintainability. For instance, you can store database connection details, API keys, and other sensitive data in environment variables. You can also use them to control specific function behaviors, such as toggling feature flags or adjusting timeout durations.

Benefits of Using Environment Variables

  • Enhanced Security: Storing sensitive information in environment variables prevents it from being hardcoded into your code, improving security. Even if your code is compromised, attackers cannot access sensitive data stored in environment variables.
  • Increased Flexibility: You can modify the values of environment variables at any time without needing to change your code or redeploy the function. This allows you to easily use different configurations in different environments or dynamically adjust the function’s behavior during runtime.

Configuring Environment Variables

Before you begin, ensure that you have created at least one function, and it has been successfully deployed. You can follow these steps to set up and manage environment variables for your functions in the Edge Cloud Apps:

  1. Log in to the CDNetworks Console, select Edge Application product, and navigate to the Function List page under the Edge Cloud Apps menu bar.
  2. Select the target function for which you wish to set environment variables and navigate to its details page.
  3. Switch to the Environment Variables tab and click the Edit button to enter the environment variable editing page.
  4. On this page, you can add, modify, or delete environment variables.

Parameter Description

Parameter Description
Variable Name (Key) The name of the environment variable you created.
Current Value The currently active variable value that has been deployed and is in use in the production environment.
New Value (To be Deployed) The modified variable value that has been saved on the platform but is not yet in effect. It is recommended to verify the configuration in the CloudIDE pre-deployment environment before deploying.


  1. After configuring the environment variables, click the Save button to save all changes.
  2. Test your function in the CloudIDE pre-deployment environment to ensure that the environment variables are effective and that the function can read and use their values correctly.
  3. Once verified, click the Deploy button to deploy the environment variables to the production environment.

Usage Limits

To ensure platform stability and performance, Edge Cloud Apps imposes certain limitations on the use of environment variables:

  • Key: Must start with an uppercase or lowercase letter and can only contain uppercase and lowercase letters, numbers, and underscores (_). The length must be between 2 and 64 characters.
  • Value: Cannot exceed 512 characters in length and cannot be empty.
  • Number of Environment Variables: Each function can have a maximum of 20 environment variables. Contact the CDNetworks technical support team if you require more.

Using Environment Variables in Code

You can directly reference environment variables within the global scope of your function code. For example, if the environment variables are configured as:

{"vars":{"DATABASE_URL":"mysql://user:password@host:port/database","API_KEY":"your_api_key"}}

You can directly use DATABASE_URL and API_KEY in your code to access their values, as shown in the following example:

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
  // Connect to the database
  const db = connectToDatabase(DATABASE_URL); 
  // Call the API
  const apiResponse = await fetch('https://api.example.com/?key=' + API_KEY); 
  // ...
}

Notes

  • Test Environment Verification: After saving environment variables, always test them in the CloudIDE testing environment to ensure that the function can read and use them correctly.
  • Error Handling: To guarantee the quality of your online services, it is recommended to add error handling mechanisms to your code, such as handling scenarios where environment variables are not defined or have invalid values. This can prevent function execution failures due to environment variable configuration errors.
  • Deployment Takes Effect: Modified environment variables will take effect online only after deployment. The online environment will continue to use the old environment variable values until you deploy the updated function.
  • Code Modification: If you modify the function code, redeploy the function using the grayscale deployment process to ensure that both the new code and the new environment variables take effect together.
  • Function Deployment Prerequisite: You must deploy a function successfully before you can deploy environment variables. Environment variables are bound to functions, so you can only set them when the function exists.
Is the content of this document helpful to you?
Yes
I have suggestion
Submitted successfully! Thank you very much for your feedback, we will continue to strive to do better!