How to Install Terraform on an Ubuntu machine

Many automation tools and scripts are available in the market, but one of the most widely used and easy to use is Terraform, also known as Infrastructure as a code tool.

In this tutorial, you’ll install Terraform on Ubuntu 20.04. You’ll then work with Terraform to create a service on Amazon Managed Service. So let’s get started.

Join 53 other followers

Table of Content

  1. What is Terraform?
  2. Prerequisites
  3. How to Install Terraform on Ubuntu 20.04 LTS
  4. Terraform files and Terraform directory structure
  5. Terraform ec2 instance example (terraform aws ec2)
  6. Conclusion

What is Terraform?

Terraform is a tool for building, versioning, and changing the infrastructure. Terraform is Written in GO Language, and the syntax language of configuration files is HashiCorp configuration language(HCL) which is much easier than yaml or json.

Terraform is used with various cloud providers such as Amazon AWS, Oracle, Microsoft Azure, Google Cloud, etc.

Prerequisites

  • Ubuntu machine preferably 18.04 version + , if you don’t have any machine you can create a ec2 instance on AWS account. Recommended to have 4GB RAM and at least 5GB of drive space
  • Ubuntu machine should have IAM role attached with AWS EC2 instance creation permissions or admin rights.

You may incur a small charge for creating an EC2 instance on Amazon Managed Web Service.

How to Install Terraform on Ubuntu 20.04 LTS

Now that you have a basic idea about terraform let’s kick off this tutorial by first installing terraform on Ubuntu 20.04 machine.

  • First log in to the Ubuntu machine sing your favorite SSH client such as putty.
  • Next, update the existing system packages on the ubuntu machine by running below command.
sudo apt update
  • Now, download the latest version of Terraform in the opt directory. You can install Terraform on any directory but it is recommended to use opt directory for software installations.
wget https://releases.hashicorp.com/terraform/0.14.8/terraform_0.14.8_linux_amd64.zip
  • Further install zip package which you will need in next step to unzip Terraform zip file.
sudo apt-get install zip -y
  • Now, unzip the Terraform download zip file from the unzip command.
unzip terraform*.zip
  • Next, move the Terraform executable file to executable directory such as /usr/local/bin so that you can run Terraform from any directory of your Ubuntu machine.
sudo mv terraform /usr/local/bin
  • Finally, verify the Terraform installation by running terraform command or terraform version command.
terraform  # To check if terraform is installed 

terraform -version # To check the terraform version </mark> 
Checking the Terraform installation
Checking the Terraform installation
  • This confirms that terraform has been successfully installed on ubuntu 18.04 machine.
Checking the Terraform installation by running terraform version command
Checking the Terraform installation by running terraform version command

Terraform files and Terraform directory structure

Now that you have Terraform installed. Let’s now dive into Terraform files and Terraform directory structure that will help you write the Terraform configuration files later in this tutorial.

Terraform code, that is, Terraform configuration files, are written in a tree-like structure to ease the overall understanding of code with .tf format or .tf.json or .tfvars format. These configuration files are placed inside the Terraform modules.

Terraform modules are on the top level in the hierarchy where configuration files reside. Terraform modules can further call another child to terraform modules from local directories or anywhere in disk or Terraform Registry.

Terraform contains mainly five files as main.tf , vars.tf , providers.tf , output.tf and terraform.tfvars.

  1. main.tf – Terraform main.tf file contains the main code where you define which resources you need to build, update or manage.
  2. vars.tf – Terraform vars.tf file contains the input variables which are customizable and defined inside the main.tf configuration file.
  3. output.tf : The Terraform output.tf file is the file where you declare what output paraeters you wish to fetch after Terraform has been executed that is after terraform apply command.
  4. .terraform: This directory contains cached provider , modules plugins and also contains the last known backend configuration. This is managed by terraform and created after you run terraform init command.
  5. terraform.tfvars files contains the values which are required to be passed for variables that are refered in main.tf and actually decalred in vars.tf file.
  6. providers.tf – The povider.tf is the most important file whrere you define your terraform providers such as terraform aws provider, terraform azure provider etc to authenticate with the cloud provider.

