---
title: "Getting Started with Terraform | Hostman Docs"
description: "Learn how to install and use Terraform to provision cloud resources with the HashiCorp provider and Hostman. Step-by-step guide covers installation on Linux, Windows, and macOS, writing .tf configuration files, authentication with API tokens, and deploying cloud servers using Infrastructure as Code principles."
---

> For the complete documentation index for AI agents, see [llms.txt](https://hostman.com/llms.txt).

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.

> [!NOTE]
> You can find more details on using Hostman Terraform provider in our [GitHub profile](https://github.com/hostman-cloud/terraform-provider-hostman).

## Step 1. Installing Terraform

First, let’s learn how to install Terraform on Linux, Windows, and macOS.

### Installing Terraform on Linux (Ubuntu)

You can run these commands as `root` or as a user with `sudo` privileges.

First, update the package list:

```shell
apt update
```

Next, 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:

```shell
apt install wget unzip
cd ~
wget https://releases.hashicorp.com/terraform/1.12.2/terraform_1.12.2_linux_amd64.zip
```

In the last command, we specify the latest version as of writing—1.12.2. You can check the latest releases [here](https://developer.hashicorp.com/terraform/install). 

Now, unzip the archive:

```shell
unzip terraform_1.12.2_linux_amd64.zip
```

And move the file to the directory:

```shell
mv terraform /usr/local/bin/
```

That’s it. Now you can verify that Terraform is successfully installed:

```shell
terraform -v
```

If everything works correctly, you will see a similar output:

```shell
Terraform v1.12.2
on linux_amd64
```

### Installing Terraform on Windows

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:

```shell
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:

```shell
choco install terraform
```

Verify that Terraform is successfully installed:

```shell
terraform -v
```

#### Manual Installation

To install the latest version of Terraform, follow these steps: 

1.  Go to the [HashiCorp website](https://developer.hashicorp.com/terraform/install) and select the version suitable for your system.
2.  Download the installation file.
3.  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:

```shell
C:\Windows\system32>C:\Terraform\terraform -v
```

```shell
Terraform v1.12.2  
on windows_amd64
```

If you try to access Terraform by simply using its name, you will get an error:

```shell
C:\Windows\system32>terraform -v
```

```shell
'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. 

1.  Go to **Control Panel** → **System and Security**.
2.  Click on **Change system environment variables** in the **System** section. This will open the **System Properties** window
3.  Go to **Advanced** → **Environment Variables**.
4.  Find **PATH** in **System variables** and click **Edit**.
5.  Click **New** and enter the path to the directory where you extracted Terraform.
6.  Click **OK**.

Check that everything works well by running in the command line:

```shell
terraform -v
```

Output:

```shell
Terraform v1.12.2
on windows_amd64
```

### Installing Terraform on macOS

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:

```shell
brew tap hashicorp/tap
```

Next, you can install Terraform by running:

```shell
brew install hashicorp/tap/terraform
```

Check that the installation was successful by requesting the Terraform version:

```shell
terraform -v
```

#### Manual Installation on macOS

1.  Go to the [HashiCorp website](https://developer.hashicorp.com/terraform/install) and download the zip archive suitable for your system.
2.  Unzip it.
3.  List the locations in your `PATH` using the command:

```shell
echo $PATH
```

4.  Now, you need to place the Terraform executable file to one of the locations in the list. You can do it with the command:
    

```shell
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:

```shell
mv ~/Downloads/terraform /usr/local/bin/
```

## Step 2. Creating the .tf Configuration File

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

Now let’s configure the Terraform provider in our configuration file.

At the beginning of the `hm_project.tf` file add the following lines:

```shell
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:

```shell
terraform init
```

## Step 4. Obtaining a Token

To work with the provider, you will need an API token that you can obtain in the [API](https://my.hostman.com/api-keys) section of your Hostman dashboard.

1.  Click **Create**.
2.  Enter a token name; it will be displayed in the dashboard.
3.  Select the validity period for the token.
4.  Click **Issue**.
5.  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:

```shell
provider "hm" {
  token = "fb246030216d5g30b1g6228e3071037g"
}
```

Make sure to replace `fb246030216d5g30b1g6228e3071037g` with your own token.

## Step 5. Preparing the Configuration

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:

```shell
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](https://hostman.com/docs/cloud-servers/ssh-keys/) in our documentation. Next, copy your public key to the server by adding the following configuration to the `hm_project.tf` file:

```shell
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`:

```shell
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:

```shell
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

Use this command:

```shell
terraform validate
```

If 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

First, enter the following command:

```shell
terraform plan
```

It 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:

```shell
terraform apply
```

This 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:

```shell
terraform destroy
```

In the next step, confirm the deletion by typing `yes`.

## Conclusion

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](https://developer.hashicorp.com/terraform/cli). And to find more about the Hostman Terraform provider, check [our GitHub page](https://github.com/hostman-cloud/terraform-provider-hostman).
