Obtain custom environment variables from an application

更新时间:
复制 MD 格式

On Enterprise Distributed Application Service (EDAS), you can define custom environment variables on the application server and read them at runtime from your application code—keeping configuration out of source code and making it easy to update values without redeploying.

Prerequisites

Before you begin, ensure that you have:

  • An EDAS application deployed on an Elastic Compute Service (ECS) instance

  • SSH access to the application server

  • The admin account credentials

Set and access environment variables

  1. Log on to the application server and switch to the admin account:

    su - admin
  2. Open ~/.bash_profile and add your environment variables, then save and exit:

    export API_ENDPOINT="https://api.example.com"
    export DB_HOST="rm-xxxxxxx.mysql.rds.aliyuncs.com"
    Important

    Do not store passwords or API keys in plain text. If you have already added sensitive values, update the credentials in the Alibaba Cloud Management Console immediately and use a dedicated secrets store for long-term secret management.

  3. Load the variables into the current session:

    source ~/.bash_profile
  4. Read the variables from your application code. Use System.getenv() to retrieve them at runtime:

    String endpoint = System.getenv("API_ENDPOINT");
  5. Restart the application for the new environment variables to take effect.

Verify the environment variable

Run the following command on the server to confirm the variable is set correctly:

echo $API_ENDPOINT

The command outputs the value you defined in ~/.bash_profile.

Considerations

  • Variables defined in ~/.bash_profile are loaded each time the admin user starts a login shell. They are not automatically available in non-login shells.

  • Before modifying configurations or data on an ECS instance or an ApsaraDB RDS instance, create snapshots or enable backup features such as log backup for ApsaraDB RDS.