DEV Community

Cover image for How to Install Terraform on Ubuntu
Aditi Bindal for NodeShift

Posted on

How to Install Terraform on Ubuntu

Terraform is a powerful open-source tool that simplifies the process of developing and managing infrastructure components. With Terraform, you can define your infrastructure resources through human-readable configuration files, allowing you to automate resource creation, updates, and deletions. Its key features, like version control, modularity, and support for multiple cloud providers, make it popular for DevOps teams looking to reduce repetitive tasks and improve developer productivity. It works in the following three stages:

  • Write: You can declare configurations of infrastructure resources in human-readable language, which may work across multiple cloud providers.

  • Plan: It creates an execution plan to implement tasks such as create, update, and destroy according to the plan and your configuration.

  • Apply: Once you approve and apply the changes, it performs those actions in the specified order, keeping the dependencies in mind.

hero-image

In this guide, we’ll walk you through the steps to install Terraform on an Ubuntu machine. We'll then deploy NGINX on Docker using Terraform to show you how you can efficiently build infrastructure using Terraform.

Prerequisites

  • A Virtual Machine (such as the ones provided by NodeShift) with at least:

    • 1 vCPUs
    • 512 MB RAM
    • 50 GB SSD
  • Ubuntu 22.04 VM

  • Docker installed on Ubuntu

Note: The prerequisites for this are highly variable across use cases. A high-end configuration could be used for a large-scale deployment.

Step-by-step process to install Terraform on Ubuntu

For this tutorial, we'll use a CPU-powered Virtual Machine by NodeShift, which provides high-compute Virtual Machines at a very affordable cost on a scale that meets GDPR, SOC2, and ISO27001 requirements. It also offers an intuitive and user-friendly interface, making it easier for beginners to get started with Cloud deployments. However, feel free to use any cloud provider you choose and follow the same steps for the rest of the tutorial.

Step 1: Setting up a NodeShift Account

Visit app.nodeshift.com and create an account by filling in basic details, or continue signing up with your Google/GitHub account.

If you already have an account, login straight to your dashboard.

Image-step1-1

Step 2: Create a Compute Node (CPU Virtual Machine)

After accessing your account, you should see a dashboard (see image), now:

1) Navigate to the menu on the left side.

2) Click on the Compute Nodes option.

Image-step2-1

3) Click on Start to start creating your very first compute node.

Image-step2-2

These Compute nodes are CPU-powered virtual machines by NodeShift. These nodes are highly customizable and let you control different environmental configurations, such as vCPUs, RAM, and storage, according to your needs.

Step 3: Select configuration for VM

1) The first option you see is the Reliability dropdown. This option lets you choose the uptime guarantee level you seek for your VM (e.g., 99.9%).

Image-step3-1

2) Next, select a geographical region from the Region dropdown where you want to launch your VM (e.g., United States).

Image-step3-2

3) Most importantly, select the correct specifications for your VM according to your workload requirements by sliding the bars for each option.

Image-step3-3

Step 4: Choose VM Configuration and Image

1) After selecting your required configuration options, you'll see the available VMs in your region and as per (or very close to) your configuration. In our case, we'll choose a '1vCPUs/2GB/50GB SSD' as the closest match to the "Prerequisites".

2) Next, you'll need to choose an image for your Virtual Machine. For the scope of this tutorial, we'll select Ubuntu, as we will install Terraform on Ubuntu.

Image-step4-1

Step 5: Choose the Billing cycle and Authentication Method

1) Two billing cycle options are available: Hourly, ideal for short-term usage, offering pay-as-you-go flexibility, and Monthly for long-term projects with a consistent usage rate and potentially lower cost.

Image-step5-1

2) Next, you'll need to select an authentication method. Two methods are available: Password and SSH Key. We recommend using SSH keys, as they are a more secure option. To create one, head over to our official documentation.

Image-step5-2

Step 6: Finalize Details and Create Deployment

Finally, you can also add a VPC (Virtual Private Cloud), which provides an isolated section to launch your cloud resources (Virtual machine, storage, etc.) in a secure, private environment. We're keeping this option as the default for now, but feel free to create a VPC according to your needs.

