How to Use the Terraform Command Line Interface on Ubuntu

Wondering how to use the Terraform Command Line Interface on Ubuntu? 

For those of you in the field of cloud computing, you might find yourself using the default GUI of your cloud-based application to clear for basic setups and development.

While this may be a simple and easy-to-understand approach, using these GUIs can be tiresome really quickly. This is because relying on the GUI means reconfiguring and replicating the configurations will involve a lot of going back and forth between different panels.

To avoid this problem, there was a need for a CLI that could save these configurations in a user-friendly format. Fortunately, Terraform gets the job well done. Terraform allows you to manage a variety of services via the CLI.

This guide is meant to help you learn how to use the Terraform CLI on Ubuntu. We’ll be using Ubuntu 20.04 LTS for this guide. However, the process shouldn’t be any different for other versions.

With that said, let’s begin!

Installing the Terraform CLI.

The first step is to install the Terraform CLI on your system. 

This step will make use of the Linux Command Terminal.

It should be noted that you would require an account that has access to sudo and/or root privileges for this method to work. 

Follow these steps to install the Terraform Command Line Interface on your system.

  1. Start by opening the Command Terminal. You can achieve this by pressing Ctrl + Alt + T on your keyboard. 
  2. Next, type the following command:
$ sudo apt update
Terraform Command Line Interface on Ubuntu

This should update your packages and repositories to the latest available version.

  1. Once the update finishes, type these commands to add the required repository keys.
$ sudo apt install curl
Terraform Command Line Interface on Ubuntu
$ curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
Terraform Command Line Interface on Ubuntu

These commands should fetch and add the Hashicorp keys required for installation.

  1. Afterward, add the Terraform repository to your list of packages by writing the following in the Command Terminal.
$ sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
Terraform Command Line Interface on Ubuntu
  1. Update your system once again to ensure the added packages are up to date,
$ sudo apt update
update
  1. Lastly, proceed to install terraform using the apt-install command.
$ sudo apt install terraform
install command

With the steps followed correctly, you should now have Terraform installed on your system. With this, you can proceed to learn how to use the Terraform Command Line Interface on Ubuntu.

Getting Started With Terraform CLI.

This section is meant to help you get started with the Terraform Command Line Interface.

As mentioned earlier, the Terraform CLI allows you to manage, configure and develop applications for your cloud-based services by using a simple, reader-friendly coding format.

Terraform comes with a variety of commands, each serving its own purpose. A list of useful Terraform commands is given below.

  • init            – This command creates a directory for commands
  • Validate – This command checks the validity of the configuration
  • Plan – This command displays the changes needed by the active configuration
  • Apply – This command is used to create the Terraform infrastructure
  • Destroy – This command is used to delete the previous infrastructure

You can learn more about these commands by visiting the official Terraform guide.

Cloning Using Terraform CLI

One of the advantages of Terraform is the ability to clone the code files quickly.

Cloning saves you from the problem above of reconfiguring your server manually. It automates the process of creating new configuration files. 

Follow these steps to clone your code using the Terraform CLI:

  1. First, open the Command Terminal by pressing Ctrl + Alt + T on your keyboard.
  2. Next, type the following:
$ sudo apt update
update

This will update the required packages and repositories.

  1. Once that’s finished, install git by typing this command:
$ sudo apt install git
install git

This will install the Git repository on your system.

  1. Afterward, make a copy of the Terraform CLI file by using the git clone command as follow:
$ git clone https://github.com/howtoforge-com/terraform-cli.git
Terraform Command Line Interface on Ubuntu
  1. Navigate to the newly created directory and then open the file titled main.tf using your preferred editor. The syntax for the command is as follows:
$ cd terraform-cli && sudo <editor name> main.tf

In our case,

$ cd terraform-cli && sudo nano main.tf
Terraform Command Line Interface on Ubuntu

Your output should be similar to the image below.

Here you can make changes in certain blocks of code as per need.

Understanding Contents within the main.tf File.

Upon opening the Main.tf file in the editor, you will see different code blocks. Each of these blocks of code controls certain aspects of your server. Details of these sections are given below:

Starting with the terraform{ } block, this block of code is responsible for providing access to the docker. If you wish to allow Terraform to handle your infrastructure, you need to configure the docker provider.

Terraform Command Line Interface on Ubuntu

The next block is for resource "docker_image" "nginx" { }. This block of code is responsible for creating and managing docker images. These images contain instructions that are used to form containers. This block of code is highly compatible with Nginx resources.

docker images

The last block of code that you need to know about is the resource "docker_container" "nginx" { }. This block of code is responsible for creating and managing docker containers. You can assign internal and external ports on your machine with the help of this block.

Terraform Command Line Interface on Ubuntu

You might have noticed that the Main.tf file has code segments that use Nginx. Nginx is an application that’s used to store and process web content before delivering it to the users accessing the network.

Nginx allows for optimized performance, which is a plus considering web requests can take up a lot of memory. With features like this, no doubt you’d want to learn more about Nginx. Learning how to install and use Nginx could give you and your website the competitive edge you’re looking for.

We hope this guide helped you learn how to use the Terraform Command Line Interface on Ubuntu. If you have any questions or queries, let us know in the comment section down below.

If this guide helped you, please share it. 🙂

Leave a Reply

Your email address will not be published.

Previous Post
Flashlight Not Working on iPhone

How to Fix Flashlight Not Working on iPhone

Next Post
Telegram Not Sending Messages on iPhone

How to Fix Links Not Opening on Telegram

Related Posts