While I had already looked into Terraform in the past, I am actually pretty new to it, and just started now to take a better look at it and working with it
In my last post, I had a look on the concept of Terraform. Today I will start using it, to setup a simple environment on Hetzner Cloud.
Initializing and provider setup
As a first we need a provider, obviously in this case the hcloud provider. As instructed in the documentation you add the following to your project, by adding a .tf-file, usually it’s called provider.tf - the filename really does not matter, as terraform will merge all .tf-files together:
Then you run terraform init
This will download the required files to .terraform/providers/ or in this case .terraform/providers/registry.terraform.io/hetznercloud/hcloud/1.33.1/linux_amd64/terraform-provider-hcloud_v1.33.1.
However, what we are missing is the “permissions” to access the hcloud. As documented this can be configured as follows
token - (Required, string) This is the Hetzner Cloud API Token, can also be specified with the HCLOUD_TOKEN environment variable.
So you can extend provider.tf for instance as follows
The token then goes into a .tfvars-file or you can even specify it as cli parameter like -var="hcloud_token=...".
As token is sensitive, you should declare it as sensitive, which prevents Terraform from showing its value in the plan or apply output. Ultimately my provider.tf looks like this:
Create a server
To create a server we follow the documentation and create a .tf-file - e.g. example.tf - which contains the specification:
Now you can run a terraform plan, which shows you what terraform will create
If you are fine with that, run tf apply, which shows you the same info as above. Once you approve by typing yes, terraform will create the resources
If you go to your hcloud web ui, you can find the server created:
Make changes
Let’s adjust the name of the server in example.tf, plus we add some labels:
Now, running terraform plan will tell you what it will change. Certain changes are “in-place”
After running terraform apply we can observe, that the node name has changed and the server also has the specific label associated
The terraform state
Let’s have a quick look at the terraform.tfstate file. This currently looks like this
What happens if we do some manual changes on the server, e.g. by manually adding a label in the UI
If you run terraform plan, it detects the changes, and will tell you that this label will be removed, while terraform apply will definitively remove it:
Terraform destory
If you don’t need your stuff anymore, you just can run terraform destory which ultimately will delete all resources you manage with terraform.
Having done, that I can visually confirm that this has happened:
A look at the terraform.tfstate also shows, that nothing is managed anymore with terraform
Summary and next steps
This was a basic example on how to create, manage and destroy resources in [hecloud] using terraform. Obviously there are much complexer environments to manage, than a single server. Also interesting is, how to start managing resources, which you previously managed manually. I will cover that in one of my next posts.