When using envconsul, the typical goal is to retrieve environment variables from Consul or Vault and inject them into the runtime environment of an application. envconsul provides multiple methods to configure the retrieval and propagation of these environment variables. Below are general steps and specific configuration examples:
1. Install envconsul
First, ensure that envconsul is installed on your system. You can download the version for your operating system from the official website.
2. Configure envconsul
You can configure envconsul using configuration files or command-line parameters. Configuration files typically use HCL or JSON format. Here, we focus on how to pass environment variables in configuration files.
Example Configuration File (config.hcl):
hclconsul { address = "localhost:8500" token = "your-consul-token" } vault { address = "https://vault.server:8200" token = "your-vault-token" } exec { command = "your-application" env { no_prefix = true } } secret { path = "secret/data/myapp/config" format = "dotenv" }
3. Passing Environment Variables
In the above configuration, the secret section specifies the source path for sensitive configurations (in this example, the secret/data/myapp/config path in Vault). The format option determines how configuration data is processed, and the dotenv format converts key-value pairs into environment variable format. The env setting in the exec section controls the prefix for environment variables. Setting no_prefix to true means no prefix is applied, so key-value pairs retrieved from Consul or Vault are directly used as environment variable names.
4. Running envconsul
After completing the configuration, run envconsul using the following command to pass the configured environment variables to your application:
bashenvconsul -config=config.hcl
This command launches envconsul, reads the configuration from config.hcl, and starts the specified application your-application while providing the configured settings as environment variables.
Summary
With envconsul, you can securely manage and propagate the configuration and sensitive data required for application runtime. Using configuration files enables clearer and more systematic management of these settings, especially in complex deployment environments.