In this blog post I am going to introduce Terraform and install it.
Terraform is a tool to help build infrastructure from code, i.e. it takes a terraform file and then creates / destroys infrastructure as required. Terraform was created by Hashicorp, the same company behind Vagrant and Packer. Terraform is written in Go and uses the APIs of cloud provides (e.g. AWS, Azure, Google Cloud) to provide a uniformed approach to creating, managing and destroying infrastructure via Infrastructure As Code (IaC).
Doesn’t Ansible do the same?
I think Ansible is a fantastic tool and I have not even scratched the surface of what it can be used for. However, Ansible is generally considered as a configuration management tool where as Terraform is considered a provisioning tool.
For example, take a look at the Ansible play I used in “creating multiple EC2 instances“. During this Ansible play I used the count option to create 2 instances. A similar count option is available in Terraform. Running the Ansible playbook gives me 2 EC2 instances. Running Terraform code gives me 2 EC2 instances. So far it seems like Ansible and Terraform are pretty similar but what happens if I rerun them again. Ansible would create 2 more EC2 instances so I now have 4 Ansible EC2 created instances and Terraform would not as it would check and see that I already have the 2 EC2 instances I asked for. What happens if I wanted to reduce the number of instances? In Terraform I can adjust the code to say count=1, in Ansible I would need to write a new playbook (or amend the original playbook) to remove the unwanted instance, or instances if the original playbook has been run more than once.
So Terraform instead of Ansible?
I see it more as the two can work together. Terraform is for provisioning, Ansible is for configuration management, e.g. Terraform can be used to provision (deploy) infrastructure like AWS EC2 instances and then Ansible can be used to configure them.
Terraform does not require an agent/client to be installed on the end device (e.g. the AWS instances), however Terraform does require installing on a device to send commands from.
Terraform is available for download for Windows, MacOS and Linux.
I am going to install Terraform on my Raspberry Pi running Raspberry Pi OS (32-bit). On the Terraform download page I went with the Linux Arm download. I also download the sha256 checksum value.
Once the Terraform download has finished, run the command:
Replacing FILENAME with the name of the Terraform download, after a few seconds the command will output a sha256 value – this should match the checksum provided by Terraform.
With matching sha256 checksums, unzip the Terraform download and you should have a binary file called “terraform”.
Using the command:
sudo mv terraform /usr/local/bin/
To move the terraform file to the /usr/local/bin directory. Note: I had to sudo up to do this, depending on your account permissions you may not need to.
So that Terraform can autocomplete (using the tab key) commands, use the command:
And to check that Terraform installed ok use the command:
Note: After installing the autocomplete a restart may be needed.
At the time of writing (July 2020) Terraform is in a pre-version 1 release, i.e. it’s on version 0.12.29.
will show the version installed.