Terraform ec2 instance example (terraform aws ec2)

Let’s wrap up this ultimate guide with a basic Terraform ec2 instance example or terraform aws ec2.

  • Assuming you already have Terraform installed on your machine.
  • First create a folder terraform-demo in opt directory. This folder will contain all the configuraion file that Terraform needs to build ec2.
mkdir /opt/terraform-demo
cd /opt/terraform-demo
  • Now create main.tf file under terraform-demo folder and copy/paste the content below.
resource "aws_instance" "my-machine" {          # This is Resource block where we define what we need to create

  ami = var.ami                                 # ami is required as we need ami in order to create an instance
  instance_type = var.instance_type             # Similarly we need instance_type
}

  • Create one more file named vars.tf file under terraform-demo folder and copy/paste the content below. The vars.tf file contains the variables that you referred in main.tf file.
variable "ami" {                       # We are declaring the variable ami here which we used in main.tf
  type = string      
}

variable "instance_type" {             # We are declaring the variable instance_type here which we used in main.tf
  type = string 
}
  • Create one more file output.tf file under terraform-demo folder and paste the content below. This file will allow Terraform to display he output after running terraform apply command.
output "ec2_arn" {
  value = aws_instance.my-machine.arn    
}  
  • Create provider.tf file under terraform-demo folder and paste the content below.
provider "aws" {     # Defining the Provider Amazon  as we need to run this on AWS  
  region = "us-east-2"
}
  • Create terraform.tfvars file under terraform-demo folder and paste the content below. This file contains the value of Terraform vaiables declared in vars.tf file.
ami = "ami-013f17f36f8b1fefb" 
instance_type = "t2.micro"
  • Now, run tree command which will provide you the folder structure. You should see something like below.
tree command
tree command
  • Now your files and code are ready for execution. Initialize the terraform using the terraform init command.
terraform init
Initializing the terraform using the terraform init command.
Initializing the terraform using the terraform init command.
  • Terraform initialized successfully , now its time to run the plan command which provides you the details of the deployment. Run terraform plan command to confirm if correct resources is going to provisioned or deleted.
terraform plan
Running the terraform plan command
Running the terraform plan command
Running the terraform plan command
Running the terraform plan command
  • After verification, now its time to actually deploy the code using terraform apply command.
terraform apply
Running the terraform apply command
Running the terraform apply command
Output of the terraform apply command
The output of the terraform apply command

Great Job; terraform commands were executed successfully. Now you should have the AWS EC2 instance launched in AWS Cloud.

It generally takes a minute or so to launch an instance, and yes, you can see that the instance is successfully launched now in the us-east-2 region as expected.

Join 53 other followers

Conclusion

In this tutorial, you learned What is terraform, how to Install Terraform on the Ubuntu machine and launch an ec2 instance on an AWS account using terraform. Keep Terraforming !!

Now that you have the AWS EC2 instance launched, what are you planning to deploy on the newly created AWS EC2?

Advertisement

7 thoughts on “How to Install Terraform on an Ubuntu machine

  1. Pingback: How to Setup AWS WAF and Web ACL using Terraform on Amazon Cloud

  2. Pingback: How to Launch AWS Elasticsearch using Terraform in Amazon Account

  3. Pingback: Learn Terraform: The Ultimate terraform tutorial [PART-1] | Automateinfra

  4. Pingback: How to create Secrets in AWS Secrets Manager using Terraform in Amazon account. | Automateinfra

  5. Pingback: How to Launch multiple EC2 instances on AWS using Terraform count and Terraform for_each | Automateinfra

  6. Pingback: How to Launch AWS Elastic beanstalk using Terraform | Automateinfra

  7. Pingback: How to Install and Setup Terraform on Windows Machine step by step | Automateinfra

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s