Introduction
By default, Consul-Terraform-Sync (CTS) stores the Terraform state for each task in Consul's KV store using the consul backend. This enables Terraform's state sharing and locking across multiple CTS instances when configured for high availability (HA). The state files are stored in Consul under the KV prefix: consul-terraform-sync/.
Each task's state key follows the format:
consul-terraform-sync/terraform-env:<task_name>In this example, route53-dns-sync is the name of a task defined in the CTS configuration:
$ consul kv get -keys consul-terraform-sync/
consul-terraform-sync/terraform-env:route53-dns-syncBy default, the state file will be compressed with Gzip before being written to Consul, gzip = true. While this reduced storage usage, accessing the file directly using consul kv get will return binary data:
$ consul kv get consul-terraform-sync/terraform-env:route53-dns-sync
�D���� E�~����3T�WJ)3!�e�nB���.�<�\�.��7qIy/���+1c���
�Az/� W��v\�F�>��]0��|�ʌ���De����G�|?r��V
Expected Outcome
Read the Terraform state file created by a CTS task.
Options
1. Decode the response from Consul
Pipe the output through gunzip to decompress on-the-fly:
$ consul kv get consul-terraform-sync/terraform-env:route53-dns-sync | gunzip
2. Use Terraform CLI from the Task Workspace
CTS maintains a full Terraform working directory for each task under sync-tasks/<task_name>. You can use terraform commands to manage the state content in each of the working directory:
$ cd sync-tasks/route53-dns-sync
$ terraform show
$ terraform state list
3. Configure CTS to Disable Gzip.
If you prefer plaintext JSON stored in Consul, disable Gzip in the driver block of the CTS configuration file:
driver "terraform" {
backend "consul" {
gzip = false
}
}
4. Switch to a Local Backend
To completely bypass Consul KV storage and keep plaintext state files on the filesystem:
driver "terraform" {
backend "local" {}
}After restarting CTS, each task will create a readable terraform.tfstate file in the task's working directory.
Please note that using the local backend prevents state sharing and locking when CTS is configured for HA. Only use this with single-instance installations or for debugging.
References
- https://developer.hashicorp.com/consul/docs/automate/infrastructure#consul-terraform-sync-cts
- https://github.com/hashicorp/consul-terraform-sync