Terraform Primer

In the science (fiction) community, terraforming is the act of taking a planet, moon, or other celestial body and making it suitable for human life. While a simplistic definition (more information can be found in this wikipedia article), the name is quite suitable.

In the context of IT, Terraform is a infrastructure management tool. It is stateful, which means it maintains it’s own record on how everything is supposed to look. This tool has been a crucial part in many technology organizations around the world.

Terraform allows you to express your infrastructure as code. While this term has become a bit of a buzz phrase over the last few years, managing your infrastructure as code severely optimizes your management of resources. You are now able to check your infrastructure definitions into source control. You can apply the same infrastructure instructions across multiple environments. And you can move your infrastructure from point A to B with ease.

An Example

Take the following example.

As a sysadmin you just got assigned a ticket requesting a new server for development purposes. The requester for this server has requested a standard spec sheet for the development servers that include CPU cores, memory, and storage. Before terraform, you would log into your vsphere environment, find your template by hand (search never seems to work right does it?), clone it, disable the network connection to avoid IP address conflicts, open the console, assign your IP address, add the DNS records, and finally log into your server. There goes your morning.

With terraform you could have a terraform definition ready to go. You would be using the first class vsphere provider to provision your servers. Your definition might look something like this (taken from the documentation in the provided link):

This example is enough to get you a server running on your cluster. Need 2 servers instead of 1? Add a “count” parameter to your “vsphere_virtual_machine” resource. Now you will have 2 servers. But going back to our example, we were cloning from a template right? No problem. Simply add a vsphere_virtual_machine data object to hold information about your template and instruct terraform to clone off that template by changing the vsphere_virtual_machine resource:

Note, we have added a customization section telling it what and how to clone. Now we simply run this using “terraform apply” and we have a server running. But that’s not all, we also need a DNS record so we know how to find this server on the network. Do not worry! Terraform also has a DNS provider. It can communicate with nearly any DNS server using RFC 2136 (RFC Spec and a more readable wikipedia entry).

All we need to do is add the following DNS resource and we will have our DNS setup and ready to go! (From the DNS provider documentation)

The magic happens in the “addresses” list where we told it to use whatever IP address our virtual machine is using. This means if/when the IP address for the server changes, DNS will be updated automatically. Pretty neat huh?