Terraform Brew

A few words about Terraform

I have installed Terraform v0.11.13 via homebrew and as recommended by terraform I want to ugprade to version v0.11.14 before doing the major upgrade to v0.12.0. When I run brew upgrade terraform or download the Mac package from the terraform website it would immediately update my terraform version to v0.12.0 I think. Terraform v0.11.14 The brew pin command will prevent Homebrew from updating/upgrading your version of Terraform when you run the brew upgrade command. I would strongly suggest pinning Terraform because otherwise, the brew upgrade command will remove all older versions of Terraform from your system. $ brew pin terraform. Install Terraform (using Brew) Terraform is latest version available (as the time of writing) of Terraform is 0.12. Terraform 0.12 coding syntax may be different and most example you find are the internet are written for 0.11 and before. For this reason I decided to install both versions of Terraform and switch between them when I require this.

If you have read my last blog post you already know how to create templates for further provisioning using the Cloud-Init specification. In this post I like to show you how you easily deploy your infrastructure using Terraform on the virtualisation solution Proxmox. First of all, what is Terraform exactly? Terraform is a so called 'Infrastructure as a code' software developed by Hashicorp - a company which also created Vagrant and other famous tools for professional cloud solutions. According to Microsoft,

Infrastructure as Code (IaC) is the management of infrastructure (networks, virtual machines, load balancers, and connection topology) in a descriptive model, using the same versioning as DevOps team uses for source code.

What's the benefit you may ask. One of the biggest advantages is that you don't have to install your servers and other components manually. Instead, with just a few lines of code you're able to manage your complete infrastructure. This automation step brings your stability, quality improvements, resilience and time for more important things. Furthermore, you have the possibility to integrate your Terraform configuration into your existing VCS and automatically run changes through your infrastructure with a proper CI/CD pipeline.

Another good question - why use Terraform if you've already have Ansible in place? Have you ever tried to remove a package in Ansible? If you just remove it from you playbook configuration it is still on your server until you write the right configuration (the attribute 'absent' is your friend) or remove it manualy. Terraform takes care on this changes and has a clever dependency management for 'First this, then that' decisions. Writing about the differences between the two would hijack my 'Terraform practicing' article, so to keep a long story short:

  • Use Terraform for all your infrastructure stuff (VMs and the baseline software on it)
  • Use Ansible for the configuration management (Installation of Applications and the configuration of it)

Let's start with practicing

Assumptions

  • You have already a Proxmox server installed (it works with other virtualisation and cloud providers too - but this article is about Proxmox and Terraform)
  • You have already OS-templates with Cloud-init defined (if not, please read my blog post about it)

1. Installation of Terraform

You can install Terraform on the major plattforms using either the package manager/app store from the chosen system or download the binaries from Hashicorp directly. On OSX I used Homebrew, which is a package manager for OSX.

After the installation let's check the Version of your Terraform Version. Please use Version 12.x upwards, because there have been many changes between 11.x and 12.x

2. Installation of the Terraform Proxmox provider

The Proxmox provider is not a default one - a so called 3rd party provider. Thus, it's necessary to install it manually. As prerequisite you have to install the Golang progamming language on your system. Golang is available on all major plattforms. For the installation I used Homebrew again.

The redball day!redball. With Go installed you're able to compile the provider using the commands

3. Configure your first Terraform project

We have installed the Terraform software in step 1 and the necessary Proxmox provider in step 2. Now we're going to configure our first Terraform infrastructure which runs on Proxmox. In my example, I like to install a small Kubernetes infrastructure which should run K3s (a lightweight Kubernetes distribution) later on. For that we have to define the following environment:

  • 1 x Master Server
  • 2 x Node Server
  • 1 x Storage Server (for persistent storage)
Terraform brew m1

Configure the Proxmox provider

First, we configure the connection settings for the Proxmox provider. For better readability of our infrastructure code, we split variables and provider in two different configuration files named 'variables.tf' and 'provider.tf'

Configure the virtual machines

Next step is the main configuration of our k3s-cluster server. Here you have to adapt the following attributes according to your configuration:

  • target_node (the name of your Proxmox instance)
  • name (the name of the virtual server)
  • clone (the name of the template in Proxmox)
  • cores
  • memory
  • storage (the right storage pool in Proxmox)
  • ipconfig0 (Use the right IP range for your servers - the count.index is necessary if you have more then one server configured - like the k3s_agents in the example below)

The 'ignore changes' lifecycle block is necessary, because Terraform likes to change the mac address on the second run - maybe a problem in the Proxmox provider - see here: https://github.com/Telmate/terraform-provider-proxmox/issues/112

Terraform Previous Versions

Add ssh-pubkey for Cloud-Init

To get passwordless login (useful for tools like Ansible), create a variable with your ssh_key in the variables.tf file.

Deployment Time

Terraform has a simple but powerful deployment cycle, which consists of the following steps:

  • Init - Initializes the Terraform project and install needed plugins, dependencies..
  • Validate - Validates the syntax of the created Terraform .tf files
  • Plan - Calculates the steps and changes to install/upgrade your infrastructure
  • Apply - Applies the changes on the configured systems

Upgrade Terraform Version

If you try to skip a step for example start with terraform plan, Terraform inform you to initialize the project first:

So let's start with the initialization first.

And Terraform informs you about the next step. But instead we like to check if our configuration ist correct:

Seems we did a good job during our configuration. Now it's the time to see what Terraform likes to deploy:

As you can see - Terraform likes to install 4 new server. It also shows us the detailed configuration. The configuration can be read like a 'diff' file:

Install Terraform

  • + means add
  • - means remove
  • ~ means replaced

The file 'planfile' can be used for the next apply command:

Depending on your hardware this needs some time. If everything runs fine you can see the output above. Terraform successfully created 4 new ressources, which you can use for install the K3s cluster. Because we don't like to install anything manually we will use Ansible for this job.

Terraform Prevent Resource From Destroying

But this is another story I have to tell ;-)

Terraform Prevent Destroy

Wish all of you a happy new year!