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.
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.
Step 2: Create a Compute Node (CPU Virtual Machine)
After accessing your account, you should see a dashboard (see image), now:
- Navigate to the menu on the left side.
- Click on the Compute Nodes option.
- Click on Start to start creating your very first compute node.
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
- 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%).
- Next, select a geographical region from the Region dropdown where you want to launch your VM (e.g., United States).
- Most importantly, select the correct specifications for your VM according to your workload requirements by sliding the bars for each option.
Step 4: Choose VM Configuration and Image
- 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”.
- 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.
Step 5: Choose the Billing cycle and Authentication Method
- 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.
- 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.
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 in the Quantity option.
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!
Once your node shows this status, follow the below steps to connect to the running VM via SSH:
- 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
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:
Step 8: Install Terraform
- 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
Output:
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
Output:
3. Update the package source-list.
apt update
Output:
4. Install Terraform with the below command.
apt install terraform
Output:
5. Confirm installation
terraform -version
Output:
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.
- Create a directory
First, create a separate directory for the infrastructure, we are naming it terraform
,
mkdir terraform
and then navigate inside the directory:
cd terraform
2. Edit the main.tf
using Nano editor
nano main.tf
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
}
}
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
Output:
4. Confirm if the configuration is correct and valid.
terraform validate
Output:
Step 10: Build the infrastructure
Now, let’s apply the configuration to build our infrastructure.
terraform apply
Output:
In between the process, it will ask for confirmation before performing some actions. Type “yes
” to proceed with the actions.
See if the container is running correctly:
docker ps |grep NodeShift
Output:
Destroy the infrastructure
In case you want to destroy the server, you can do so using the below command:
terraform destroy
Note: In some cases, as you run a terraform command such as “apply
” or "destroy
“, you may run into an error like this:
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
Output:
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.