Consul Backend
The Consul backend stores state in HashiCorp Consul with support for state locking and consistency.Implementation
Location:/internal/backend/remote-state/consul/backend.go
Use Cases
- Service mesh environments using Consul
- High availability requirements with strong consistency
- Dynamic infrastructure with service discovery
- Multi-datacenter deployments
Basic Configuration
Required Configuration
path
- Type: String
- Required: Yes
- Description: Path in the Consul KV store where the state will be stored
Optional Configuration
address
- Type: String
- Optional: Yes
- Default:
"127.0.0.1:8500" - Description: Address of the Consul cluster
scheme
- Type: String
- Optional: Yes
- Default:
"http" - Valid Values:
http,https - Description: URL scheme for communicating with Consul
datacenter
- Type: String
- Optional: Yes
- Description: Datacenter to use (defaults to agent’s datacenter)
Authentication
Access Token (ACL)
CONSUL_HTTP_TOKEN
HTTP Basic Authentication
username:password (password is optional)
TLS/SSL Configuration
CA Certificate
CONSUL_CACERT
Client Certificate (Mutual TLS)
CONSUL_CLIENT_CERTCONSUL_CLIENT_KEY
State Compression
- Type: Boolean
- Optional: Yes
- Default:
false - Description: Compress the state data using gzip before storing in Consul
State Locking
- Type: Boolean
- Optional: Yes
- Default:
true - Description: Enable state locking
false to disable locking.
Configuration Options Summary
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
path | string | Yes | - | Path in Consul KV store |
access_token | string | No | - | Consul ACL token |
address | string | No | 127.0.0.1:8500 | Consul server address |
scheme | string | No | http | URL scheme (http/https) |
datacenter | string | No | - | Consul datacenter |
http_auth | string | No | - | HTTP basic auth credentials |
gzip | bool | No | false | Compress state data |
lock | bool | No | true | Enable state locking |
ca_file | string | No | - | Path to CA certificate |
cert_file | string | No | - | Path to client certificate |
key_file | string | No | - | Path to client key |
Workspaces
The Consul backend supports workspaces. Each workspace is stored at a different path:Example: Production Setup with TLS
Example: Multi-Datacenter
ACL Requirements
When using Consul ACLs, the token needs:session permission is required for state locking.
Advantages
- Native Locking - Built-in distributed locking with Consul sessions
- High Availability - Consul’s consensus protocol ensures consistency
- Service Discovery - Integrates with existing Consul infrastructure
- Multi-Datacenter - Support for geo-distributed teams
- Encryption - TLS support for data in transit
Limitations
- Size Limits - Consul has a default maximum value size of 512KB
- Compression Recommended - Enable
gzipfor large state files - Performance - Not optimized for very large state files
Best Practices
- Enable compression for state files larger than 100KB
- Use ACL tokens with minimal required permissions
- Enable TLS for production environments
- Separate paths for different environments
- Monitor Consul performance and health
- Keep state small - avoid storing large binary data