Infrastructure as Code (IaC) simplifies the process of managing virtualized cloud resources. With the introduction of cloud-native dedicated servers, it is now possible to deploy physical machines with the same level of flexibility.

phoenixNAP’s cloud-native dedicated server platform Bare Metal Cloud (BMC), was designed with IaC compatibility in mind. BMC is fully integrated with HashiCorp Terraform, one of the most widely used IaC tools in DevOps. This integration allows users to leverage a custom-built Terraform provider to deploy BMC servers in minutes with just a couple lines of code.

Why Infrastructure as Code?

Infrastructure as Code is a method of automating the process of deploying and managing cloud resources through human-readable configuration files. It plays a pivotal role in DevOps, where speed and agility are of the essence.

Before IaC, sys admins deployed everything by hand. Every server, database, load balancer, or network had to be configured manually. Teams now utilize various IaC engines to spin up or tear down hundreds of servers across multiple providers in minutes.

While there are many powerful IaC tools on the market, Terraform stands out as one of the most prominent players in the IaC field.

The Basics of Terraform

Terraform by HashiCorp is an infrastructure as code engine that allows DevOps teams to safely deploy, modify, and version cloud-native resources. Its open source tool is free to use, but most teams choose to use it with Terraform Cloud or Terraform Enterprise, which enable collaboration and governance.

To deploy with Terraform, developers define the desired resources in a configuration file, which is written in HashiCorp Configuration Language (HCL). Terraform then analyzes that file to create an execution plan. Once confirmed by the user, it executes the plan to provision precisely what was defined in the configuration file.

Terraform identifies differences between the desired state and the existing state of the infrastructure. This mechanism plays an essential role in a DevOps pipeline, where maintaining consistency across multiple environments is crucial.

Deploying Bare Metal Cloud Servers with Terraform

Terraform maintains a growing list of providers that support its software. Providers are custom-built plugins from various service providers that users initialize in their configuration files.

phoenixNAP has its own Terraform provider – pnap. Any Bare Metal Cloud user can use it to deploy and manage BMC servers without using the web-based Bare Metal Cloud Portal. The source code for the phoenixNAP provider and documentation is available on the official Terraform provider page.

Terraform Example Usage with Bare Metal Cloud

To start deploying BMC servers with Terraform, create a BMC account, and install Terraform on your local system or remote server. Before running Terraform, gather necessary authentication data and store it in the config.yaml file. You need the clientId and clientSecret, both of which can be found on your BMC account.

Once everything is set up, start defining your desired BMC resources. To do so, create a Terraform configuration file and declare that you want to use the pnap provider:

terraform {
  required_providers {
    pnap = {
          source = "phoenixnap/pnap"
          version = "0.6.0"
    }
  }
}

provider "pnap" {
  # Configuration options
}

The section reserved for configuration options should contain the description of the desired state of your BMC infrastructure.

To deploy the most basic Bare Metal Cloud server configuration, s1.c1.small, with an Ubuntu OS in the Phoenix data center:

resource "pnap_server" "My-First-BMC-Server" {
    hostname = "your-hostname"
    os = "ubuntu/bionic"
    type = "s1.c1.small"
    location = "PHX"
    ssh_keys = [
       "ssh-rsa..."
    ]
    #action = "powered-on"
}

The argument name action denotes power actions that can be performed on the server, and they include reboot, reset, powered-on, powered-off, shutdown. While all argument names must contain corresponding values, the action argument does not need to be defined.

To deploy this Bare Metal Cloud instance, run the terraform init CLI command to instruct Terraform to begin the initialization process.

Your Terraform configurations should be stored in a file with a .tf extension. While Terraform uses a domain-specific language for defining configurations, users can also write configuration files in JSON. In that case, the file extension needs to be .tf.json.

IaC with Terraform on Bare metal Cloud

All Terraform configuration files are reusable, scalable, and can be versioned for easier team collaboration on BMC provisioning schemes.

Whether you need to deploy one or hundreds of servers, Terraform and BMC will make it happen. There are no limits to how many servers you can define in your configuration files. You can also use other providers alongside phoenixNAP.

For easier management of complex setups, Terraform has a feature called modules — containers that allow you to define the architecture of your environment in an abstract way. Modules are reusable chunks of code that can call other modules that contain one or more infrastructure objects.

Collaborating on BMC Configurations with Terraform Cloud

Once you’ve learned how to write and provision Terraform configurations, you’ll want to set up a method that allows your entire DevOps team to work more efficiently on deploying new and modifying existing BMC resources.

You can store Terraform configuration in a version control system and run them remotely from Terraform Cloud for free. This helps you reduce the chance of deploying misconfigured resources, improves oversight, and ensures every change is executed reliably from the cloud.

You can also leverage Terraform Cloud’s remote state storage. Terraform state files map Terraform configurations with resources deployed in the real world. Using Terraform Cloud to store state files ensures your team is always on the same page.

Another great advantage of Terraform is that all configuration files are reusable. This makes replicating the same environment multiple times extremely easy. By maintaining consistency across multiple environments, teams can deliver quality code to production faster and safer.

Automate Your Infrastructure

This article gave you a broad overview of how to leverage Terraform’s flexibility to interact with your Bare Metal Cloud resources programmatically. By using the phoenixNAP Terraform provider and Terraform Cloud, you can quickly deploy, configure, and decommission multiple BMC instances with just a couple of lines of code.

This automated approach to infrastructure provisioning improves the speed and agility of DevOps workflows. BMC, in combination with Terraform Cloud, allows teams to focus on building software rather than wasting time waiting around for their dedicated servers to be provisioned manually.