Consul installation and setting consul as terraform backend

Anant SarafAnant Saraf
2 min read

Install consul

sudo yum install -y yum-utils shadow-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install consul

Verify the installation:

consul --version

Create a Consul configuration file:

sudo mkdir -p /etc/consul.d
sudo vim /etc/consul.d/consul.hcl

Add the following content to the consul.hcl file:

codedata_dir = "/opt/consul"
server = true
bootstrap_expect = 1
bind_addr = "0.0.0.0"
client_addr = "0.0.0.0"
ui = true

Create a systemd service file for Consul:

sudo vim /etc/systemd/system/consul.service
[root@ip-172-31-0-81 ~]# which consul
/usr/bin/consul

Add the following content to the consul.service file:

[Unit]
Description=Consul
Documentation=https://www.consul.io/
After=network-online.target
Wants=network-online.target

[Service]
Restart=on-failure
ExecStart=/usr/bin/consul agent -config-dir=/etc/consul.d
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
KillSignal=SIGINT
TimeoutStopSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

Start and enable the Consul service:

sudo systemctl daemon-reload
sudo systemctl start consul
sudo systemctl enable consul

Verify Consul is running:

sudo systemctl status consul

Now we can configure the consul as backup for our terraform tfstate file

terraform {
  backend "consul" {
    address = "http://18.212.14.184:8500"
    path    = "terraform/state"
  }
}


provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example" {
  bucket = "my-unique-bucket-name-aaa-bbb-123456"
  acl    = "private"
}


output "bucket_name" {
  value = aws_s3_bucket.example.bucket
}
terraform init
terraform plan
terraform apply

Check on consul

TROUBLESHOOTING

[root@ip-172-31-0-44 consul.d]# journalctl -u consul.service
Dec 15 14:45:06 ip-172-31-0-44.ec2.internal consul[443506]: ==> failed to parse /etc/consul.d/consul.hcl: 1 error occurred:
Dec 15 14:45:06 ip-172-31-0-44.ec2.internal consul[443506]:         * invalid config key codedata_dir
Dec 15 14:45:06 ip-172-31-0-44.ec2.internal systemd[1]: consul.service: Main process exited, code=exited, status=1/FAILURE
Dec 15 14:45:06 ip-172-31-0-44.ec2.internal systemd[1]: consul.service: Failed with result 'exit-code'.
Dec 15 14:45:06 ip-172-31-0-44.ec2.internal systemd[1]: consul.service: Scheduled restart job, restart counter is at 5.
Dec 15 14:45:06 ip-172-31-0-44.ec2.internal systemd[1]: Stopped consul.service - Consul.
Dec 15 14:45:06 ip-172-31-0-44.ec2.internal systemd[1]: consul.service: Start request repeated too quickly.
Dec 15 14:45:06 ip-172-31-0-44.ec2.internal systemd[1]: consul.service: Failed with result 'exit-code'.
Dec 15 14:45:06 ip-172-31-0-44.ec2.internal systemd[1]: Failed to start consul.service - Consul.
[root@ip-172-31-0-44 consul.d]#

To troubleshoot this issue

[root@ip-172-31-0-44 consul.d]# consul validate /etc/consul.d/*
Config validation failed: failed to parse /etc/consul.d/consul.hcl: 1 error occurred:
        * invalid config key codedata_dir


[root@ip-172-31-0-44 consul.d]#

Then I changed the following parameter to data_dir = "/opt/consul“

[root@ip-172-31-0-44 consul.d]# vim /etc/consul.d/consul.hcl

Then restarted service and it got resolved

0
Subscribe to my newsletter

Read articles from Anant Saraf directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anant Saraf
Anant Saraf