Getting Started with Terraform
Terraform is a handy tool for quick provisioning and managing cloud infrastructure using configuration files.
When the configuration files change, Terraform automatically re-reads them and proceeds to create, update, or delete the resources, as described by the administrator.
In this article, we will describe how to begin working with Terraform and create cloud infrastructure in Hostman using it.
Step 1. Installing Terraform Copy link
First, let’s learn how to install Terraform on Linux, Windows, and macOS.
Installing Terraform on Linux (Ubuntu) Copy link
You can run these commands as root or as a user with sudo privileges.
First, update the package list:
apt updateNext, install wget and unzip we’ll use later, navigate to the directory where Terraform will be installed, and download the package. Run these commands consecutively:
apt install wget unzip
cd ~
wget https://releases.hashicorp.com/terraform/1.12.2/terraform_1.12.2_linux_amd64.zipIn the last command, we specify the latest version as of writing—1.12.2. You can check the latest releases here.
Now, unzip the archive:
unzip terraform_1.12.2_linux_amd64.zipAnd move the file to the directory:
mv terraform /usr/local/bin/That’s it. Now you can verify that Terraform is successfully installed:
terraform -vIf everything works correctly, you will see a similar output:
Terraform v1.12.2
on linux_amd64Installing Terraform on Windows Copy link
There are two ways of installing Terraform on Windows: via the Chocolatey package manager or manually.
While the Chocolatey method is straightforward, it won’t allow you to install the latest software version. If you want the latest version of Terraform, use the manual installation.
Via Chocolatey
The easiest way to install Terraform on Windows is using Chocolatey.
If you don’t have it yet, open PowerShell as administrator and run:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))Now you can use Chocolatey to install software on your machine.
Enter the following command into PowerShell window to install Terraform:
choco install terraformVerify that Terraform is successfully installed:
terraform -vManual Installation
To install the latest version of Terraform, follow these steps:
- Go to the HashiCorp website and select the version suitable for your system.
- Download the installation file.
- Extract the contents to a folder convenient for you, for example,
C:\Terraform.
At this point, Terraform is installed; however, you can only use it when specifying the full path in the command line:
C:\Windows\system32>C:\Terraform\terraform -v Terraform v1.12.2
on windows_amd64If you try to access Terraform by simply using its name, you will get an error:
C:\Windows\system32>terraform -v 'terraform' is not recognized as an internal or external command,
operable program or batch file. To be able to call terraform from the command line, you need to add it to the PATH environment variable.
- Go to Control Panel → System and Security.
- Click on Change system environment variables in the System section. This will open the System Properties window
- Go to Advanced → Environment Variables.
- Find PATH in System variables and click Edit.
- Click New and enter the path to the directory where you extracted Terraform.
- Click OK.
Check that everything works well by running in the command line:
terraform -v Output:
Terraform v1.12.2
on windows_amd64Installing Terraform on macOS Copy link
As with Windows, there are two ways of installing Terraform on your macOS machine: via the Homebrew package manager or by downloading the binary.
Via Homebrew
First, you need to install the official Hashicorp repository. Open the terminal and run:
brew tap hashicorp/tapNext, you can install Terraform by running:
brew install hashicorp/tap/terraformCheck that the installation was successful by requesting the Terraform version:
terraform -vManual Installation on macOS
- Go to the HashiCorp website and download the zip archive suitable for your system.
- Unzip it.
- List the locations in your
PATHusing the command:
echo $PATH-
Now, you need to place the Terraform executable file to one of the locations in the list. You can do it with the command:
mv ~/current/terraform/location /one/of/PATH/locations/For example, if the executable is located in your Downloads and your PATH includes /usr/local/bin, the command will look like this:
mv ~/Downloads/terraform /usr/local/bin/Step 2. Creating the .tf Configuration File Copy link
Terraform configuration files have the .tf extension. The file name is arbitrary; the program can read it anyway.
Let’s create a new directory (hostman_project) and place a configuration file there. We’ll call it hm_project.tf.
To create a .tf file, you can first make a simple text file and then change its extension to .tf.
One directory can have several configuration files belonging to the same project. For example, you can create a vars.tf file for describing variables, nginx.tf for configuring an Nginx server, etc. When you run terraform plan, all the .tf files will be combined into one.
However, in this article, we’ll be using one configuration file, hm_project.tf.
Step 3. Configuring the Provider Copy link
Now let’s configure the Terraform provider in our configuration file.
At the beginning of the hm_project.tf file add the following lines:
terraform {
required_providers {
hm = {
source = "hostman-cloud/hostman"
}
}
required_version = ">= 0.13"
}
In the source field, enter the provider URL. In this case, Hostman’s.
Now navigate to the directory with the configuration file and run this command to install the provider:
terraform initStep 4. Obtaining a Token Copy link
To work with the provider, you will need an API token that you can obtain in the API section of your Hostman dashboard.
- Click Create.
- Enter a token name; it will be displayed in the dashboard.
- Select the validity period for the token.
- Click Issue.
- Copy the issued token.
For example, we have obtained the token fb246030216d5g30b1g6228e3071037g.
We need to add it to the hm_project.tf file like this:
provider "hm" {
token = "fb246030216d5g30b1g6228e3071037g"
}Make sure to replace fb246030216d5g30b1g6228e3071037g with your own token.
Step 5. Preparing the Configuration Copy link
Using Terraform, you can manage various resources in your Hostman cloud, such as cloud servers, cloud databases, VPC, public IP addresses, etc.
As an example, let’s create a cloud server with the following specs:
- NVMe: 80 GB
- CPU: 2
- RAM: 4 GB
- OS: Ubuntu 22.04
We’ll name the server my-hostman-server; you can set up your own name.
To create the server, add the following configuration into the hm_project.tf file:
data "hm_configurator" "configurator" {
location = "us-2"
}
data "hm_os" "os" {
name = "ubuntu"
version = "22.04"
}
resource "hm_server" "my-hostman-server" {
name = "My Hostman Server"
os_id = data.hm_os.os.id
configuration {
configurator_id = data.hm_configurator.configurator.id
disk = 1024 * 80
cpu = 2
ram = 1024 * 4
}
}
If the program failed to create the server, check if you have entered the token correctly.
Pay attention to the errors that Terraform displays; in most cases, they make it clear what went wrong.
Additionally, you can set up SSH key authentication. You can generate the keys using this guide in our documentation. Next, copy your public key to the server by adding the following configuration to the hm_project.tf file:
resource "hm_ssh_key" "your-key" {
name = "Your key"
body = file("~/.ssh/your-key.pub")
}In the resource block, add the line ssh_keys_ids:
resource "hm_server" "my-hostman-server" {
name = "My Hostman Server"
os_id = data.hm_os.os.id
ssh_keys_ids = [hm_ssh_key.your-key.id]
}
Our final configuration file should look like this:
terraform {
required_providers {
hm = {
source = "hostman-cloud/hostman"
}
}
required_version = ">= 0.13"
}
provider "hm" {
token = "fb246030216d5g30b1g6228e3071037g"
}
data "hm_configurator" "configurator" {
location = "us-2"
}
data "hm_os" "os" {
name = "ubuntu"
version = "20.04"
}
resource "hm_ssh_key" "your-key" {
name = "Your key"
body = file("~/.ssh/your-key.pub")
}
resource "hm_server" "my-hostman-server" {
name = "My Hostman Server"
os_id = data.hm_os.os.id
ssh_keys_ids = [hm_ssh_key.your-key.id]
configuration {
configurator_id = data.hm_configurator.configurator.id
disk = 1024 * 80
cpu = 2
ram = 1024 * 4
}
}
Step 6. Testing the Configuration Copy link
Use this command:
terraform validateIf all the settings are correct, you will see the message that the configuration is valid. If any errors occur, check your configuration again.
Step 7. Applying the Configuration Copy link
First, enter the following command:
terraform planIt doesn’t apply your configuration, but displays all the planned resources so you can check the settings. If there are any issues, Terraform will point you to the places in the configuration that are set incorrectly.
If and when everything is fine, run:
terraform applyThis command applies the configuration and allows Terraform to create the described resources. Confirm by typing yes and pressing Enter.
Now you can visit your Hostman dashboard and make sure the resources are created.
Should you decide to remove them, you can use the command:
terraform destroyIn the next step, confirm the deletion by typing yes.
Conclusion Copy link
In this guide, we described how to create Terraform configuration files and create and configure resources in the Hostman cloud.
To learn more about working with Terraform, refer to the official documentation. And to find more about the Hostman Terraform provider, check our GitHub page.