> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Using Envconsul to monitor Changes to Consul KV

> This tutorial explains how to install and use HashiCorp envconsul to monitor Consul KV changes and expose them as environment variables.

In this tutorial, you’ll learn how to install and run HashiCorp envconsul on an application server. Envconsul fetches key-value pairs from Consul and exposes them as environment variables, automatically watching for changes without manual intervention.

## Prerequisites

* A running Consul cluster with KV entries under `apps/ecommerce`
* SSH access to your application server
* (Optional) Consul ACL token if your cluster enforces ACLs

### Example KV Entries

| Consul KV Path                     | Value                                               |
| ---------------------------------- | --------------------------------------------------- |
| `apps/ecommerce/database_host`     | `"customer-db"`                                     |
| `apps/ecommerce/database`          | `"billing"`                                         |
| `apps/ecommerce/connection_string` | `"Server=customer-db;Database=billing;User Id=..."` |

## 1. Install envconsul

Download the latest Linux AMD64 binary from HashiCorp’s release archive:

```bash theme={null}
curl --silent -Lo /tmp/envconsul.zip \
  https://releases.hashicorp.com/envconsul/0.11.0/envconsul_0.11.0_linux_amd64.zip
```

Unzip and move the binary into your `PATH`:

```bash theme={null}
unzip /tmp/envconsul.zip -d /tmp
sudo mv /tmp/envconsul /usr/local/bin/
```

Verify the installation:

```bash theme={null}
envconsul --version
# => envconsul v0.11.0
```

<Callout icon="lightbulb" color="#1CB2FE">
  Make sure to update the version number in the URL if a newer release is available.
</Callout>

## 2. Fetch and Export KV Data

Use `envconsul` to pull all keys under the `apps/ecommerce` prefix and convert them to uppercase environment variables:

```bash theme={null}
envconsul \
  -prefix="apps/ecommerce" \
  -uppercase \
  -- printenv
```

Sample output:

```bash theme={null}
DATABASE_HOST=customer-db
DATABASE=billing
CONNECTION_STRING=Server=customer-db;Database=billing;User Id=...
```

### Common Flags

| Flag         | Description                                        |
| ------------ | -------------------------------------------------- |
| `-prefix`    | Consul KV path to fetch                            |
| `-uppercase` | Convert all keys to uppercase environment variable |
| `--`         | Separator before the command to execute            |

## 3. Launch Your Application with envconsul

To ensure your application picks up the variables at startup and on any KV change:

```bash theme={null}
envconsul \
  -prefix="apps/ecommerce" \
  -uppercase \
  -- ./start-your-app.sh
```

<Callout icon="triangle-alert" color="#FF6B6B">
  If your app requires a Consul ACL token, set `CONSUL_HTTP_TOKEN` in the environment or pass `-token` to `envconsul`.
</Callout>

## Summary

In this demo, you:

1. Installed `envconsul` on a Linux server.
2. Retrieved KV pairs from Consul and exposed them as environment variables.
3. Launched your application to inherit and watch for configuration updates automatically.

By integrating `envconsul`, your service maintains up-to-date settings without manual polling or restarts.

## Links and References

* [envconsul Releases][1]
* [Consul KV Documentation](https://www.consul.io/docs/kv)
* [HashiCorp envconsul Overview](https://www.hashicorp.com/products/envconsul)

[1]: https://releases.hashicorp.com/envconsul/

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/hashicorp-certified-consul-associate-certification/module/70a7eb0f-aec7-41aa-b417-398c341698b6/lesson/ffc95153-fa8e-41ed-a1c8-3f2e71794713" />
</CardGroup>
