How to Install Terraform on Ubuntu 18.04

07 July 2020 | Category : Ubuntu

How to Install Terraform on Ubuntu 18.04

What is Terraform?

The Infrastructure Server today has been evolved we can build the infrastructure with a code, this is called with name Infrastructure as a Code (IaaC) we know the tools are popular in IaaC is Ansible and Terraform. In the last post, we have posted about Ansible and now we will share about Terraform. Terraform is one of IaaC Tools that is frequently used to build, develop, and manage the Infrastructure Server. Terraform created or developed by Hashicorp, this is written with Go programming language. Terraform can manage Infrastructure on Cloud Providers such as Digital Ocean, Vultr, Amazon Web Services (AWS), Microsoft Azure, Google Cloud Platform (GCP) and many others, this tool also can manage on your private cloud which builds with Openstack, OpenNebula or other.

In this article, we will learn how to install Terraform on Ubuntu 18.04 and how to get started with Terraform.

Install Terraform on Ubuntu 18.04

To install Terraform on Ubuntu 18.04 we have two ways. First, we can install from snap package manager, and the second way we can install from the binary file which downloads from the official repository/website.

  1. Install from snap

The first way to install Terraform on Ubuntu 18.04 we can use snap package manager. This command similar apt command, you can read more about snap right here https://snapcraft.io/

Before to installation step make sure if snap has been installed, you can run this command to install or to make sure snap has been installed or not :

apt install snap -y

If the snap command has been installed you can run this command to install terraform on your ubuntu :

snap install terraform
2020-07-28T14:27:37Z INFO Waiting for automatic snapd restart...
terraform v0.11.11 from Nathan Handler (nhandler) installed
  1. Install from Binary File

Another way to install terraform is installing from the binary file, it’s mean you need to download the binary file from the repository or official page terraform. To do the installation from binary you can follow this step.
Before we start the installation, we need to install the dependency package that is needed when installing terraform and download the binary file of terraform, to do that you can follow this command :

apt install -y wget unzip
wget https://releases.hashicorp.com/terraform/0.12.29/terraform_0.12.29_linux_amd64.zip

After finished installation of the dependency and download binary file, next you can extract the file archive of the terraform and move it file binary to folder /usr/local/bin. To do that you can follow this command bellow :

    unzip terraform_0.12.29_linux_amd64.zip
    mv terraform /usr/local/bin/

Finally, if the terraform has been installed to check installation you can run this command to ensure the installation has been successful.

terraform --version
Terraform v0.12.29

Getting Started

Now, you have been installed terraform on your ubuntu to started using terraform to build an infrastructure of some project, for example, webserver on some cloud you need to create file configuration with extension .tf. To build the VM/Server of webserver we can create file main.tf as the configuration file terraform to build the VM/Server of the webserver.

  • Create main.tf configuration

The configuration line of main.tf you can fill with this script configuration :

vim main.tf
      provider "opennebula"{
        endpoint = "${var.one_endpoint}"
        username = "${var.one_username}"
        password = "${var.one_password}"

        version = "~> 0.1"
      }

      resource "opennebula_virtual_machine" "instance" {
        template_id = "${var.vm_template_id}"
        name        = "${var.vm_name}"
        cpu         = "${var.vm_cpu}"
        vcpu        = "${var.vm_vcpu}"
        memory      = "${var.vm_memory}"
        permissions = "660"

        context = {
          NETWORK   = "YES"
          HOSTNAME  = "${var.vm_name}"
          SSH_PUBLIC_KEY = "${file("~/.ssh/id_rsa.pub")}"
        }

        disk {
          image_id = "${var.vm_image_id}"
          size = "${var.vm_disk}"
          target = "${var.vm_disk_target}"
        }

        nic {
          model       = "virtio"
          network_id  = "${var.vm_net_public}"
        }

        nic {
          model       = "virtio"
          network_id  = "${var.vm_net_private}"
        }

      }

      output "vm_ip" {
        value = "${opennebula_virtual_machine.instance.ip}"
      }

