Use the phoenixNAP Terraform provider (pnap) to provision and manage Bare Metal Cloud (BMC) servers through configuration files instead of the BMC portal. This tutorial covers the complete server lifecycle.
Prerequisites
Administrative access to a system with Terraform installed. You will also need a BMC account with API credentials and an SSH public key to install on the server.
Step 1: Configure the pnap Terraform Provider
1. Open a terminal and create a directory for your Terraform configuration files:
mkdir bmc && cd bmcCode language: Bash (bash)
2. Create the main configuration file:
nano main.tfCode language: Bash (bash)
3. Add the provider definition:
terraform {
required_providers {
pnap = {
source = "phoenixnap/pnap"
version = "0.34.0"
}
}
}
provider "pnap" {
# Configuration options
}
Code language: JSON / JSON with Comments (json)
The example uses the version 0.34.0. Check the Terraform pnap Registry for the latest version.
4. Save the file and exit the editor.
The configuration specifies the provider source and version. Terraform downloads the provider when you initialize the working directory.
Step 2: Configure API Credentials
1. In the BMC portal, open the API Credentials page.
2. Create API credentials with the bmc scope and copy the Client ID and Client Secret.
3. Set the PNAP_CLIENT_ID and PNAP_CLIENT_SECRET environment variables. For example, on Linux or macOS, set them in the terminal where you will run Terraform:
export PNAP_CLIENT_ID="[YOUR_ID]"
export PNAP_CLIENT_SECRET="[YOUR_SECRET]"Code language: Bash (bash)
Replace the example values with your credentials.
Warning
Do not commit secrets to version control or include them directly in configuration files.
Step 3: Define a BMC Server Resource
1. Create a file for a new resource in the Terraform project directory:
nano resource.tfCode language: Bash (bash)
2. Add the following configuration:
resource "pnap_server" "test_server" {
hostname = "test_server_1"
os = "ubuntu/noble"
type = "s1.c1.medium"
location = "PHX"
install_default_ssh_keys = true
ssh_keys = [
"[YOUR_SSH_KEY]"
]
}
Code language: JSON / JSON with Comments (json)
Replace [YOUR_SSH_KEY] with your actual SSH public key. Adjust the hostname, operating system, server type, and location as needed.
Note
The install_default_ssh_keys argument specifies whether to install SSH keys marked as default, while the ssh_keys argument specifies the public SSH key to install on the server.
Step 4: Initialize and Provision the Server
1. Initialize the provider:
terraform initCode language: Bash (bash)
2. Check the execution plan before applying the changes:
terraform planCode language: Bash (bash)
3. Apply the configuration:
terraform applyCode language: Bash (bash)
4. Enter Yes to confirm the action.
Terraform submits the request to BMC and reports progress as it provisions the server. When the operation completes, check the BMC portal Servers section to verify the server appears
Step 5: Remove Terraform Resources
Warning
Destroying a server can interrupt services and cause data loss. Review the execution plan carefully before confirming any deletion.
To remove a specific resource:
1. Open the file that contains the resource configuration:
nano resource.tfCode language: Bash (bash)
2. Remove or comment out the pnap_server resource block.
3. Save the file and exit the editor.
4. Apply the new configuration:
terraform applyCode language: Bash (bash)
5. Enter Yes to confirm the action.
To destroy all resources created by Terraform, run:
terraform destroyCode language: Bash (bash)
Review the proposed deletions and confirm the operation when prompted.