How to pass environmental variables in envconsul config file?

When working with envconsul, passing environmental variables effectively is crucial for managing configurations in dynamic environments. Envconsul provides a seamless way to inject environment variables retrieved from Consul, making it a powerful tool for managing runtime configurations in applications.

In this article, we will explore how to pass environmental variables in an envconsul configuration file, ensuring secure and efficient application deployment.

Understanding Envconsul

Envconsul is a HashiCorp tool that allows you to retrieve key-value data from Consul and expose it as environment variables. This approach helps applications dynamically configure themselves based on stored settings in Consul, eliminating the need for hardcoded configurations.

By using envconsul, developers can ensure that their applications adjust immediately when configuration changes occur, making it ideal for cloud-based and containerized workloads.

Setting Up an Envconsul Configuration File

To streamline the process, we can define settings in an envconsul configuration file instead of passing options via the command line. A typical configuration file is written in JSON or HCL.

Here is an example of a basic envconsul configuration file written in HCL:

exec {
  command = ["your-app"]
}

consul {
  address = "127.0.0.1:8500"
}

vault {
  enabled = true
  address = "https://vault.service.consul"
}

env {
  keys = ["app/config", "database/config"]
}

In this configuration:

  • exec: Defines the command to execute the application.
  • consul: Specifies the address of the Consul agent.
  • vault: Enables HashiCorp Vault integration for retrieving secrets.
  • env: Lists the Consul keys to be exposed as environment variables.

Passing Environmental Variables from Consul

The env block in the configuration file helps specify which key-paths from Consul should be mapped to environment variables. The structure of the env block enables selective import of only the required environment variables.

For instance, if Consul contains the following key-value pairs:

app/config/database_url = "postgres://user:password@localhost:5432/mydb"
app/config/log_level = "debug"

Then, by specifying "app/config" in the env block, envconsul will automatically convert these into:

DATABASE_URL="postgres://user:password@localhost:5432/mydb"
LOG_LEVEL="debug"

This mapping makes it easy for applications to consume configurations as environment variables without modifying the code.

Using Wildcards for Dynamic Configuration Management

Envconsul allows the use of wildcards in key paths to fetch multiple configurations dynamically. Instead of specifying each key manually, using a wildcard improves flexibility.

For example:

env {
  keys = ["app/config/*"]
}

This setting retrieves and exposes all keys under app/config/ as environment variables, enhancing the adaptability of applications to configuration changes.

Overriding Environment Variables

By default, envconsul does not override existing environment variables in the shell. However, this behavior can be changed using the pristine flag:

pristine = true

When set to true, envconsul will override any existing environment variables, ensuring that only variables from Consul are used.

Security Considerations

Since sensitive data (such as database credentials or API keys) can be exposed through environment variables, security best practices should be followed:

  • Use HashiCorp Vault integration whenever possible to store sensitive credentials securely.
  • Limit access to envconsul configurations to trusted users and services.
  • Ensure that secrets are not inadvertently logged by the application.

Running Envconsul with the Config File

Once the configuration file is prepared, run envconsul using:

envconsul -config=/path/to/config-file.hcl

This command will fetch environment variables from Consul based on the specified settings and execute the defined application command.

Conclusion

Passing environmental variables in an envconsul configuration file is a robust method for dynamic configuration management in cloud and microservices architectures. By properly configuring the env block, using wildcards, and integrating security best practices, developers can ensure that applications adapt seamlessly to configuration changes.

Implementing envconsul correctly will simplify configuration management, reduce manual intervention, and enhance the application’s security posture.