Also, you can deploy multiple nodes at once using the Quantity option.

Image-step6-1

That's it! You are now ready to deploy the node. Finalize the configuration summary; if it looks good, go ahead and click Create to deploy the node.

Step 7: Connect to active Compute Node using SSH

As soon as you create the node, it will be deployed in a few seconds or a minute. Once deployed, you will see a status Running in green, meaning that our Compute node is ready to use!

Image-step7-1

Once your node shows this status, follow the below steps to connect to the running VM via SSH:

1) Open your terminal and run the below SSH command:

(replace root with your username and paste the IP of your VM in place of ip after copying it from the dashboard)

ssh root@ip
Enter fullscreen mode Exit fullscreen mode

2) In some cases, your terminal may take your consent before connecting. Enter ‘yes’.

3) A prompt will request a password. Type the SSH password, and you should be connected.

Output:
Image-step7-2

Step 8: Install Terraform

1) Download and save the Hashicorp PGP key.

wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step8-1

2) Add the key to Ubuntu's package source-list.

echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step8-2

3) Update the package source-list.

apt update
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step8-3

4) Install Terraform with the below command.

apt install terraform
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step8-4

5) Confirm installation

terraform -version
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step8-5

Step 9: Configure the infrastructure

Now that Terraform is installed, let's configure and build an infrastructure using Terraform. For demonstration purposes, we'll build a basic infrastructure by deploying NGINX on Docker using Terraform.

1) Create a directory

First, create a separate directory for the infrastructure, we are naming it terraform,

mkdir terraform
Enter fullscreen mode Exit fullscreen mode

and then navigate inside the directory:

cd terraform
Enter fullscreen mode Exit fullscreen mode

2) Edit the main.tf using Nano editor

nano main.tf
Enter fullscreen mode Exit fullscreen mode

main.tf is the main configuration file of terraform where we'll define the settings of our infrastructure to instruct Terraform to use the Docker provider to NGINX image and create a resource named nginx from it.

Copy and paste the below configuration to the file:

terraform {
  required_providers {
    docker = {
      source = "kreuzwerker/docker"
      version = "~> 3.0.1"  
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.image_id
  name  = "NodeShift_demo"
  ports {
    internal = 80
    external = 8000
  }
}
Enter fullscreen mode Exit fullscreen mode

Once done, save and close the editor (Ctrl+O > ENTER > Ctrl+X).

3) Initialize the directory

We initialize the directory to let it download and install the requirements as per the configuration. Use the below command to initialize it:

terraform init
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step9-1

4) Confirm if the configuration is correct and valid.

terraform validate
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step9-2

Step 10: Build the infrastructure

Now, let's apply the configuration to build our infrastructure.

terraform apply
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step10-1

In between the process, it will ask for confirmation before performing some actions. Type "yes" to proceed with the actions.

Image-step10-2

See if the container is running correctly:

docker ps |grep NodeShift
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step10-3

Destroy the infrastructure

In case you want to destroy the server, you can do so using the below command:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

Note: In some cases, as you run a terraform command such as "apply" or "destroy", you may run into an error like this:

Image-step10-4

Terraform usually throws this error to prevent the file from being modified by more than one user at a time, mostly when it is modified through the apply or destroy command.

To disable this lock, use your command with the "-lock=false" prefix.

For example:

terraform destroy -lock=false
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step10-5

Image-step10-6

As you may observe in the output, running the command with the lock disabled successfully destroyed the infrastructure.

Conclusion

Installing Terraform and building infrastructure with it is a simple process that empowers developers to manage infrastructure as code efficiently. By following the steps outlined in this guide, you can set up Terraform seamlessly and start automating your cloud infrastructure. At NodeShift, we continuously work to provide robust cloud essentials like GPUs, Compute nodes, etc., where deploying VMs like the one used in this tutorial becomes effortless. NodeShift offers the reliability and scalability you need so you can focus on building and innovating.

For more information about NodeShift:

Top comments (0)