Note: In the above, you can see the line with variable ${var<name>} which means right there you can fill with your variable configuration. This configuration use OpenNebula as a cloud provider means this is to use a private cloud. If you want to use AWS, DigitalOcean, GCP, or other you can change the provider in the first line to the provider name of Cloud Provider.

  • Initialize configuration

After you create the main.tf to run the configuration you can execute this command first to initialize:

    terraform init

That command will be showing the result like this :

    terraform init

Initializing the backend...

Initializing provider plugins...

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
    
  • Check the plan configuration

After successful initialize and then you can run the common below :

    terraform plan

This command to check the plan of VM/Server which will be you creating also simulate create VM/Server without actually not creating anything on OpenNebula or Cloud Provider, and will be showing the result :

    terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # opennebula_virtual_machine.instance will be created
  + resource "opennebula_virtual_machine" "instance" {
      + context     = {
          + "HOSTNAME"       = "webserver.linux-id.net"
          + "NETWORK"        = "YES"
          + "SSH_PUBLIC_KEY" = <<~EOT
            ssh-rsa AAAAB3NzaC1yc2EAAvyJOlo2ooKGci+0k6VD7d61EsZYLRHavHv/R9OVKG7AXV5QImaUD8TggTXQwgDWMmwUxbSXEd1NgorABSjHNInbE93Lruvc5gMjCc5bMibQxHRyl9BBiEs/d9llWWcd69W4DMHoXGExJ0Kw== me@linux-id.net
            EOT
        }
      + cpu         = 1
      …
      ...
      + memory      = 2048
      + name        = "webserver.linux-id.net"
      + pending     = false
      + permissions = "660"
      ...
      + template_id = 47
      ...
      + vcpu        = 1

      + disk {
          + driver   = (known after apply)
          + image_id = 199
          + size     = 20000
          + target   = "vda"
        }
      …
      ...
      + nic {
          + ip              = (known after apply)
          + mac             = (known after apply)
          + model           = "virtio"
          + network         = (known after apply)
          + network_id      = 8
          ...
        }
      + nic {
          + ip              = (known after apply)
          + mac             = (known after apply)
          + model           = "virtio"
          + network         = (known after apply)
          + network_id      = 6
          ...
        }
        ...
   }
...

  • Apply the configuration

Then if the plan of VM/Server it’s alright, you can apply that plan with the command :

    terraform apply

And after running apply command it will need to take confirmation action and will show the prompt like below :

    
      Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.

      Enter a value: yes
    
  

Type 'yes' and then press Enter to continue process creating VM/Server

  • Checking the IP of VM/Server

If the process of creating VM/Server finished you can run this command to check the IP of VM on terraform output :

    terraform output
vm_ip = xxx.xxx.xxx.xxx
    

Right there on output message showing the IP of VM/Server webserver

  • Destroy the VM/Server

To destroy the VM/Server with terraform you can run this command :

    terraform destroy

And after running that command it will need to take confirmation action to destroy the VM/Server like below :

    
      Do you really want to destroy all resources?
      Terraform will destroy all your managed infrastructure, as shown above.
      There is no undo. Only 'yes' will be accepted to confirm.

      Enter a value: yes
      opennebula_virtual_machine.instance: Destroying... [id=1253]
      opennebula_virtual_machine.instance: Still destroying... [id=1253, 10s elapsed]
      opennebula_virtual_machine.instance: Destruction complete after 11s

      Destroy complete! Resources: 1 destroyed.
    
  

Type 'yes' and then press Enter to destroy the VM/Server

Conclusion

Using Terraform will be helping the System Administrator or DevOps to build the Infrastructure Server of some projects. Terraform command one of a popular tool to manage infrastructure, like Ansible command. Terraform can be integrated into several Cloud Providers such as AWS, GCP, Linode, Vultr, DigitalOcean, and others.

Share this article :