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
adminaccount credentials
Set and access environment variables
-
Log on to the application server and switch to the
adminaccount:su - admin -
Open
~/.bash_profileand add your environment variables, then save and exit:export API_ENDPOINT="https://api.example.com" export DB_HOST="rm-xxxxxxx.mysql.rds.aliyuncs.com"ImportantDo 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.
-
Load the variables into the current session:
source ~/.bash_profile -
Read the variables from your application code. Use
System.getenv()to retrieve them at runtime:String endpoint = System.getenv("API_ENDPOINT"); 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_profileare loaded each time theadminuser 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.