Software engineering, open source, community and passion
Migrating From Dokku to Kamal: Provisioning with Terraform
I have a
2 GB Memory, 1 vCPU, 50 GB Disk VPS on Digital Ocean which cost me 12 USD per month. Comparing the prices
with Hetzner I noticed I could have 3x
4 GB, 2 vCPU Arm64, 40 GB Disk for 13.53 EUR per month. That's a lot more power for almost the same
price.
The droplet is running a Rails application with Dokku. I really enjoyed my time with Dokku, it makes our lives so
much easier when setting up a VPS from scratch. The major downside of Dokku is the lack of support to multi-host
though.
After the introduction of Kamal I thought in giving it a try, but this time using Hetzner instead of Digital Ocean.
Provisioning Resources on Hetzner with Terraform
Since I would be creating new servers on Hetzner, I asked ChatGPT to help me doing that with Terraform so the whole
process could be easily repeatable.
After some back and forth I ended up with the following script on terraform/main.tf:
The script above does a couple of things:
define a few variables so the Terraform script doesn't hold any hard coded sensitive data, namely the Hetzner api token, the fingerprint of the ssh key associated with my account on Hetzner and my ssh private key
define the resources to be managed on the german provider: 3 VPSs of the type CAX11, named web, worker and db
create and give permission to the file /letsencrypt/acme.json only on the web server as it will be used ahead to hold Let's Encrypt certificate
change default DNS and enable DNSOverTLS when supported
print the ipv4 of the created resources
update the .env file with the ipv4 of the created servers. This file will be used by Kamal to set up the environment variables on the servers
remove the ipv4 of the created resources from the .ssh/known_hosts file, this can be left out in case you are not applying and destroying the resources on Hetzner often.
define the versions of Terraform and the provider (boilerplate code required by Terraform)
Once you have uploaded your public ssh key to Hetzner, you can copy the fingerprint by visiting Security -> SSH keys on their website.
By visiting Security -> API tokens you can generate an api token that will allow Terraform to manage resources on your Hetzner account.
The path to your private key is required so Terraform can ssh into the servers on your behalf.
Once you have everything in place and have installed Terraform on your machine you can run:
to initialize the terraform thing and:
to create the resources on Hetzner. Notice you gotta prepend TF_VAR_ on the name of the variables defined in the script.
In case you want to destroy the resources created you can run:
In the next post of the series I share how Kamal has been used to set up the servers, including Let's Encrypt on the web.