IBM Cloud Global

 View Only

IBM Cloud and Terraform : The Perfect Combination for Virtual Server Provisioning

By Gunasekaran Venkatesan posted Mon May 01, 2023 05:18 PM


IBM Cloud is a powerful cloud platform that allows businesses to quickly and easily provision virtual resources, such as virtual servers. However, provisioning these resources can be a time-consuming and error-prone process, especially when done manually. Fortunately, Terraform provides a solution to this problem by allowing you to automate the provisioning process.

In this blog post, we'll guide you through the steps required to provision an IBM Cloud Classic Virtual Server using Terraform.


Before we begin, you will need to have the following:

  1. An IBM Cloud account: You can create a free account at
  2. The IBM Cloud command-line interface (CLI): You can download it from
  3. Terraform: You can download it from

Step 1: Set up IBM Cloud CLI

Once you have installed the IBM Cloud CLI, you will need to log in to your IBM Cloud account by running the following command:

ibmcloud login
This will prompt you to enter your email and password associated with your IBM Cloud account. Once you have logged in successfully, you can set your target region by running the following command:
ibmcloud target -r <region>

Replace <region> with the region you want to target, such as us-south or eu-de.

Step 2: Install Terraform

we need to have it installed on our local machine. You can download Terraform from the official website, and installation instructions are provided for all major operating systems. For Mac OS, please copy the following contents:

brew tap hashicorp/tap
brew install hashicorp/tap/terraform

For Linux- Ubuntu/Debian:

wget -O- | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform

For Linux- Redhat/Centos:

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo
sudo yum -y install terraform

Step 3: Create an API key

To provision a virtual classic server using Terraform, you will need to create an API key in IBM Cloud. You can create an API key by following these steps:

  1. Log in to your IBM Cloud account and navigate to the Manage -> Access (IAM) page.

  2. Click on the API keys tab and then click the Create an IBM Cloud API key button.

  3. Give your API key a name and click Create.

Copy the API key value and store it in a safe place. You will need this later.

Step 4: Define Virtual Server Configuration

Next, we will create a Terraform configuration file that describes the virtual classic server we want to provision. Open a text editor, Create a file called and add the following contents:
resource "ibm_compute_vm_instance" "vm1" {
hostname             = "vm1"
domain               = ""
os_reference_code    = "DEBIAN_10_64"
datacenter           = "dal10"
network_speed        = 10
hourly_billing       = true
private_network_only = false
cores                = 1
memory               = 1024
disks                = [25]
local_disk           = false

This code defines an IBM Cloud Virtual Server with a specific name and hostname, running Debian 10. In the above configuration file, about the description of ibm_compute_vm_instance resource argument and its values, refer to registry documentation of ibm_compute_vm_instance

Step 5: Create a file

The file is where we'll specify the IBM Cloud Classic Infrastructure API credentials required to connect to the API. Open a text editor, create a new file, and save it as Add the following code to the file:
provider "ibm" {
    ibmcloud_api_key = "<api_key>"
    iaas_classic_username = "<classic_username>"
    iaas_classic_api_key = "<classic_api_key>"
    region      = "<your_region>"
Please do not commit the file containing the provider block into a public source repository as it contains sensitive information. Kindly refer here for arguments are supported in the provider block.

To obtain your IBM Cloud API Keys and Classic Infrastructure API Keys, please follow these steps:

  1. Go to the IBM Cloud website and create or find your API keys for ibmcloud_api_key and iaas_classic_api_key here.
  2. To view your ibmcloud_api_key, select the "My IBM Cloud API Keys" option from the view dropdown.
  3. To view your iaas_classic_api_key, select the "Classic Infrastructure API Keys" option from the view dropdown.
  4. To obtain your iaas_classic_username, go to the "Users" section on the IBM Cloud website. Click on the desired user and find their username in the "VPN password" section under the "User Details" tab.                                                                       

Step 6: Create a file

The file is where we'll specify the required version of Terraform. Open a text editor, create a new file, and save it as Add the following code to the file:

terraform {
    required_version = ">=1.0.0, <2.0"
    required_providers {
        ibm = {
            source = "IBM-Cloud/ibm"

Step 6: Initialize Terraform

Now that we have all the necessary files, we need to initialize Terraform. Open a terminal or command prompt, navigate to the directory where your Terraform files are stored, and run the following command:


terraform init

This command will download the necessary provider plugins and initialize the Terraform backend.

Step 6: Plan the Provisioning

Once Terraform is initialized, we can use the command terraform plan to see what changes will be made to our infrastructure. This allows us to review the changes before they're actually made, which can help prevent mistakes and ensure that our infrastructure is provisioned correctly. This will trigger a validation process where the syntax of your configuration file and resource definitions will be checked against the specifications provided by the IBM Cloud Provider plug-in.

terraform plan

Step 7: Apply the Provisioning

If the plan looks good, you can proceed with provisioning your virtual server. Run the following command in the terminal:

terraform apply

This command will create the resources defined in your file and apply any other changes that were shown in the plan.

Please confirm the creation by typing "yes" when prompted.

Congratulations! You have successfully provisioned an IBM Cloud Classic Virtual Server using Terraform. You can now log in to your IBM Cloud Classic Infrastructure console and verify that your virtual server has been created.


In this guide, we've shown you how to provision an IBM Cloud Classic Virtual Server using Terraform. By automating the provisioning process, you can save time and reduce the risk of errors. With Terraform, you can easily manage your IBM Cloud Classic Infrastructure resources and make changes to them as needed. We hope this guide has been helpful, and happy Terraforming!

IBM Cloud Classic Infrastructure provider documentation for Terraform:

Getting started with Terraform on IBM Cloud: