Back

How to create a dedicated server via Terraform

Prerequisites

You must have:

Install Terraform

Follow the steps below to install Terraform on macOS using Homebrew:

  1. Open the terminal and install the HashiCorp tap (a repository of Homebrew packages):
    brew tap hashicorp/tap
  2. Install Terraform
    brew install hashicorp/tap/terraform
  3. Verify the installation
    $ terraform -help

For other operating systems, refer to the official Terraform installation guide.

Create a manifest file

Create a manifest file (e.g., main.tf) in an empty working directory. It should begin with:


terraform {
  required_providers {
    serverscom = {
      source = "serverscom/serverscom"
    }
  }
}
provider "serverscom" {
  token = "<PAPI token>"
  endpoint = "<API URL>"
}

Replace <PAPI token> with your actual API token.

You can find <API URL> in the customer portal under Identity and AccessAPI tokens.

Define resources in the manifest

In Terraform, a resource is an object on which an operation is performed (create, modify, or destroy). It can be a server, load balancer or any other standalone service.

You can find the list of available resources in the Servers.com Terraform documentation.

Example manifest:


terraform {
  required_providers {
    serverscom = {
      source = "serverscom/serverscom"
    }
  }
}
provider "serverscom" {
  token = "<PAPI token>"
  endpoint = "<API URL>"
}
 
resource "serverscom_dedicated_server" "dedicated_server_ubuntu22" {
  hostname             = "staging-host1"
  location             = "AMS1"
  server_model         = "Dell R220 / Intel Xeon E3-1231 v3 / 32 GB RAM / 2x2 TB SATA"
  ram_size             = 32
  operating_system     = "Ubuntu 22.04-server x86_64"
  private_uplink       = "Private 1 Gbps without redundancy"
  public_uplink        = "Public 1 Gbps without redundancy"
  bandwidth            = "20002 Gb"
  ssh_key_fingerprints = [
    "<SSH-KEY-FINGERPRINT>"
  ]
  slot {
    drive_model = "2 TB SATA"
    position    = 0
  }
 
  layout {
    raid = 0
    slot_positions = [0]
     
    partition {
      target = "/boot"
      size = 1024
      fill = false
      fs = "ext4"
    }
 
    partition {
      target = "/"
      size = 10240
      fill = false
      fs = "ext4"
    }
 
    partition {
      target = "swap"
      size = 4096
      fill = false
    }
     
    partition {
      target = "/home"
      size = 1
      fill = true
      fs = "ext4"
    }
     
  }
}

The values shown here may become outdated. Always review Terraform error messages and update your configuration accordingly. Use the Public API queries to fetch the latest valid values.
See Public API documentation.

The values shown here may become outdated. Always review Terraform error messages and update your configuration accordingly. Use the Public API queries to fetch the latest valid values.

See Public API documentation.

Resource reference breakdown:

serverscom_dedicated_server The name of the provider and the type of resource being managed (in this case, a dedicated server).
dedicated_server_ubuntu22 The internal name of the resource in Terraform. This name is not passed to the API and must be unique in your configuration.
hostname = "staging-host1" The name of the resource within the servers.com infrastructure. This value is passed via API to servers.com and must follow valid DNS format (no spaces or special characters).
ssh_key_fingerprints The fingerprint of an SSH key you previously added via the customer portal, in MD5 format. You can find this in the customer portal under Identity and AccessSSH & GPG Keys. Terraform uses the fingerprint to associate the key to the provisioned resource.

Initialize the working directory

Run the following command in the directory where your .tf manifest file is located:

terraform init

This is the first required command for any Terraform project. It initializes the working directory based on the configuration defined in the manifest file and prepares dependencies.

You do not need to run Terraform initialization every time. Only in the following cases:

  • When setting up the provider for the first time
  • The provider or backend configuration has changed
  • The Terraform or provider version has changed

Expected output (partial):


Initializing the backend...
Initializing provider plugins...
- Finding latest version of serverscom/serverscom...
...

Terraform has been successfully initialized!
...

Plan infrastructure changes

To preview the changes Terraform will make to your infrastructure without applying them:

terraform plan

This command:

  • Reads the configuration from .tf file
  • Compares it with the current infrastructure state (using the .tfstate file and API)
  • Outputs the changes to be made: created (+), updated (~), or destroyed (-)

Apply the configuration

To provision the resources defined in your configuration:

terraform apply

Terraform will display the planned infrastructure changes again and prompt for confirmation. Type yes to proceed.

Do not close the terminal while the provisioning is in progress.

After successful execution, you will see an output similar to:


serverscom_dedicated_server.dedicated_server_ubuntu22: Creation complete after 1h01m255 [ id=eXaMp1e]
Apply complete! Resources: 1 added, • changed, • destroyed.

Removing resources

List all resources currently managed by Terraform:

terraform state list

Example output:


serverscom_dedicated_server.dedicated_server_debian12
serverscom_dedicated_server.dedicated_server_ubuntu22
serverscom_cloud_computing_instance.cloud_instance

To destroy a specific resource (e.g., dedicated_server_ubuntu22):

terraform destroy -target=serverscom_dedicated_server.dedicated_server_ubuntu22

To destroy all resources managed by Terraform:

terraform destroy

If you have already removed a resource manually (via the customer portal or API), but it is still tracked in Terraform, you can remove it from the Terraform state:

terraform state rm serverscom_dedicated_server.dedicated_server_ubuntu22

This command does not remove the resource from servers.com infrastructure, it only removes the record from Terraform state.

Suggested Articles

  • Dedicated servers

    How to create a dedicated server via public API

  • Dedicated servers

    Getting started