How to Install and Use Terraform on Ubuntu 20.04

install terraform ubuntu

What is Terraform?

Terraform is an open-source software tool created by Hashicorp. It is used to automate and manage your infrastructure, your platform and services that run on that platform. It uses a declarative language that means you don’t have to define every step of how this automation and management is done. With Terraform, you can create a VPS, AWS users and permissions, spin up servers and install the application on servers.

Difference Between Ansible and Terraform

  • Ansible and Terraform both are infrastructures as a code. That means both are used to automate provisioning, configuring and managing the infrastructure.
  • Terraform is mainly an infrastructure provisioning tool. However, you can also deploy applications using Terraform.
  • Ansible is mainly a configuration tool. It is used to configure that infrastructure like, deploy applications, install and update software, etc.
  • Terraform is best for infrastructure while the Ansible is best for configuring that infrastructure.

Terraform Providers

A provider in Terraform is basically a set of code that provides API level abstraction. It is responsible for the lifecycle of a resource: create, read, update, delete. Each and every Terraform provider has its own documentation, describing its resource types and their arguments. Currently, Terraform supports cloud service providers including, AWS, Alibaba Cloud, Google Cloud, Azure and many more.

Features

  • Support for Local and Remote execution
  • Workspaces for Organizing Infrastructure
  • Remote State Management, Data Sharing, and Run Triggers
  • Version Control Integration
  • Command-Line Integration
  • Private Module Registry
  • Notifications
  • Access Control

In this tutorial, we will show you how to install and use Terraform on Ubuntu 20.04.

Prerequisites

  • A server running Ubuntu 20.04.
  • A root password is configured on your server.

Install Terraform

By default, Terraform is not included in the Ubuntu standard repository. So you will need to add the Terraform repository to the APT.

First, install some packages required to install Terraform:

apt-get install curl gnupg2 software-properties-common -y

Once all the packages are installed, add the Terraform GPG key and repository with the following command:

curl -fsSL https://apt.releases.hashicorp.com/gpg | apt-key add -
apt-add-repository "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

Once the repository is added, install the Terraform by running the following command:

apt-get install terraform -y

Once the installation is finished, you can verify the installed version of Terraform with the following command:

terraform -v

You should see the installed version of Terraform in the following output:

Terraform v0.14.4

How to Use Terraform

After installing Terraform, you must have a knowledge of how to use Terraform to connect with different cloud providers like AWS, Azure, Google Cloud and more.

Create a Configuration File for Provider

Each and every provider has its own configuration file in order to connect to the provider. You can use .tf file to grant access to multiple cloud providers all at once. In this section, we will show you how to create a configuration file to connect and access the AWS provider.

First, create a directory to store the Terraform provider configuration file:

mkdir AWS

Next, change the directory to AWS and create a configuration file with the following command:

cd AWS
nano config.tf

Add the following lines as per your AWS configuration:

provider "aws" {
  region     = "us-west-2"
  access_key = "aws_access_key"
  secret_key = "aws_secret_key"
}

resource "aws_instance" "example" {
ami = "ami-2757f631"
instance_type = "t2.micro"
}

Save and close the file when you are done.

Note: Replace aws_access_key and aws_secret_key with your AWS keys.

Initialize Terraform

Next, you will need to initialize Terraform to download the AWS provider information to your system.

Run the following command to initialize the Terraform:

terraform init

This command will create a .terraform directory and download the AWS provider information inside it.

Create a Terraform Plan

Next, you will need to create a Terraform plan to test how Terraform works. You can use the terraform plan command to create an execution plan.

Run the following command to create a Terraform plan:

terraform plan

This will configure, reconfigures, and instantiates resources and their dependencies.

Connect Terraform to AWS Services

Next, you will need to execute the plan which you have created in the previous step. You can use the terraform apply command to apply the changes required to reach the desired state of the configuration.

Run the terraform apply command as shown below to connect to AWS using the information in your config.tf file:

terraform apply

Once the terraform plan has been executed successfully, wait for some time.

After a few seconds, your AWS instance has been started. You can check it from your AWS console.

Destroy the AWS Instance

You can also remove your test plan and destroy the AWS instance. You can achieve it using the terraform destroy command:

terraform destroy

You will be asked to type yes to destroy the instance.

Uninstall Terraform

If you want to remove the Terraform from your system, run the following command:

apt-get remove terraform -y

Update Terraform to the Latest Version

It is always recommended to update the Terraform to the latest version.

To update the Terraform, run the following command:

apt-get update -y
apt-get install --only-upgrade terraform

Conclusion

Congratulations! you have successfully installed Terraform on Ubuntu 20.04 server. You can now automate and manage your infrastructure with Terraform.

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva