You can achieve this by using either envconsul or Teller.
Quoting envconsul's docs, "Envconsul provides a convenient way to launch a subprocess with environment variables populated from HashiCorp Consul and Vault."
Teller, on the other hand, supports reading data from other providers, in addition to Consul.
Here are two examples of using both envconsul and Teller to read keys from public_stable/
and public_temp/
, and makes those available as environment variables to the launched sub-process.
First, create a test key under each of these paths.
$ consul kv put public_stable/STABLE_URL https://example.com/stable_website
Success! Data written to: public_stable/STABLE_URL
$ consul kv put public_temp/TEMP_RELOAD_INTERVAL 600
Success! Data written to: public_temp/TEMP_RELOAD_INTERVAL
envconsul
Download envconsul. Set the CONSUL_HTTP_ADDR
environment variable to the address of your Consul client's API. If ACLs are enabled in the environment, you will also need to configure the CONSUL_HTTP_TOKEN
environment variable.
export CONSUL_HTTP_ADDR=http://localhost:8500
export CONSUL_HTTP_TOKEN=<token>
Run the following command which instructs envconsul to query the keys under the public_temp and public_stable, and make them available as environment variables. envconsul will run the env
command so that we can see the set of variables that are provided to the sub-process.
$ envconsul -pristine -prefix public_temp -prefix public_stable -once env
TEMP_RELOAD_INTERVAL=600
STABLE_URL=https://example.com/stable_website
Teller
Install Teller and create .teller.yml
in your project's directory with the following contents.
.teller.yml
---
opts:
environment: public
# Providers
providers:
# Configure via environment:
# CONSUL_HTTP_ADDR
# CONSUL_HTTP_TOKEN, if ACLs are enabled
consul:
env_sync:
path: "{{environment}}_stable/"
consul2:
kind: consul
env_sync:
path: "{{environment}}_temp/"
Use teller env
to output the retrieved key-value pairs in .env
file format in order to validate the data is correctly being merged into a single set of environment variables.
$ teller env
TEMP_RELOAD_INTERVAL=600
STABLE_URL=https://example.com/stable_website
You can then run your application under Teller using teller run
. Details of this command's syntax are documented in https://github.com/SpectralOps/teller#running-running-subprocesses.