In this guide, we will show you how to install and use Terraform on CentOS 8. Before we proceed further, what is Terraform? Created by Hashicorp, Terraform is a free and opensource declarative coding tool that allows you to automate and manage your IT infrastructure and various services that run on servers. In fact, Terraform is popularly referred to as ‘Infrastructure as a Code’ tool.
Terraform makes use of a simple syntax to efficiently and safely provision resources across on-premise and cloud platforms such as Microsoft Azure, Google Cloud Platform and AWS. Where required, it can also re-provision these changes in response to changes in configuration.
Without much further ado, let us walk you through the installation steps.
Installation of Terraform on CentOS 8
First up, head over to the official Terraform download site and download the latest zip file. By the time of writing down this guide, the latest version is Terraform 0.13.3. To download use the wget command as shown
[[email protected] ~]$ wget https://releases.hashicorp.com/terraform/0.13.3/terraform_0.13.3_linux_amd64.zip
Once downloaded, unzip the file to the /usr/local/bin path using the -d switch as shown.
[[email protected] ~]$ sudo unzip terraform_0.13.3_linux_amd64.zip -d /usr/local/bin
[[email protected] ~]$
Alternatively, you can locally unzip the file in your current working directory and later move the unzipped directory to the /usr/local/bin destination.
[[email protected] ~]$ unzip terraform_0.13.3_linux_amd64.zip
[[email protected] ~]$ mv terraform /usr/local/bin
To confirm that everything went as expected, invoke the following command:
[[email protected] ~]$ terraform -v
[[email protected] ~]$
And that’s it! We are done installing Terraform. The output confirms that Terraform is successfully installed on our system. As you can see, installing Terraform is quite a simple and straightforward procedure.
Terraform in action – Deploying a VM in GCP
To get a better understanding of how Terraform can be used to provision resources, we are going to demonstrate how to deploy a vm on Google cloud.
But first, you need to have a Google Cloud account with billing enabled. Usually, you get $300 worth of free credit during your free trial. In this demo, we are using a free trial.
Once you have logged in, click on the cloud shell icon as shown
This will initialize the Google cloud shell at the bottom of your screen. This usually takes a few seconds.
Next, we are going to install Terraform locally using docker to make it more convenient. To make it more persistent on restarts, we will install it into $HOME/bin as shown.
$ docker run -v $HOME/bin:/software sethvargo/hashicorp-installer terraform 0.13.3
$ sudo chown -R $(whoami):$(whoami) $HOME/bin/
Next, add bin to the path as shown
$ export PATH=$HOME/bin:$PATH
At this point, terraform is installed. Next, you need to enable the Cloud Engine API to make the API available for use.
$ gcloud services enable compute.googleapis.com
We are going to download a terraform configuration file from Github. The configuration file initializes a compute instance (virtual machine) that installs Apache webserver with a custom configuration. The compute engine is assigned a unique name and an external IP address that you will use to access the webserver. To download the config file, run:
$ curl -sSfO https://raw.githubusercontent.com/sethvargo/terraform-gcp-examples/master/public-instance-webserver/main.tf
Use cat command to view the contents of main.tf file
$ cat main.tf
Here’s just a snippet of the …