How to Install Terraform on Ubuntu 22.04

Written by: Bobbin Zachariah   |   Last updated: July 16, 2022

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, etc.

Here in this guide, we are going to install the latest version of terraform on Ubuntu. We are performing terraform installation on Ubuntu 22.04. You can do the same procedure on all other Linux Distributions.

Also, learn how to use terraform with a simple example by launching an ec2 instance and create s3 bucket.

Prerequisites

  • A system running Ubuntu 22.04.
  • A user account with sudo privileges.

Install Terraform on Ubuntu

Download the latest version of Terraform from the official website. At the time of writing the guide, the latest version is 1.2.5.

Downloading terraform to the current folder:

$ wget https://releases.hashicorp.com/terraform/1.2.5/terraform_1.2.5_linux_amd64.zip

Now, unzip the download file:

$ sudo apt install zip -y
$ sudo unzip terraform_1.2.5_linux_amd64.zip

Move the terraform binary file to /usr/local/bin directory:

$ sudo mv terraform /usr/local/bin/

Check the terraform version:

$ terraform version
check terraform version
Check terraform version from the command line

How to use Terraform

Let's explain how to use terraform with basic examples.

Launching ec2 instance and creating an s3 bucket

I would like to create a folder and do everything inside it.

$ mkdir aws && cd aws

Create a configuration file for terraform having extension 'tf'

vi configuration.tf

Now you need to provide the following information:

  • Provider: Cloud provider, AWS in our case
  • Access, Secret key: Access to AWS resources
  • Region: The region where you are going to provision your infrastructure. I am doing it in Oregon.

On the second block of the code define AWS instance, ie ami (check amazon EC2 AMI Locator), instance type, and tag.

The last part of the following code will create s3 bucket named 'bucket-launched-using-terrafrom-20210106'. Remember that the bucket name must be unique over the AWS.

Copy the following content paste in the file configuration.tf file. Provide access, secret key, region, bucket name of your own.

#Define keys and region
provider "aws" {
access_key = "YOUR-ACCESS-KEY"
secret_key = "YOUR-SECRET-KEY"
region = "us-west-2"
}
#Define ec2 instance 
resource "aws_instance" "instance1" {
ami = "ami-089668cd321f3cf82"
instance_type = "t2.micro"
tags = {
Name = "ubuntu-22.04"
}
}
#Define s3 bucket
resource "aws_s3_bucket" "bucket1" {
bucket = "bucket-launched-using-terrafrom-20210106"
acl = "private" # or can be "public-read"
tags = {
Name = "Bucket"
Environment = "Production"
}
}

Now, initialize, plan, and execute your code. Initializing terraform will make necessary configuration, planning is like you see what will happen in actuality. It will also find out your syntax error. And, finally applying means you will deploy the code in the cloud. Let's execute the following command one by one.

Initializing terraform will make the necessary configuration. So, execute the following command,

$ terraform init

Planning is like you see what will happen in actuality. It will also find out your syntax error.

$ terraform plan

Applying means you will deploy the code in the cloud. Do it just by executing the command,

$ terraform apply

It will ask you for confirmation. Just type 'yes' and hit enter. Within few second your infrastructure will be ready.

You can now login to the AWS console and go to the service ec2. You will find ec2 is launched.

Similarly, go to s3 and search your bucket.

Destroy infrastructure

If you want to destroy the above infrastructure, you can simply type 'terraform destroy'.

$ terraform destroy

It asks you for the confirmation just say 'yes' and hit enter. The ec2 instance and s3 bucket you create above should be removed from your AWS account. You may verify by logging into the AWS console.

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'.

Conclusion

This guide has simply guided you to install terraform, launching ec2 instance, and creating s3 bucket using it.  We also showed you to destroy your infrastructure created from terraform.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment