Alex Cohen
Alex Cohen

Reputation: 6226

How to set up Terraform "COMMAND: REMOTE CONFIG" with consul

I have a server I am using for self healing and auto scaling of a consul cluster. It does this by with terraform scripts that are run by consul watches and health checks.

I want to add an additional backup terraform server for failover. To do this I must share the terraform.tfstate and terraform.tfstate.backup between my servers so that they can run terraform on the same resources. I would like to share these files using the Terraform "COMMAND: REMOTE CONFIG", but to me it is unclear as to how I would begin the share.

Basically I want the terraform.tfstate and terraform.tfstate.backup files to constantly be in sync on both servers. Here is my attempt at setting this up. Note that both terraform servers are running consul clients connected to the rest of my consul cluster:

terraform remote config \
-backend=consul \
-backend-config="address=localhost:8500" \
-backend-config="path=/consul-scripts/terr/terraform.tfstate" \
-backend-config="backup=/consul-scripts/terr/terraform.tfstate.backup" \
-path="/consul-scripts/terr/terraform.tfstate" \
-backup="/consul-scripts/terr/terraform.tfstate.backup" \
-address="localhost:8500" 

However this was obviously the wrong syntax. When trying to run the consul example provided on the linked documentation I received the following output:

username@hostname:/consul-scripts/terr$ terraform remote config \
>     -backend=consul \
>     -backend-config="address=localhost:8500" \
>     -backend-config="path=/consul-scripts/terr/terraform.tfstate"
Error writing backup state file: open terraform.tfstate.backup: permission denied

I would like to have my terraform servers sync up through the Terraform "COMMAND: REMOTE CONFIG" instead of a normal file share system like glusterfs or something.

How can I correctly sync my terraform files in this way?

Upvotes: 0

Views: 694

Answers (1)

Alex Cohen
Alex Cohen

Reputation: 6226

So yea @Martin Atkins got it right I just had to run the example in the documentation with sudo. Using the terraform remote config will store the .tfstate files in a hidden directory .terraform/ within the directory containing the terraform scripts.

How terraform remote config works is that it creates a key value in consul that contains the details of the tfstate file.

The answer is very close to what is listed in the documentation. In practice using terraform remote config is a 3 step process.

Before running terraform the following should be run to pull the current tfstate file:

#this will pull the current tfstate file
#if none exists it will create the tfstate key value
terraform remote config \
-backend=consul \
-backend-config="address=localhost:8500" \
-backend-config="path=tfstate" \
pull

Then run:

terraform apply

After this is finished run the following to push the updated tfstate file out to consul in order to change the key value:

terraform remote config \
-backend=consul \
-backend-config="address=localhost:8500" \
-backend-config="path=tfstate" \
push

Upvotes: 1

Related Questions