For the last few days I have been working on updating my Consul deployment to run on Ubuntu 18.04 instead of an AmazonLinux instance. The migration was fairly straight forward, but I ran into two small issues that I figured that I would share here.

The first issue was around updating my startup script from System V style init script to a systemd style script. My first pass of the script seemed to work ok, but there were a few things wrong with it.

[generic]  
[Unit]  
Description=Consul  
Documentation=https://www.consul.io/

[Service]  
LimitNOFILE=1024  
ExecStart=/opt/consul/bin/consul agent -config-dir=/etc/consul.d/  
ExecReload=/bin/kill -HUP $MAINPID

[Install]  
WantedBy=multi-user.target  
[/generic]

The biggest problem with the startup file was that it ran the consul process as root. It also had a little bit of an issue with the node name. After a little trial and error, I was finally able to come up with a script that would start it up as the proper user and set the node name properly.

[generic]  
[Unit]  
Description=Consul  
Documentation=https://www.consul.io/

[Service]  
PermissionsStartOnly=true  
User=consul  
Group=consul  
LimitNOFILE=1024  
ExecStartPre=/bin/bash -c “/bin/systemctl set-environment HOSTNAME=$(hostname)ExecStart=/opt/consul/bin/consul agent -node $HOSTNAME -config-dir=/etc/consul.d/  
ExecReload=/bin/kill -HUP $MAINPID  
KillSignal=SIGINT

[Install]  
WantedBy=multi-user.target  
[/generic]

The next issue was around the sending autoscaling the signal to the stack that the machines are up. The Canonical provided instances does not have the cfn-bootstrap helper apps, so my original UserData was not able to send the signal causing my CloudFormation to fail.

[shell]  
UserData:  
Fn::Base64: !Sub |  
#!/bin/bash -xe  
/usr/local/bin/cfn-signal -e 0 –stack ${AWS::StackName} –resource ConsulASG –region ${AWS::Region}  
[/shell]

Getting it to work was really easy. All I needed to do was install the bootstrap tools first. Thankfully, it was as easy as adding a line to UserData to run a pip command first.

[shell]  
UserData:  
Fn::Base64: !Sub |  
#!/bin/bash -xe  
sudo pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz  
/usr/local/bin/cfn-signal -e 0 –stack ${AWS::StackName} –resource ConsulASG –region ${AWS::Region}  
[/shell]

That’s it.  My CloudFormation template successfully builds my cluster in AWS.  I’ve also update my Ansible Galaxy Role for Consul with the new startup script.