How to Provision Infrastructure with Terraform

August 25, 2022

Introduction

Terraform is an Infrastructure as Code (IaC) tool for provisioning data center infrastructure. The tool uses the declarative approach to configuration, allowing users to define and provision infrastructure securely and efficiently.

This tutorial will show you how to use Terraform to facilitate infrastructure provisioning. The article uses phoenixNAP's Bare Metal Cloud solution to illustrate the procedure.

How to provision infrastructure with Terraform.

Prerequisites

Provision Infrastructure with Terraform

When provisioning infrastructure using Terraform, users create declarative configuration files that define the infrastructure provider and the resources to provision. The files are written in HashiCorp Configuration Language or JSON.

The following sections will show you how to:

  • Register your provider and credentials.
  • Create and provision resources in Terraform.
  • Remove resources provisioned with Terraform.

Install Infrastructure Provider

Terraform lets you provision infrastructure from a large number of supported providers. Visit Terraform's website to search the list of supported providers.

As an example, this tutorial uses pnap, a verified Terraform provider that allows you to provision phoenixNAP's Bare Metal Cloud servers. Follow the steps below to register pnap on your system.

1. Browse to the provider's page in Terraform Registry.

2. Select the Use Provider button in the upper-right corner of the page.

3. Copy the code from the How to use this provider box.

The pnap provider page in the Terraform Registry.

4. On your system, create and go to the directory in which you will store your Terraform configuration:

mkdir bmc && cd bmc

5. Use a text editor to create the main Terraform configuration file:

nano main.tf

6. Paste the provider definition into the file.

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

provider "pnap" {
  # Configuration options
}

7. Save the file and exit.

Provide Credentials

To communicate with the Bare Metal Cloud, you need to connect your API credentials to the Terraform installation.

To do so:

1. Log in to the Bare Metal Cloud portal.

Note:  If you are not registered in the Bare Metal Cloud portal, learn how to create an account. For further assistance, watch the BMC account creation video instructions.

2. Select API Credentials in the menu on the left side.

The location of the API credentials item in the main menu of the BMC portal.

3. Select the Actions item for the credentials you wish to use and click View Credentials in the menu.

Accessing the API credentials.

4. On your system, create and go to the .pnap directory. The directory needs to be in your home directory.

mkdir ~/.pnap && cd ~/.pnap

5. Use a text editor to create the configuration YAML file:

nano config.yaml

6. Return to the BMC portal and copy your Client ID and Client Secret by clicking the copy buttons.

Copying the API credentials.

7. Paste the ID and the secret into the config.yaml using the following format:

clientID: [your-client-id]
clientSecret: [your-client-secret]

8. Save the file and exit.

Create Resources in Terraform

Resources in Terraform are defined in .tf files using the arguments supported by the specific provider.

1. Create a file for a new resource.

nano resource.tf

2. Type the server configuration. To provision a server, the pnap provider requires the following arguments:

  • hostname - the hostname of the server.
  • os - the operating system to be installed on the server. For example, ubuntu/bionic.
  • type - the server type ID. For example, s1.c1.medium.
  • location - the location of the server. For example, to provision a server located in Phoenix, type PHX.

Note: The full lists of supported operating systems, server types, and locations are available in the BMC Developer Portal.

Following is a configuration that uses the examples listed above.

resource "pnap_server" "Test-Server-1" {
    hostname = "Test-Server-1"
    os = "ubuntu/bionic"
    type = "s1.c1.medium"
    location = "PHX"
    install_default_ssh_keys = true
    ssh_keys = [
        "[your-SSH-key]"
    ]
}

3. Save the file and exit.

Note: For the list of all the available arguments, refer to the pnap provider documentation in the Terraform Registry.

Provision Resource in Terraform

Terraform utilizes the terraform command and subcommands to provision and manage resources. The steps below explain how to initialize the configuration, prepare for the provisioning, and provision new infrastructure.

1. Initialize the provider with the following command:

terraform init
The output of the terraform init command shows that the provider has been initialized.

The output shows Terraform successfully initializing the pnap provider.

2. To check the execution plan before applying the changes, type:

terraform plan

Terraform generates an execution plan and outputs the actions that will be performed.

The output of the terraform plan command shows actions that will be performed.

3. Apply the configuration by typing:

terraform apply

The output shows the plan and asks you to confirm it:

Confirming an action in Terraform.

4. Confirm the plan by entering yes.

Terraform starts the process, and you can follow the progress in the output. When the configuration is applied, the confirmation message appears.

Terraform confirming the creation of a resource.

Return to the BMC portal and confirm that the new server is listed in the Servers section.

The server created with Terraform shows up in the Servers section of the BMC portal.

Remove Terraform Resources

To remove all the resources created by Terraform, use the following command:

terraform destroy

To remove a specific resource:

1. Open the file that contains the resource configuration.

nano resource.tf

2. Comment out the resource by typing the # sign at the beginning of each related line.

Commenting out the resource in a Terraform configuration file.

Save the file and exit.

3. Apply the new configuration:

terraform apply

4. Terraform reports that one resource is scheduled to be destroyed. Confirm the action by entering Yes.

Confirming the action of destroying a resource in Terraform.

The output confirms the resource removal.

Terraform confirming the removal of a resource.

Conclusion

After following this tutorial, you should know how to provision a server using Terraform. The article covered the steps to register an infrastructure provider, define resources, and provision them using Terraform.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
Terraform vs Kubernetes: What Are the Differences
November 9, 2023

Automation has become one of the most important concepts in software development. Automating infrastructure speeds up configuration changes, eliminates the human error risk factor, and provides the necessary transparency for all..
Read more
How to Install Terraform on CentOS 7/ Ubuntu
August 25, 2022

Terraform is a tool that creates a single provisioned interface for several different cloud-based services. It is sometimes referred to as an "Infrastructure as Code" tool because it uses a configuration file to manage resources.
Read more
Jenkins vs. Kubernetes: What Is the Difference?
May 26, 2022

Kubernetes and Jenkins are excellent open-source tools and an integral part of the DevOps pipeline. Although both tools automate software development processes, their similarities end there.
Read more
CI/CD Security - How to Secure Your CI/CD Pipeline
August 26, 2021

The CI/CD pipeline is the pillar of software development and one of the main components of the DevOps pipeline. The continuous integration/delivery (or deployment) process defines a series of steps for software engineers to...
Read more