×


Install Terraform on Ubuntu 18.04 / 20.04 LTS - Step by step process ?

Terraform is an infrastructure as a code platform developed by HashiCorp.

You can simply write code in the human-readable format following HashiCorp Configuration Language (HCL) and deploy it to get the infrastructure in the cloud. 

Terraform is supported in many cloud providers like Google, Amazon, Alibaba, and so on.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Ubuntu related queries.

In this context, we shall look into how to install Terraform on Ubuntu Linux System.


How to Download and Install Terraform on Ubuntu ?

To begin, we need to download the Terraform compressed file from Terraform's download page. Terraform 0.14.6 is the most recent version by the time of publishing this tutorial.

To download Terraform, use the wget command to grab the Linux installation zipped file:

$ sudo wget https://releases.hashicorp.com/terraform/0.14.6/terraform_0.14.6_linux_amd64.zip

Once the download is complete, confirm that the zipped file exists using the ls command as shown:

$ ls | grep terraform

Next, unzip the file to the /usr/local/bin path where non-system programs are usually located:

$ sudo unzip terraform_0.14.6_linux_amd64.zip -d /usr/local/bin

Once uncompressed, the installation of Terraform is complete. To verify the version of Terraform installed issue the command:

$ terraform version

Further, you will learn how we can use Terraform to provision an ec2 instance on AWS cloud platform.


How to use Terraform to launch an EC2 instance on AWS ?

Terraform can be used to deploy on-premise and cloud resources on popular cloud platforms such as AWS and Google cloud to mention a few.

To start off, we are going to create a separate directory for our AWS project and navigate into it:

$ mkdir aws && cd aws

We will create a Terraform configuration file bearing a .tf extension:

$ sudo vim config.tf

Now note the following when dealing with AWS.

1. Cloud provider's name – In this case, AWS.

2. Access key & secret keys – These are encrypted keys that provide access to our AWS resources

3. Region – the location where the cloud infrastructure will be provisioned. In our case, this will be in us-east-2

4. ami – This is a unique ID required to deploy an EC2 instance.


Then, Copy and paste the following content:

#Specify the access & secret keys and region
provider "aws" {
access_key = "Your-own-access-key"
secret_key = "Your-own-secret-key"
region = "us-east-2"
}
#Specify details of the ec2 instance 
resource "aws_instance" "instance1" {
ami = "ami-0dd9f0e7df0f0a138"
instance_type = "t2.micro"
tags = {
Name = "ubuntu-20.04"
}
}

To start working with Terraform and running command, we are going to initialize it as shown below:

$ terraform init

To perform a simulation of how the command will run, invoke the terraform plan command. 

Note that this is only a simulation that is meant to see how the whole thing will play out:

$ terraform plan

Finally, to provision the ec2 instance, run:

$ terraform apply

A flurry of information will be printed on the terminal displaying the various actions that will be performed by Terraform

When prompted whether to proceed with the execution of the actions, simply type 'Yes' and hit ENTER.

Within a few seconds, the creation of your ec2 instance will be complete.

And just to sure that your instance was created, head over to our AWS account and navigate to the EC2 instances.


How to Remove terraform ?

If you want to remove terraform you can simply delete the 'terraform' file kept at /usr/local/bin/:

$ sudo rm -f /usr/local/bin/terraform

Also, you can clean your directory where you have initialized terraform.

In our case, we can delete all the content of the folder 'aws'.


[Need urgent assistance in fixing missing packages on Ubuntu Linux System? We are available to help you today. ]


Conclusion

This article covers how to install Terraform on Ubuntu. Terraform is an infrastructure automation tool which allows you to define and describe your infrastructure as code inside configuration files using a declarative language and to deploy and manage that infrastructure across a variety of public cloud providers like AWS, GCP, Azure, and so on.


To Install Terraform on Ubuntu:

1. First, create ~/bin directory:

$ mkdir ~/bin

2. Next, download the zip archive. Visit the Terraform download page for the latest version to download:

$ wget https://releases.hashicorp.com/terraform/0.12.24/terraform_0.12.24_linux_amd64.zip

3. Unzip the archive. 

The archive will extract a single binary called terraform.

$ unzip terraform_0.12.24_linux_amd64.zip

4. Move the terraform binary to a directory included in your system's PATH in our case that's ~/bin directory.

$ mv terraform ~/bin

5. To check whether Terraform is installed, run:

$ terraform version